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

Read/write-lock protected shared object wrapper. More...

#include <concurrency_utils.H>

Collaboration diagram for Aleph::RwSynchronized< T, SharedMutex >:
[legend]

Classes

class  ReadLockedPtr
 RAII guard for shared (read-only) access. More...
 
class  WriteLockedPtr
 RAII guard for exclusive (write) access. More...
 

Public Member Functions

 RwSynchronized ()=default
 Default-construct the protected value.
 
 RwSynchronized (const RwSynchronized &)=delete
 Deleted copy constructor.
 
RwSynchronizedoperator= (const RwSynchronized &)=delete
 Deleted copy assignment operator.
 
 RwSynchronized (RwSynchronized &&)=delete
 Deleted move constructor.
 
RwSynchronizedoperator= (RwSynchronized &&)=delete
 Deleted move assignment operator.
 
 RwSynchronized (const T &value)
 Construct from a copy of value.
 
 RwSynchronized (T &&value)
 Construct by moving from value.
 
template<typename... Args>
 RwSynchronized (std::in_place_t, Args &&... args)
 Construct the protected value in-place.
 
ReadLockedPtr read () const
 Acquire a shared lock and return a guard.
 
WriteLockedPtr write ()
 Acquire an exclusive lock and return a guard.
 
template<typename F >
decltype(autowith_read_lock (F &&f) const
 Execute a callback with shared (read-only) access.
 
template<typename F >
decltype(autowith_write_lock (F &&f)
 Execute a callback with exclusive (write) access.
 

Private Attributes

SharedMutex mutex_
 
T value_
 

Detailed Description

template<typename T, typename SharedMutex = std::shared_mutex>
class Aleph::RwSynchronized< T, SharedMutex >

Read/write-lock protected shared object wrapper.

RwSynchronized<T> encapsulates a value and a shared mutex, allowing multiple concurrent readers OR a single exclusive writer. All public member functions are thread-safe. Calls are not reentrant on the same object unless SharedMutex itself supports recursive acquisition.

Readers acquire a shared lock through read() / with_read_lock(). Writers acquire an exclusive lock through write() / with_write_lock().

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

Definition at line 905 of file concurrency_utils.H.

Constructor & Destructor Documentation

◆ RwSynchronized() [1/6]

template<typename T , typename SharedMutex = std::shared_mutex>
Aleph::RwSynchronized< T, SharedMutex >::RwSynchronized ( )
default

Default-construct the protected value.

Exceptions
Anyexception thrown by T's default constructor.

◆ RwSynchronized() [2/6]

template<typename T , typename SharedMutex = std::shared_mutex>
Aleph::RwSynchronized< T, SharedMutex >::RwSynchronized ( const RwSynchronized< T, SharedMutex > &  )
delete

Deleted copy constructor.

◆ RwSynchronized() [3/6]

template<typename T , typename SharedMutex = std::shared_mutex>
Aleph::RwSynchronized< T, SharedMutex >::RwSynchronized ( RwSynchronized< T, SharedMutex > &&  )
delete

Deleted move constructor.

◆ RwSynchronized() [4/6]

template<typename T , typename SharedMutex = std::shared_mutex>
Aleph::RwSynchronized< T, SharedMutex >::RwSynchronized ( 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 999 of file concurrency_utils.H.

◆ RwSynchronized() [5/6]

template<typename T , typename SharedMutex = std::shared_mutex>
Aleph::RwSynchronized< T, SharedMutex >::RwSynchronized ( 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 1006 of file concurrency_utils.H.

◆ RwSynchronized() [6/6]

template<typename T , typename SharedMutex = std::shared_mutex>
template<typename... Args>
Aleph::RwSynchronized< T, SharedMutex >::RwSynchronized ( 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.

Definition at line 1014 of file concurrency_utils.H.

Member Function Documentation

◆ operator=() [1/2]

Deleted copy assignment operator.

◆ operator=() [2/2]

Deleted move assignment operator.

◆ read()

template<typename T , typename SharedMutex = std::shared_mutex>
ReadLockedPtr Aleph::RwSynchronized< T, SharedMutex >::read ( ) const
inline

Acquire a shared lock and return a guard.

Returns
A ReadLockedPtr that keeps a shared lock until destruction.
Exceptions
Anyexception thrown while locking SharedMutex.
Note
Thread-safe. This call may block until there is no active writer.

Definition at line 1022 of file concurrency_utils.H.

References Aleph::RwSynchronized< T, SharedMutex >::mutex_, and Aleph::RwSynchronized< T, SharedMutex >::value_.

Referenced by TEST().

◆ with_read_lock()

template<typename T , typename SharedMutex = std::shared_mutex>
template<typename F >
decltype(auto) Aleph::RwSynchronized< T, SharedMutex >::with_read_lock ( F &&  f) const
inline

Execute a callback with shared (read-only) access.

Template Parameters
FCallback type.
Parameters
fThe callback to execute.
Returns
The value returned by f.
Exceptions
Anyexception thrown while locking SharedMutex or by the callback itself.
Note
Thread-safe and may block waiting for active writers. The callback must not return a reference; otherwise compilation fails to prevent dangling references after the lock is released.

Definition at line 1051 of file concurrency_utils.H.

References Aleph::divide_and_conquer_partition_dp(), Aleph::RwSynchronized< T, SharedMutex >::mutex_, and Aleph::RwSynchronized< T, SharedMutex >::value_.

Referenced by TEST(), and TEST().

◆ with_write_lock()

template<typename T , typename SharedMutex = std::shared_mutex>
template<typename F >
decltype(auto) Aleph::RwSynchronized< T, SharedMutex >::with_write_lock ( F &&  f)
inline

Execute a callback with exclusive (write) access.

Template Parameters
FCallback type.
Parameters
fThe callback to execute.
Returns
The value returned by f.
Exceptions
Anyexception thrown while locking SharedMutex or by the callback itself.
Note
Thread-safe and may block waiting for other readers or writers. The callback must not return a reference; otherwise compilation fails to prevent dangling references after the lock is released.

Definition at line 1072 of file concurrency_utils.H.

References Aleph::divide_and_conquer_partition_dp(), Aleph::RwSynchronized< T, SharedMutex >::mutex_, and Aleph::RwSynchronized< T, SharedMutex >::value_.

Referenced by TEST(), and TEST().

◆ write()

template<typename T , typename SharedMutex = std::shared_mutex>
WriteLockedPtr Aleph::RwSynchronized< T, SharedMutex >::write ( )
inline

Acquire an exclusive lock and return a guard.

Returns
A WriteLockedPtr that keeps an exclusive lock until destruction.
Exceptions
Anyexception thrown while locking SharedMutex.
Note
Thread-safe. This call may block until all readers and writers have released the mutex.

Definition at line 1034 of file concurrency_utils.H.

References Aleph::RwSynchronized< T, SharedMutex >::mutex_, and Aleph::RwSynchronized< T, SharedMutex >::value_.

Referenced by TEST().

Member Data Documentation

◆ mutex_

◆ value_


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