CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | List of all members
CppCommon::RWLock Class Reference

Read/Write lock synchronization primitive. More...

#include <rw_lock.h>

Public Member Functions

 RWLock ()
 
 RWLock (const RWLock &)=delete
 
 RWLock (RWLock &&lock)=delete
 
 ~RWLock ()
 
RWLockoperator= (const RWLock &)=delete
 
RWLockoperator= (RWLock &&lock)=delete
 
bool TryLockRead ()
 Try to acquire read lock without block. More...
 
bool TryLockWrite ()
 Try to acquire write lock without block. More...
 
bool TryLockReadFor (const Timespan &timespan)
 Try to acquire read lock for the given timespan. More...
 
bool TryLockWriteFor (const Timespan &timespan)
 Try to acquire write lock for the given timespan. More...
 
bool TryLockReadUntil (const UtcTimestamp &timestamp)
 Try to acquire read lock until the given timestamp. More...
 
bool TryLockWriteUntil (const UtcTimestamp &timestamp)
 Try to acquire write lock until the given timestamp. More...
 
void LockRead ()
 Acquire read lock with block. More...
 
void LockWrite ()
 Acquire write lock with block. More...
 
void UnlockRead ()
 Release read lock. More...
 
void UnlockWrite ()
 Release write lock. More...
 

Detailed Description

Read/Write lock synchronization primitive.

An read/write lock allows concurrent access for read-only operations, while write operations require exclusive access. This means that multiple threads can read the data in parallel but an exclusive lock is needed for writing or modifying data. When a writer is writing the data, all other writers or readers will be blocked until the writer is finished writing.

Thread-safe.

https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock

Examples
threads_rw_lock.cpp.

Definition at line 30 of file rw_lock.h.

Constructor & Destructor Documentation

◆ RWLock() [1/3]

CppCommon::RWLock::RWLock ( )

Definition at line 129 of file rw_lock.cpp.

◆ RWLock() [2/3]

CppCommon::RWLock::RWLock ( const RWLock )
delete

◆ RWLock() [3/3]

CppCommon::RWLock::RWLock ( RWLock &&  lock)
delete

◆ ~RWLock()

CppCommon::RWLock::~RWLock ( )

Definition at line 140 of file rw_lock.cpp.

Member Function Documentation

◆ LockRead()

void CppCommon::RWLock::LockRead ( )

Acquire read lock with block.

Will block.

Definition at line 197 of file rw_lock.cpp.

◆ LockWrite()

void CppCommon::RWLock::LockWrite ( )

Acquire write lock with block.

Will block.

Definition at line 198 of file rw_lock.cpp.

◆ operator=() [1/2]

RWLock& CppCommon::RWLock::operator= ( const RWLock )
delete

◆ operator=() [2/2]

RWLock& CppCommon::RWLock::operator= ( RWLock &&  lock)
delete

◆ TryLockRead()

bool CppCommon::RWLock::TryLockRead ( )

Try to acquire read lock without block.

Will not block.

Returns
'true' if the read lock was successfully acquired, 'false' if the read lock is busy

Definition at line 146 of file rw_lock.cpp.

◆ TryLockReadFor()

bool CppCommon::RWLock::TryLockReadFor ( const Timespan timespan)

Try to acquire read lock for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timespan to wait for the read lock
Returns
'true' if the read lock was successfully acquired, 'false' if the read lock is busy

Definition at line 149 of file rw_lock.cpp.

◆ TryLockReadUntil()

bool CppCommon::RWLock::TryLockReadUntil ( const UtcTimestamp timestamp)
inline

Try to acquire read lock until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the read lock
Returns
'true' if the read lock was successfully acquired, 'false' if the read lock is busy

Definition at line 79 of file rw_lock.h.

◆ TryLockWrite()

bool CppCommon::RWLock::TryLockWrite ( )

Try to acquire write lock without block.

Will not block.

Returns
'true' if the write lock was successfully acquired, 'false' if the write lock is busy

Definition at line 147 of file rw_lock.cpp.

◆ TryLockWriteFor()

bool CppCommon::RWLock::TryLockWriteFor ( const Timespan timespan)

Try to acquire write lock for the given timespan.

Will block for the given timespan in the worst case.

Parameters
timespan- Timespan to wait for the write lock
Returns
'true' if the write lock was successfully acquired, 'false' if the write lock is busy

Definition at line 173 of file rw_lock.cpp.

◆ TryLockWriteUntil()

bool CppCommon::RWLock::TryLockWriteUntil ( const UtcTimestamp timestamp)
inline

Try to acquire write lock until the given timestamp.

Will block until the given timestamp in the worst case.

Parameters
timestamp- Timestamp to stop wait for the write lock
Returns
'true' if the write lock was successfully acquired, 'false' if the write lock is busy

Definition at line 88 of file rw_lock.h.

◆ UnlockRead()

void CppCommon::RWLock::UnlockRead ( )

Release read lock.

Will not block.

Definition at line 199 of file rw_lock.cpp.

◆ UnlockWrite()

void CppCommon::RWLock::UnlockWrite ( )

Release write lock.

Will not block.

Definition at line 200 of file rw_lock.cpp.


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