PMDK C++ bindings
1.13.0-git107.g7e59f08f
This is the C++ bindings documentation for PMDK's libpmemobj.
|
Transactional approach to store data on pmem. More...
Classes | |
class | pmem::obj::basic_transaction |
C++ transaction handler class. More... | |
class | pmem::obj::flat_transaction |
C++ flat transaction handler class. More... | |
Transactional approach to store data on pmem.
The heart of the libpmemobj are transactions. A transaction is defined as series of operations on persistent memory objects that either all occur, or nothing occurs. In particular, if the execution of a transaction is interrupted by a power failure or a system crash, it is guaranteed that after system restart, all the changes made as a part of the uncompleted transaction will be rolled back, restoring the consistent state of the memory pool from the moment when the transaction was started.
int x = 5
or malloc()
) used within transaction will not be a part ot the transaction and won't be rolled back on failure! To properly store variables on pmem use Primitives , to allocate data on pmem see Allocation functions.In C++ bindings (this library) transactions were designed (in comparison to C API) to be as developer-friendly as possible. Even though libpmemobj++ are the bindings you should not mix these two APIs - using libpmemobj (C API) in C++ application will not work!
Let's take a look at the following snippet:
Code above is an example how automatic transaction can look like. After object creation there are a few statements executed within a transaction. Transaction will be committed during tx object's destruction at the end of the scope.
It's worth noticing that pmem::obj::flat_transaction is recommended to use over pmem::obj::basic_transaction. An extra explanation is provided inline an example in pmem::obj::flat_transaction description.
Mentioned above transactions are handled through two internal classes:
In both approaches one of the parameters is the locks
. They are held for the entire duration of the transaction and they are released at the end of the scope - so within the catch
block, they are already unlocked.
If you want to read more and see example usages of both, you have to see flat or basic transaction documentation, because each implementation may differ.
When you are using transaction API a transaction can be in one of the following states:
Moving from one stage to another is possible under some conditions, but in libpmemobj-cpp it's transparent for user, so please focus on relationships between stages. Look at the diagram below:
To be more familiar with functions used in diagram read e.g. pmemobj_tx_begin(3) manpage (C API for libpmemobj, link below in Additional resources).
If you need to read general information about transaction move to the Additional resources section.
For comparison with the previous snippet, here is a code snippet of pmem::obj::flat_transaction which is listed below with basic explanation inline.
If you read the inline comments you should be able to notice the difference between pmem::obj::flat_transaction and pmem::obj::basic_transaction. For more examples please look at the examples directory in libpmemobj-cpp repository.