Samchon Framework for CPP
1.0.0
|
Shared lock from a RWMutex. More...
#include <SharedReadLock.hpp>
Public Member Functions | |
SharedReadLock (const RWMutex &, bool=true) | |
Construct from RWMutex. More... | |
SharedReadLock (const SharedReadLock &) | |
Copy Constructor. More... | |
SharedReadLock (SharedReadLock &&) | |
Move Constructor. More... | |
~SharedReadLock () | |
Default Destructor. More... | |
void | lock () const |
Lock on read. More... | |
void | unlock () const |
Unlock of read. More... | |
Private Attributes | |
const RWMutex * | semaphore |
Managed RWMutex. More... | |
std::atomic< size_t > * | reference |
Referencing count sharing same RWMutex. More... | |
std::atomic< bool > * | isLocked |
Whether the mutex was locked by SharedReadLock. More... | |
Shared lock from a RWMutex.
A SharedReadLock is an object manages a RWMutex with shared ownership in both states.
On default construction, the object acquires a RWMutex object, for whose locking and unlocking operations becomes responsible. When copy constructions, responsibilities of locking and unlocking RWMutex are shared with copied with those SharedReadLock objects.
The class shared_lock is a general-purpose shared mutex ownership wrapper allowing deferred locking, timed locking and transfer of lock ownership. Locking a shared_lock locks the associated shared mutex in shared mode (to lock it in exclusive mode, std::unique_lock can be used)
This class guarantees a unlocked status on destruction of all shared objects (even if not called explicitly). Therefore it is especially useful as an object with automatic duration, as it guarantees the RWMutex object is properly unlocked in case an exception is thrown.
Referenced comments of std::unique_lock
Though, that the SharedReadLock object does not manage the lifetime of the RWMutex object in any way: the duration of the RWMutex object shall extend at least until the destruction of the SharedReadLock that manages it.
Definition at line 50 of file SharedReadLock.hpp.
SharedReadLock::SharedReadLock | ( | const RWMutex & | semaphore, |
bool | doLock = true |
||
) |
Construct from RWMutex.
semaphore | RWMutex to manage |
doLock | Whether to lock directly or not |
Definition at line 12 of file SharedReadLock.cpp.
References samchon::library::RWMutex::readLock().
SharedReadLock::SharedReadLock | ( | const SharedReadLock & | obj | ) |
Copy Constructor.
Definition at line 22 of file SharedReadLock.cpp.
SharedReadLock::SharedReadLock | ( | SharedReadLock && | obj | ) |
Move Constructor.
Definition at line 30 of file SharedReadLock.cpp.
SharedReadLock::~SharedReadLock | ( | ) |
Default Destructor.
Definition at line 43 of file SharedReadLock.cpp.
void SharedReadLock::lock | ( | ) | const |
Lock on read.
Increases a reading count.
When write_lock is on a progress, wait until write_unlock to be called.
Definition at line 58 of file SharedReadLock.cpp.
void SharedReadLock::unlock | ( | ) | const |
Unlock of read.
Decreases a reading count.
When write_lock had done after read_lock, it continues by read_unlock if the reading count was 1 (read_unlock makes the count to be zero).
Definition at line 66 of file SharedReadLock.cpp.
|
private |
|
private |
Referencing count sharing same RWMutex.
Definition at line 61 of file SharedReadLock.hpp.
Referenced by SharedReadLock().
|
private |
Whether the mutex was locked by SharedReadLock.
Definition at line 66 of file SharedReadLock.hpp.
Referenced by SharedReadLock().