C++ transaction handler class.
This class is the pmemobj transaction handler. Scoped transactions are handled through two internal classes: manual and automatic.
- manual transactions need to be committed manually, otherwise they will be aborted on object destruction.
- automatic transactions are only available in C++17. They handle transaction commit/abort automatically.
This class also exposes a closure-like transaction API, which is the preferred way of handling transactions.
The typical usage example would be:
using namespace pmem::obj;
void
general_tx_example()
{
struct root {
mutex pmutex;
shared_mutex shared_pmutex;
p<int> count;
persistent_ptr<root> another_root;
};
auto proot = pop.get_root();
try {
[&]() {
proot->another_root =
make_persistent<root>();
proot->count++;
},
proot->pmutex, proot->shared_pmutex);
} catch (...) {
}
}
static pool< T > create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:519
static void exec_tx(pool_base &pool, std::function< void()> tx, Locks &... locks)
Execute a closure-like transaction and lock locks.
Definition: transaction.hpp:393
Custom transaction error class.
Definition: pexceptions.hpp:63
Persistent_ptr transactional allocation functions for objects.
Persistent smart pointer.
Convenience extensions for the resides on pmem property template.
Pmem-resident shared mutex.
C++ pmemobj transactions.
template<typename... Locks>
static void pmem::obj::transaction::exec_tx |
( |
pool_base & |
pool, |
|
|
std::function< void()> |
tx, |
|
|
Locks &... |
locks |
|
) |
| |
|
inlinestatic |
Execute a closure-like transaction and lock locks
.
The locks have to be persistent memory resident locks. An attempt to lock the locks will be made. If any of the specified locks is already locked, the method will block. The locks are held until the end of the transaction. The transaction does not have to be committed manually. Manual aborts will end the transaction with an active exception.
If an exception is thrown within the transaction, it gets aborted and the exception is rethrown. Therefore extra care has to be taken with proper error handling.
The locks are held for the entire duration of the transaction. They are released at the end of the scope, so within the catch
block, they are already unlocked. If the cleanup action requires access to data within a critical section, the locks have to be manually acquired once again.
- Parameters
-
[in,out] | pool | the pool in which the transaction will take place. |
[in] | tx | an std::function<void ()> which will perform operations within this transaction. |
[in,out] | locks | locks to be taken for the duration of the transaction. |
- Exceptions
-