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

RAII guard for exclusive access to the synchronized value. More...

#include <concurrency_utils.H>

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

Public Member Functions

 LockedPtr (Mutex &mutex, T &value)
 Construct a guard and acquire the lock.
 
 LockedPtr (LockedPtr &&other) noexcept
 Take the lock and the access of another guard, which is left with neither.
 
LockedPtr & operator= (LockedPtr &&other) noexcept
 Release the lock of this guard and take the lock and the access of another, which is left with neither.
 
T & get () noexcept
 Access the protected value.
 
T * operator-> () noexcept
 Access members of the protected value.
 
T & operator* () noexcept
 Access the protected value.
 

Private Attributes

std::unique_lock< Mutex > lock_
 
T * ptr_
 

Detailed Description

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

RAII guard for exclusive access to the synchronized value.

Note
Thread-safe to use only while the guard object remains alive on the thread that acquired it. The destructor releases the lock.
A moved-from guard holds no lock and gives no access: its operator->() returns nullptr, and get() and operator*() must not be called on it (asserted in Debug builds).

Definition at line 736 of file concurrency_utils.H.

Constructor & Destructor Documentation

◆ LockedPtr() [1/2]

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

Construct a guard and acquire the lock.

Parameters
mutexMutex protecting the value.
valueProtected object to expose through the guard.
Exceptions
Anyexception thrown while locking mutex.

Definition at line 747 of file concurrency_utils.H.

◆ LockedPtr() [2/2]

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

Take the lock and the access of another guard, which is left with neither.

The implicit move took the lock but copied the pointer: the moved-from guard still gave the value, with no lock held.

Parameters
[in,out]otherguard to move from.

Definition at line 758 of file concurrency_utils.H.

Member Function Documentation

◆ get()

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

Access the protected value.

Returns
A mutable reference to the protected object.
Exceptions
Nothing.
Precondition
The guard was not moved from.

Definition at line 779 of file concurrency_utils.H.

References Aleph::Synchronized< T, Mutex >::LockedPtr::ptr_.

◆ operator*()

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

Access the protected value.

Returns
A mutable reference to the protected object.
Exceptions
Nothing.
Precondition
The guard was not moved from.

Definition at line 795 of file concurrency_utils.H.

References Aleph::Synchronized< T, Mutex >::LockedPtr::ptr_.

◆ operator->()

template<typename T , typename Mutex = std::mutex>
T * Aleph::Synchronized< T, Mutex >::LockedPtr::operator-> ( )
inlinenoexcept

Access members of the protected value.

Returns
A mutable pointer to the protected object, or nullptr if the guard was moved from.
Exceptions
Nothing.

Definition at line 789 of file concurrency_utils.H.

References Aleph::Synchronized< T, Mutex >::LockedPtr::ptr_.

◆ operator=()

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

Release the lock of this guard and take the lock and the access of another, which is left with neither.

Parameters
[in,out]otherguard to move from.
Returns
this guard.

Definition at line 766 of file concurrency_utils.H.

References Aleph::Synchronized< T, Mutex >::LockedPtr::lock_, and Aleph::Synchronized< T, Mutex >::LockedPtr::ptr_.

Member Data Documentation

◆ lock_

template<typename T , typename Mutex = std::mutex>
std::unique_lock<Mutex> Aleph::Synchronized< T, Mutex >::LockedPtr::lock_
private

◆ ptr_


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