284
These convenient persistent memory-aware synchronization primitives are available
for C and C++ developers. But what if a developer wants to use a custom synchronization
object that is more appropriate for a particular use case? As we mentioned earlier, the
main feature of persistent memory-aware synchronization primitives is that they are
reinitialized every time we open a persistent memory pool. The libpmemobj-cpp library
provides a more generic mechanism to reinitialize any user-provided type every time a
persistent memory pool is opened.
The libpmemobj-cpp provides the pmem::obj::v class template which
allows creating a volatile field inside a persistent data structure. The mutex object
is semantically a volatile entity, and the state of a mutex should not survive an
application restart. On application restart, a mutex object should be in the unlocked
state. The pmem::obj::v class template is targeted for this purpose. Listing 14-4
demonstrates how to use the pmem::obj::v class template with std::mutex on
persistent memory.
Listing 14-4. Example demonstrating usage of std::mutex on persistent memory
38 namespace pobj = pmem::obj;
39
40 struct root {
41
pobj::experimental::v mtx;
42 };
43
44 using pop_type = pobj::pool;
45
46 int main(int argc, char *argv[]) {
47
pop_type pop =
48
pop_type::open("/pmemfs/file", "MUTEX");
49
50
auto proot = pop.root();
51
52
proot->mtx.get().lock();
53
54
pop.close();
55
return 0;
56 }
Chapter 14 ConCurrenCy and persistent MeMory
Précédent

- 308/457

Suivant