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

Shared lock from a RWMutex. More...

#include <SharedReadLock.hpp>

Collaboration diagram for samchon::library::SharedReadLock:

Public Member Functions

 SharedReadLock (const RWMutex &rw_mutex, bool lock=true)
 Construct from RWMutex. More...
 
 SharedReadLock (const SharedReadLock &obj)
 Copy Constructor. More...
 
 SharedReadLock (SharedReadLock &&obj)
 Move Constructor. More...
 
 ~SharedReadLock ()
 Default Destructor. More...
 
void lock () const
 Lock on read. More...
 
void unlock () const
 Unlock of read. More...
 

Private Attributes

const RWMutexrw_mutex
 Managed RWMutex. More...
 
std::atomic< size_t > * reference
 Referencing count sharing same RWMutex. More...
 
std::atomic< bool > * locked
 Whether the mutex was locked by SharedReadLock. More...
 

Detailed Description

Shared lock from a RWMutex.

A SharedReadLock 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 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

Class Diagram
Note

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.

Library - Critical Section

Author
Jeongho Nam http://samchon.org

Definition at line 43 of file SharedReadLock.hpp.

Constructor & Destructor Documentation

samchon::library::SharedReadLock::SharedReadLock ( const RWMutex rw_mutex,
bool  lock = true 
)
inline

Construct from RWMutex.

Parameters
rw_mutexRWMutex to manage
lockWhether to lock directly or not

Definition at line 71 of file SharedReadLock.hpp.

References lock(), samchon::library::RWMutex::readLock(), and rw_mutex.

Here is the call graph for this function:

samchon::library::SharedReadLock::SharedReadLock ( const SharedReadLock obj)
inline

Copy Constructor.

Definition at line 84 of file SharedReadLock.hpp.

References locked, reference, and rw_mutex.

samchon::library::SharedReadLock::SharedReadLock ( SharedReadLock &&  obj)
inline

Move Constructor.

Definition at line 96 of file SharedReadLock.hpp.

samchon::library::SharedReadLock::~SharedReadLock ( )
inline

Default Destructor.

Definition at line 112 of file SharedReadLock.hpp.

References locked, samchon::library::RWMutex::readUnlock(), and reference.

Here is the call graph for this function:

Member Function Documentation

void samchon::library::SharedReadLock::lock ( ) const
inline

Lock on read.

Increases a reading count.

When write_lock is on a progress, wait until write_unlock to be called.

  • Reading can be done by multiple sections.
  • Reading can't be done when writing.
Warning
You've to call read_unlock when the reading work is terminated.

Definition at line 130 of file SharedReadLock.hpp.

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

Referenced by SharedReadLock().

Here is the call graph for this function:

Here is the caller graph for this function:

void samchon::library::SharedReadLock::unlock ( ) const
inline

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 142 of file SharedReadLock.hpp.

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

Here is the call graph for this function:

Member Data Documentation

const RWMutex* samchon::library::SharedReadLock::rw_mutex
private

Managed RWMutex.

Definition at line 49 of file SharedReadLock.hpp.

Referenced by SharedReadLock().

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

Referencing count sharing same RWMutex.

Definition at line 54 of file SharedReadLock.hpp.

Referenced by SharedReadLock(), and ~SharedReadLock().

std::atomic<bool>* samchon::library::SharedReadLock::locked
private

Whether the mutex was locked by SharedReadLock.

Definition at line 59 of file SharedReadLock.hpp.

Referenced by SharedReadLock(), and ~SharedReadLock().


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