278
Transactions and Multithreading
In computer science, ACID (atomicity, consistency, isolation, and durability) is a set of
properties of transactions intended to guarantee data validity and consistency in case
of errors, power failures, and abnormal termination of a process. Chapter 7 introduced
PMDK transactions and their ACID properties. This chapter focuses on the relevancy
of multithreaded programs for persistent memory. Looking forward, Chapter 16 will
provide some insights into the internals of libpmemobj transactions.
The small program in Listing 14-1 shows that the counter stored within the root
object is incremented concurrently by multiple threads. The program opens the
persistent memory pool and prints the value of counter. It then runs ten threads, each
of which calls the increment() function. Once all the threads complete successfully, the
program prints the final value of counter.
Listing 14-1. Example to demonstrate that PMDK transactions do not
automatically support isolation
41 using namespace std;
42 namespace pobj = pmem::obj;
43
44 struct root {
45
pobj::p counter;
46 };
47
48 using pop_type = pobj::pool;
49
50 void increment(pop_type &pop) {
51
auto proot = pop.root();
52
pobj::transaction::run(pop, [&] {
53
proot->counter.get_rw() += 1;
54
});
55 }
56
57 int main(int argc, char *argv[]) {
58
pop_type pop =
59
pop_type::open("/pmemfs/file", "COUNTER_INC");
60
Chapter 14 ConCurrenCy and persistent MeMory
Transactions and Multithreading
In computer science, ACID (atomicity, consistency, isolation, and durability) is a set of
properties of transactions intended to guarantee data validity and consistency in case
of errors, power failures, and abnormal termination of a process. Chapter 7 introduced
PMDK transactions and their ACID properties. This chapter focuses on the relevancy
of multithreaded programs for persistent memory. Looking forward, Chapter 16 will
provide some insights into the internals of libpmemobj transactions.
The small program in Listing 14-1 shows that the counter stored within the root
object is incremented concurrently by multiple threads. The program opens the
persistent memory pool and prints the value of counter. It then runs ten threads, each
of which calls the increment() function. Once all the threads complete successfully, the
program prints the final value of counter.
Listing 14-1. Example to demonstrate that PMDK transactions do not
automatically support isolation
41 using namespace std;
42 namespace pobj = pmem::obj;
43
44 struct root {
45
pobj::p
46 };
47
48 using pop_type = pobj::pool
49
50 void increment(pop_type &pop) {
51
auto proot = pop.root();
52
pobj::transaction::run(pop, [&] {
53
proot->counter.get_rw() += 1;
54
});
55 }
56
57 int main(int argc, char *argv[]) {
58
pop_type pop =
59
pop_type::open("/pmemfs/file", "COUNTER_INC");
60
Chapter 14 ConCurrenCy and persistent MeMory
