Samchon Framework for CPP  1.0.0
samchon::library::SharedWriteLock Class Reference

Shared lock from a RWMutex. More...

#include <SharedWriteLock.hpp>

Collaboration diagram for samchon::library::SharedWriteLock:

Public Member Functions

 SharedWriteLock (RWMutex &, bool=true)
 Construct from RWMutex. More...
 
 SharedWriteLock (const SharedWriteLock &)
 Copy Constructor. More...
 
 SharedWriteLock (SharedWriteLock &&)
 Move Constructor. More...
 
 ~SharedWriteLock ()
 Default Destructor. More...
 
void lock ()
 Lock on writing. More...
 
void unlock ()
 Unlock on writing. More...
 

Private Attributes

RWMutexsemaphore
 Managed RWMutex. More...
 
std::atomic< size_t > * reference
 Referencing count sharing same RWMutex. More...
 
std::atomic< bool > * isLocked
 Whether the mutex was locked by SharedWriteLock. More...
 

Detailed Description

Shared lock from a RWMutex.

A SharedWriteLock is an object manages a RWMutex with shared ownership in both states.

  • locked
  • unlocked

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 SharedWriteLock 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

library_critical_section.png
Note

Though, that the SharedWriteLock 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 SharedWriteLock that manages it.

See also
library::UniqueAcquire
samchon::library
Author
Jeongho Nam http://samchon.org

Definition at line 50 of file SharedWriteLock.hpp.

Constructor & Destructor Documentation

SharedWriteLock::SharedWriteLock ( RWMutex semaphore,
bool  doLock = true 
)

Construct from RWMutex.

Parameters
semaphoreRWMutex to manage
doLockWhether to lock directly or not

Definition at line 12 of file SharedWriteLock.cpp.

References samchon::library::RWMutex::writeLock().

Here is the call graph for this function:

SharedWriteLock::SharedWriteLock ( const SharedWriteLock obj)

Copy Constructor.

Definition at line 22 of file SharedWriteLock.cpp.

References isLocked, reference, and semaphore.

SharedWriteLock::SharedWriteLock ( SharedWriteLock &&  obj)

Move Constructor.

Definition at line 30 of file SharedWriteLock.cpp.

SharedWriteLock::~SharedWriteLock ( )

Default Destructor.

Definition at line 43 of file SharedWriteLock.cpp.

Member Function Documentation

void SharedWriteLock::lock ( )

Lock on writing.

Changes writing flag to true.

If another write_lock or read_lock is on a progress, wait until them to be unlocked.

  • Writing can be done by only a section at once.
  • Writing can't be done when reading.
Note
You've to call write_unlock when writing work was terminated.

Definition at line 58 of file SharedWriteLock.cpp.

void SharedWriteLock::unlock ( )

Unlock on writing.

Definition at line 66 of file SharedWriteLock.cpp.

Member Data Documentation

RWMutex* samchon::library::SharedWriteLock::semaphore
private

Managed RWMutex.

Definition at line 56 of file SharedWriteLock.hpp.

Referenced by SharedWriteLock().

std::atomic<size_t>* samchon::library::SharedWriteLock::reference
private

Referencing count sharing same RWMutex.

Definition at line 61 of file SharedWriteLock.hpp.

Referenced by SharedWriteLock().

std::atomic<bool>* samchon::library::SharedWriteLock::isLocked
private

Whether the mutex was locked by SharedWriteLock.

Definition at line 66 of file SharedWriteLock.hpp.

Referenced by SharedWriteLock().


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