nlib
nn::nlib::threading::SharedCriticalSection Class Referencefinal

Implements a read/write lock. Used when multiple threads simultaneously read data, and a single thread writes data. More...

#include "nn/nlib/threading/SharedCriticalSection.h"

Public Member Functions

constexpr SharedCriticalSection () noexcept=default
 Instantiates the object with default parameters (default constructor).
 
void lock_shared () noexcept NLIB_ACQUIRE_SHARED()
 Gets the read lock, and enters the critical section. Blocks until it can get a lock.
 
NLIB_CHECK_RESULT bool try_lock_shared () noexcept NLIB_TRY_ACQUIRE_SHARED(true)
 Gets the read lock, and attempts to enter the critical section. More...
 
NLIB_CHECK_RESULT bool try_lock_shared_for (const TimeSpan &timeout) noexcept NLIB_TRY_ACQUIRE_SHARED(true)
 Gets the read lock, and attempts to enter the critical section. Times out. More...
 
NLIB_CHECK_RESULT bool try_lock_shared_until (const DateTime &abstime) noexcept NLIB_TRY_ACQUIRE_SHARED(true)
 Gets the read lock, and attempts to enter the critical section. Times out. More...
 
void unlock_shared () noexcept NLIB_RELEASE_SHARED()
 Releases the read lock.
 
void lock () noexcept NLIB_ACQUIRE()
 Gets a write lock, and enters the critical section. Blocks until it can get a lock.
 
NLIB_CHECK_RESULT bool try_lock () noexcept NLIB_TRY_ACQUIRE(true)
 Gets a write lock, and attempts to enter the critical section. More...
 
NLIB_CHECK_RESULT bool try_lock_for (const TimeSpan &timeout) noexcept NLIB_TRY_ACQUIRE(true)
 Gets a write lock, and attempts to enter the critical section. Times out. More...
 
NLIB_CHECK_RESULT bool try_lock_until (const DateTime &abstime) noexcept NLIB_TRY_ACQUIRE(true)
 Gets a write lock, and attempts to enter the critical section. Times out. More...
 
void unlock () noexcept NLIB_RELEASE()
 Releases a write lock.
 

Detailed Description

Implements a read/write lock. Used when multiple threads simultaneously read data, and a single thread writes data.

Description
The parallelization is higher than using a simple CriticalSection when the threads reading data do not need to be excluded from each other.
The following code is a common implementation.
// Initialize.
// Thread that reads data.
m.lock_shared(); // Place a read lock. It does not wait unless it is currently writing.
// Read operation on shared data. (It must be possible for multiple threads to read simultaneously.)
.....
m.unlock_shared(); // Removes the read lock.
.....
// Thread that writes data.
m.lock(); // Places a write lock. Waits until there are no more reading or writing threads.
// Write operation to shared data.
.....
m.unlock(); // Removes the write lock.

Definition at line 25 of file SharedCriticalSection.h.

Member Function Documentation

◆ try_lock()

nn::nlib::threading::SharedCriticalSection::try_lock ( )
inlinenoexcept

Gets a write lock, and attempts to enter the critical section.

Returns
Returns true if a lock is acquired.

Definition at line 75 of file SharedCriticalSection.h.

◆ try_lock_for()

nn::nlib::threading::SharedCriticalSection::try_lock_for ( const TimeSpan timeout)
inlinenoexcept

Gets a write lock, and attempts to enter the critical section. Times out.

Parameters
[in]timeoutSpecifies the time until timeout.
Returns
Returns true if a lock is acquired.

Definition at line 80 of file SharedCriticalSection.h.

◆ try_lock_shared()

nn::nlib::threading::SharedCriticalSection::try_lock_shared ( )
inlinenoexcept

Gets the read lock, and attempts to enter the critical section.

Returns
Returns true if a lock is acquired.

Definition at line 47 of file SharedCriticalSection.h.

◆ try_lock_shared_for()

nn::nlib::threading::SharedCriticalSection::try_lock_shared_for ( const TimeSpan timeout)
inlinenoexcept

Gets the read lock, and attempts to enter the critical section. Times out.

Parameters
[in]timeoutSpecifies the time until timeout.
Returns
Returns true if a lock is acquired.

Definition at line 52 of file SharedCriticalSection.h.

◆ try_lock_shared_until()

nn::nlib::threading::SharedCriticalSection::try_lock_shared_until ( const DateTime abstime)
inlinenoexcept

Gets the read lock, and attempts to enter the critical section. Times out.

Parameters
[in]abstimeThe time when the timeout occurs.
Returns
Returns true if a lock is acquired.

Definition at line 58 of file SharedCriticalSection.h.

◆ try_lock_until()

nn::nlib::threading::SharedCriticalSection::try_lock_until ( const DateTime abstime)
inlinenoexcept

Gets a write lock, and attempts to enter the critical section. Times out.

Parameters
[in]abstimeThe time when the timeout occurs.
Returns
Returns true if a lock is acquired.

Definition at line 86 of file SharedCriticalSection.h.


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