Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Synchronized< T, Mutex > Class Template Reference

Mutex-protected shared object wrapper. More...

#include <concurrency_utils.H>

Collaboration diagram for Aleph::Synchronized< T, Mutex >:
[legend]

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
 Default-construct the protected value.
 
 Synchronized (const Synchronized &)=delete
 Deleted copy constructor.
 
Synchronizedoperator= (const Synchronized &)=delete
 Deleted copy assignment operator.
 
 Synchronized (Synchronized &&)=delete
 Deleted move constructor.
 
Synchronizedoperator= (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(autowith_lock (F &&f)
 Execute a callback with exclusive access to the value.
 
template<typename F >
decltype(autowith_lock (F &&f) const
 Execute a callback with read-only access to the value.
 

Private Attributes

Mutex mutex_
 
T value_
 

Detailed Description

template<typename T, typename Mutex = std::mutex>
class Aleph::Synchronized< T, Mutex >

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.

Template Parameters
TThe type of the protected object.
MutexThe mutex type (defaults to std::mutex).

Definition at line 705 of file concurrency_utils.H.

Constructor & Destructor Documentation

◆ Synchronized() [1/6]

template<typename T , typename Mutex = std::mutex>
Aleph::Synchronized< T, Mutex >::Synchronized ( )
default

Default-construct the protected value.

Exceptions
Anyexception thrown by T's default constructor.

◆ Synchronized() [2/6]

template<typename T , typename Mutex = std::mutex>
Aleph::Synchronized< T, Mutex >::Synchronized ( const Synchronized< T, Mutex > &  )
delete

Deleted copy constructor.

◆ Synchronized() [3/6]

template<typename T , typename Mutex = std::mutex>
Aleph::Synchronized< T, Mutex >::Synchronized ( Synchronized< T, Mutex > &&  )
delete

Deleted move constructor.

◆ Synchronized() [4/6]

template<typename T , typename Mutex = std::mutex>
Aleph::Synchronized< T, Mutex >::Synchronized ( const T value)
inlineexplicit

Construct from a copy of value.

Parameters
valueInitial value to copy into the wrapper.
Exceptions
Anyexception thrown while copying T.

Definition at line 801 of file concurrency_utils.H.

◆ Synchronized() [5/6]

template<typename T , typename Mutex = std::mutex>
Aleph::Synchronized< T, Mutex >::Synchronized ( T &&  value)
inlineexplicit

Construct by moving from value.

Parameters
valueInitial value to move into the wrapper.
Exceptions
Anyexception thrown while moving T.

Definition at line 808 of file concurrency_utils.H.

◆ Synchronized() [6/6]

template<typename T , typename Mutex = std::mutex>
template<typename... Args>
Aleph::Synchronized< T, Mutex >::Synchronized ( std::in_place_t  ,
Args &&...  args 
)
inlineexplicit

Construct the protected value in-place.

Template Parameters
ArgsConstructor argument types.
Parameters
argsArguments to pass to the constructor of T.
Exceptions
Anyexception thrown by T's constructor.

Definition at line 817 of file concurrency_utils.H.

Member Function Documentation

◆ lock() [1/2]

template<typename T , typename Mutex = std::mutex>
LockedPtr Aleph::Synchronized< T, Mutex >::lock ( )
inline

Acquire an exclusive lock and return a guard.

Returns
A LockedPtr that keeps the mutex locked until destruction.
Exceptions
Anyexception thrown while locking Mutex.
Note
Thread-safe. This call may block until another holder releases the lock. Reentrancy depends on Mutex.

Definition at line 826 of file concurrency_utils.H.

References Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.

Referenced by TEST(), Aleph::Synchronized< T, Mutex >::with_lock(), and Aleph::Synchronized< T, Mutex >::with_lock().

◆ lock() [2/2]

template<typename T , typename Mutex = std::mutex>
ConstLockedPtr Aleph::Synchronized< T, Mutex >::lock ( ) const
inline

Acquire an exclusive lock (for const access) and return a guard.

Returns
A ConstLockedPtr that keeps the mutex locked until destruction.
Exceptions
Anyexception thrown while locking Mutex.
Note
Thread-safe. This call may block until another holder releases the lock. Reentrancy depends on Mutex.

Definition at line 835 of file concurrency_utils.H.

References Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.

◆ operator=() [1/2]

template<typename T , typename Mutex = std::mutex>
Synchronized & Aleph::Synchronized< T, Mutex >::operator= ( const Synchronized< T, Mutex > &  )
delete

Deleted copy assignment operator.

◆ operator=() [2/2]

template<typename T , typename Mutex = std::mutex>
Synchronized & Aleph::Synchronized< T, Mutex >::operator= ( Synchronized< T, Mutex > &&  )
delete

Deleted move assignment operator.

◆ with_lock() [1/2]

template<typename T , typename Mutex = std::mutex>
template<typename F >
decltype(auto) Aleph::Synchronized< T, Mutex >::with_lock ( F &&  f)
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.

Template Parameters
FCallback type.
Parameters
fThe callback to execute.
Returns
The value returned by f.
Exceptions
Anyexception thrown while locking Mutex or by the callback itself.
Note
Thread-safe and may block waiting for the mutex. Not reentrant on the same object unless 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 856 of file concurrency_utils.H.

References Aleph::divide_and_conquer_partition_dp(), Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.

Referenced by TEST(), and TEST().

◆ with_lock() [2/2]

template<typename T , typename Mutex = std::mutex>
template<typename F >
decltype(auto) Aleph::Synchronized< T, Mutex >::with_lock ( F &&  f) const
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.

Template Parameters
FCallback type.
Parameters
fThe callback to execute.
Returns
The value returned by f.
Exceptions
Anyexception thrown while locking Mutex or by the callback itself.
Note
Thread-safe and may block waiting for the mutex. Not reentrant on the same object unless 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 881 of file concurrency_utils.H.

References Aleph::divide_and_conquer_partition_dp(), Aleph::Synchronized< T, Mutex >::lock(), Aleph::Synchronized< T, Mutex >::mutex_, and Aleph::Synchronized< T, Mutex >::value_.

Member Data Documentation

◆ mutex_

◆ value_


The documentation for this class was generated from the following file: