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

Named read/write lock synchronization primitive. More...

#include <named_rw_lock.h>

Public Member Functions

 NamedRWLock (const std::string &name)
 Default class constructor. More...
 
 NamedRWLock (const NamedRWLock &)=delete
 
 NamedRWLock (NamedRWLock &&lock)=delete
 
 ~NamedRWLock ()
 
NamedRWLockoperator= (const NamedRWLock &)=delete
 
NamedRWLockoperator= (NamedRWLock &&lock)=delete
 
const std::string & name () const
 Get the read/write lock name. More...
 
bool TryLockRead ()
 Try to acquire read lock without block. More...
 
bool TryLockWrite ()
 Try to acquire write lock without block. More...
 
bool TryConvertWriteToRead ()
 Try to convert write lock to read 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 TryConvertWriteToReadFor (const Timespan &timespan)
 Try to convert write lock to read 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...
 
bool TryConvertWriteToReadUntil (const UtcTimestamp &timestamp)
 Try to convert write lock to read 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...
 
void ConvertWriteToRead ()
 Convert write lock to read lock without block. More...
 

Detailed Description

Named read/write lock synchronization primitive.

Named read/write lock behaves as a simple read/write lock but could be shared between processes on the same machine.

The fast lock is around 7% faster than the critical section when there is no contention, when used solely for mutual exclusion. It is also much smaller than the critical section.

Thread-safe.

See also
RWLock
Examples
threads_named_rw_lock.cpp.

Definition at line 32 of file named_rw_lock.h.

Constructor & Destructor Documentation

◆ NamedRWLock() [1/3]

CppCommon::NamedRWLock::NamedRWLock ( const std::string &  name)
explicit

Default class constructor.

Parameters
name- Read/Write lock name

Definition at line 507 of file named_rw_lock.cpp.

◆ NamedRWLock() [2/3]

CppCommon::NamedRWLock::NamedRWLock ( const NamedRWLock )
delete

◆ NamedRWLock() [3/3]

CppCommon::NamedRWLock::NamedRWLock ( NamedRWLock &&  lock)
delete

◆ ~NamedRWLock()

CppCommon::NamedRWLock::~NamedRWLock ( )

Definition at line 518 of file named_rw_lock.cpp.

Member Function Documentation

◆ ConvertWriteToRead()

void CppCommon::NamedRWLock::ConvertWriteToRead ( )

Convert write lock to read lock without block.

Exclusive acquires are not given a chance to acquire the lock before this function does - as a result, this function will never block.

Will not block.

Definition at line 606 of file named_rw_lock.cpp.

◆ LockRead()

void CppCommon::NamedRWLock::LockRead ( )

Acquire read lock with block.

Will block.

Definition at line 602 of file named_rw_lock.cpp.

◆ LockWrite()

void CppCommon::NamedRWLock::LockWrite ( )

Acquire write lock with block.

Will block.

Definition at line 603 of file named_rw_lock.cpp.

◆ name()

const std::string & CppCommon::NamedRWLock::name ( ) const

Get the read/write lock name.

Definition at line 524 of file named_rw_lock.cpp.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ TryConvertWriteToRead()

bool CppCommon::NamedRWLock::TryConvertWriteToRead ( )

Try to convert write lock to read lock without block.

Will not block.

Returns
'true' if the write lock was successfully converted to the read one, 'false' if the write lock is busy

Definition at line 528 of file named_rw_lock.cpp.

◆ TryConvertWriteToReadFor()

bool CppCommon::NamedRWLock::TryConvertWriteToReadFor ( const Timespan timespan)

Try to convert write lock to read lock for the given timespan.

Will block for the given timespan in the worst case.

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

Definition at line 578 of file named_rw_lock.cpp.

◆ TryConvertWriteToReadUntil()

bool CppCommon::NamedRWLock::TryConvertWriteToReadUntil ( const UtcTimestamp timestamp)
inline

Try to convert write lock to read 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 to read lock conversion
Returns
'true' if the write lock was successfully converted to the read one, 'false' if the write lock is busy

Definition at line 121 of file named_rw_lock.h.

◆ TryLockRead()

bool CppCommon::NamedRWLock::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
Examples
threads_named_rw_lock.cpp.

Definition at line 526 of file named_rw_lock.cpp.

◆ TryLockReadFor()

bool CppCommon::NamedRWLock::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 530 of file named_rw_lock.cpp.

◆ TryLockReadUntil()

bool CppCommon::NamedRWLock::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 103 of file named_rw_lock.h.

◆ TryLockWrite()

bool CppCommon::NamedRWLock::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
Examples
threads_named_rw_lock.cpp.

Definition at line 527 of file named_rw_lock.cpp.

◆ TryLockWriteFor()

bool CppCommon::NamedRWLock::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 554 of file named_rw_lock.cpp.

◆ TryLockWriteUntil()

bool CppCommon::NamedRWLock::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 112 of file named_rw_lock.h.

◆ UnlockRead()

void CppCommon::NamedRWLock::UnlockRead ( )

Release read lock.

Will not block.

Examples
threads_named_rw_lock.cpp.

Definition at line 604 of file named_rw_lock.cpp.

◆ UnlockWrite()

void CppCommon::NamedRWLock::UnlockWrite ( )

Release write lock.

Will not block.

Examples
threads_named_rw_lock.cpp.

Definition at line 605 of file named_rw_lock.cpp.


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