|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Mutex-protected shared object wrapper. More...
#include <concurrency_utils.H>
Classes | |
| class | ConstLockedPtr |
| RAII guard for read-only access to the synchronized value. More... | |
| class | LockedPtr |
| RAII guard for exclusive access to the synchronized value. More... | |
Public Member Functions | |
| Synchronized ()=default | |
| Value-initialize the protected value: zero for a scalar. | |
| Synchronized (const Synchronized &)=delete | |
| Deleted copy constructor. | |
| Synchronized & | operator= (const Synchronized &)=delete |
| Deleted copy assignment operator. | |
| Synchronized (Synchronized &&)=delete | |
| Deleted move constructor. | |
| Synchronized & | operator= (Synchronized &&)=delete |
| Deleted move assignment operator. | |
| Synchronized (const T &value) | |
Construct from a copy of value. | |
| Synchronized (T &&value) | |
Construct by moving from value. | |
| template<typename... Args> | |
| Synchronized (std::in_place_t, Args &&... args) | |
| Construct the protected value in-place. | |
| LockedPtr | lock () |
| Acquire an exclusive lock and return a guard. | |
| ConstLockedPtr | lock () const |
| Acquire an exclusive lock (for const access) and return a guard. | |
| template<typename F > | |
| decltype(auto) | with_lock (F &&f) |
| Execute a callback with exclusive access to the value. | |
| template<typename F > | |
| decltype(auto) | with_lock (F &&f) const |
| Execute a callback with read-only access to the value. | |
Private Attributes | |
| Mutex | mutex_ |
| T | value_ = T() |
Mutex-protected shared object wrapper.
Synchronized<T> encapsulates a value of type T and a mutex. Access to the value is only possible by acquiring a lock, ensuring thread-safe concurrent access. All public member functions are thread-safe. Calls are not reentrant on the same object unless Mutex itself is recursive.
The recommended interface is with_lock() for short critical sections. When the caller needs to hold the lock across multiple operations, lock() returns a small guard (LockedPtr or ConstLockedPtr) exposing operator* and operator->.
The callback passed to with_lock() should not return references or pointers that outlive the callback itself.
| T | The type of the protected object. |
| Mutex | The mutex type (defaults to std::mutex). |
Definition at line 726 of file concurrency_utils.H.
|
default |
Value-initialize the protected value: zero for a scalar.
| Any | exception thrown by T's default constructor. |
|
delete |
Deleted copy constructor.
|
delete |
Deleted move constructor.
|
inlineexplicit |
Construct from a copy of value.
| value | Initial value to copy into the wrapper. |
| Any | exception thrown while copying T. |
Definition at line 898 of file concurrency_utils.H.
|
inlineexplicit |
Construct by moving from value.
| value | Initial value to move into the wrapper. |
| Any | exception thrown while moving T. |
Definition at line 905 of file concurrency_utils.H.
|
inlineexplicit |
Construct the protected value in-place.
| Args | Constructor argument types. |
| args | Arguments to pass to the constructor of T. |
| Any | exception thrown by T's constructor. |
Definition at line 914 of file concurrency_utils.H.
|
inline |
Acquire an exclusive lock and return a guard.
LockedPtr that keeps the mutex locked until destruction. | Any | exception thrown while locking Mutex. |
Mutex. Definition at line 923 of file concurrency_utils.H.
References Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.
Referenced by TEST(), TEST(), Aleph::Synchronized< T, Mutex >::with_lock(), and Aleph::Synchronized< T, Mutex >::with_lock().
|
inline |
Acquire an exclusive lock (for const access) and return a guard.
ConstLockedPtr that keeps the mutex locked until destruction. | Any | exception thrown while locking Mutex. |
Mutex. Definition at line 932 of file concurrency_utils.H.
References Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.
|
delete |
Deleted copy assignment operator.
|
delete |
Deleted move assignment operator.
|
inline |
Execute a callback with exclusive access to the value.
Acquires the lock, calls f with a reference to the protected value, and releases the lock when the callback returns.
| F | Callback type. |
| f | The callback to execute. |
f. | Any | exception thrown while locking Mutex or by the callback itself. |
Mutex allows recursive locking. The callback must not return a reference; otherwise compilation fails to prevent dangling references after the lock is released. Definition at line 953 of file concurrency_utils.H.
References Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.
Referenced by example_channels_and_shared_state(), main(), Moved_From_Case< Const_Locked >::make(), TEST(), TEST(), and TEST().
|
inline |
Execute a callback with read-only access to the value.
Acquires the lock, calls f with a const reference to the value, and releases the lock when the callback returns.
| F | Callback type. |
| f | The callback to execute. |
f. | Any | exception thrown while locking Mutex or by the callback itself. |
Mutex allows recursive locking. The callback must not return a reference; otherwise compilation fails to prevent dangling references after the lock is released. Definition at line 978 of file concurrency_utils.H.
References Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.
|
mutableprivate |
Definition at line 876 of file concurrency_utils.H.
Referenced by Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::with_lock(), and Aleph::Synchronized< T, Mutex >::with_lock().
|
private |
Definition at line 877 of file concurrency_utils.H.
Referenced by Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::with_lock(), and Aleph::Synchronized< T, Mutex >::with_lock().