100
105
pmemobj_set_value(pool, &act[0],
106
&D_RW(account_a)->balance,
107
D_RW(account_a)->balance – price);
108
pmemobj_set_value(pool, &act[1],
109
&D_RW(account_b)->balance,
110
D_RW(account_b)->balance + price);
111
pmemobj_publish(pool, act, 2);
112
113
pmemobj_close(pool);
114
return 0;
115 }
• Line 44: Defines the location of the memory pool.
• Lines 48-52: Declares an account data structure with a name and
balance.
• Lines 60-89: The new_account() function reserves the memory (lines
66 and 78), updates the name and balance (lines 83 and 84), persists
the changes (line 85), and then publishes the updates (line 87).
• Lines 93-95: Create a new pool or exit on failure.
• Line 97: Declare two account instances.
• Lines 98-101: Create a new account for each owner with initial
balances.
• Lines 103-111: We subtract 42 from Julius Caesar’s account and add
42 to Mark Anthony’s account. The modifications are published on
line 111.
Transactional API
The reserve/publish API is fast, but it does not allow reading data you have just written.
In such cases, you can use the transactional API.
The first time a variable is written, it must be explicitly added to the transaction. This
can be done via pmemobj_tx_add_range() or its variants (xadd, _direct). Convenient
macros such as TX_ADD() or TX_SET() can perform the same operation. The transactionbased functions and macros provided by libpmemobj include
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
105
pmemobj_set_value(pool, &act[0],
106
&D_RW(account_a)->balance,
107
D_RW(account_a)->balance – price);
108
pmemobj_set_value(pool, &act[1],
109
&D_RW(account_b)->balance,
110
D_RW(account_b)->balance + price);
111
pmemobj_publish(pool, act, 2);
112
113
pmemobj_close(pool);
114
return 0;
115 }
• Line 44: Defines the location of the memory pool.
• Lines 48-52: Declares an account data structure with a name and
balance.
• Lines 60-89: The new_account() function reserves the memory (lines
66 and 78), updates the name and balance (lines 83 and 84), persists
the changes (line 85), and then publishes the updates (line 87).
• Lines 93-95: Create a new pool or exit on failure.
• Line 97: Declare two account instances.
• Lines 98-101: Create a new account for each owner with initial
balances.
• Lines 103-111: We subtract 42 from Julius Caesar’s account and add
42 to Mark Anthony’s account. The modifications are published on
line 111.
Transactional API
The reserve/publish API is fast, but it does not allow reading data you have just written.
In such cases, you can use the transactional API.
The first time a variable is written, it must be explicitly added to the transaction. This
can be done via pmemobj_tx_add_range() or its variants (xadd, _direct). Convenient
macros such as TX_ADD() or TX_SET() can perform the same operation. The transactionbased functions and macros provided by libpmemobj include
Chapter 7 libpmemobj: a Native traNsaCtioNal objeCt store
