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

Condition variable synchronization primitive. More...

#include <condition_variable.h>

Public Member Functions

 ConditionVariable ()
 
 ConditionVariable (const ConditionVariable &)=delete
 
 ConditionVariable (ConditionVariable &&cv)=delete
 
 ~ConditionVariable ()
 
ConditionVariableoperator= (const ConditionVariable &)=delete
 
ConditionVariableoperator= (ConditionVariable &&cv)=delete
 
void NotifyOne ()
 Notify one of waiting thread about event occurred. More...
 
void NotifyAll ()
 Notify all waiting threads about event occurred. More...
 
void Wait (CriticalSection &cs)
 Wait until condition variable is notified. More...
 
template<typename TPredicate >
void Wait (CriticalSection &cs, TPredicate predicate)
 Wait until condition variable is notified using the given predicate. More...
 
bool TryWaitFor (CriticalSection &cs, const Timespan &timespan)
 Try to wait for the given timespan until condition variable is notified. More...
 
template<typename TPredicate >
bool TryWaitFor (CriticalSection &cs, const Timespan &timespan, TPredicate predicate)
 Try to wait for the given timespan until condition variable is notified using the given predicate. More...
 
bool TryWaitUntil (CriticalSection &cs, const UtcTimestamp &timestamp)
 Try to wait until the given timestamp until condition variable is notified. More...
 
template<typename TPredicate >
bool TryWaitUntil (CriticalSection &cs, const UtcTimestamp &timestamp, TPredicate predicate)
 Try to wait until the given timestamp until condition variable is notified using the given predicate. More...
 

Detailed Description

Condition variable synchronization primitive.

Condition variable is a synchronization primitive that enable threads to wait until a particular condition occurs. Condition variables are user-mode objects that cannot be shared across processes.

Condition variables enable threads to atomically release a lock and enter the sleeping state. They can be used with critical sections. Condition variables support operations that "notify one" or "notify all" waiting threads. After a thread is woken, it re-acquires the lock it released when the thread entered the sleeping state.

Thread-safe.

https://en.wikipedia.org/wiki/Monitor_(synchronization)

Examples
threads_condition_variable.cpp.

Definition at line 32 of file condition_variable.h.

Constructor & Destructor Documentation

◆ ConditionVariable() [1/3]

CppCommon::ConditionVariable::ConditionVariable ( )

Definition at line 120 of file condition_variable.cpp.

◆ ConditionVariable() [2/3]

CppCommon::ConditionVariable::ConditionVariable ( const ConditionVariable )
delete

◆ ConditionVariable() [3/3]

CppCommon::ConditionVariable::ConditionVariable ( ConditionVariable &&  cv)
delete

◆ ~ConditionVariable()

CppCommon::ConditionVariable::~ConditionVariable ( )

Definition at line 131 of file condition_variable.cpp.

Member Function Documentation

◆ NotifyAll()

void CppCommon::ConditionVariable::NotifyAll ( )

Notify all waiting threads about event occurred.

Unblocks all threads currently waiting for this condition. If no threads are waiting, the function does nothing.

Will not block.

Examples
threads_condition_variable.cpp.

Definition at line 138 of file condition_variable.cpp.

◆ NotifyOne()

void CppCommon::ConditionVariable::NotifyOne ( )

Notify one of waiting thread about event occurred.

Unblocks one of the threads currently waiting for this condition. If no threads are waiting, the function does nothing. If more than one, it is unspecified which of the threads is selected.

Will not block.

Examples
threads_condition_variable.cpp.

Definition at line 137 of file condition_variable.cpp.

◆ operator=() [1/2]

ConditionVariable& CppCommon::ConditionVariable::operator= ( ConditionVariable &&  cv)
delete

◆ operator=() [2/2]

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

◆ TryWaitFor() [1/2]

bool CppCommon::ConditionVariable::TryWaitFor ( CriticalSection cs,
const Timespan timespan 
)

Try to wait for the given timespan until condition variable is notified.

The execution of the current thread (which shall have locked critical section) is blocked during timespan, or until notified (if the latter happens first).

Will block for the given timespan in the worst case.

Parameters
cs- Critical section (must be locked)
timespan- Timespan to wait for the condition variable notification
Returns
'true' if the condition variable was successfully notified, 'false' if the timeout was occurred

Definition at line 142 of file condition_variable.cpp.

◆ TryWaitFor() [2/2]

template<typename TPredicate >
bool CppCommon::ConditionVariable::TryWaitFor ( CriticalSection cs,
const Timespan timespan,
TPredicate  predicate 
)

Try to wait for the given timespan until condition variable is notified using the given predicate.

This method is equivalent to:

Timestamp timeout = UtcTimestamp() + timespan;
while (!predicate())
if (!TryWaitFor(cs, timeout - UtcTimestamp()))
return predicate();
return true;
bool TryWaitFor(CriticalSection &cs, const Timespan &timespan)
Try to wait for the given timespan until condition variable is notified.

Will block for the given timespan in the worst case.

Parameters
cs- Critical section (must be locked)
timespan- Timespan to wait for the condition variable notification
predicate- Predicate to check
Returns
'true' if the condition variable was successfully notified, 'false' if the timeout was occurred

Definition at line 19 of file condition_variable.inl.

◆ TryWaitUntil() [1/2]

bool CppCommon::ConditionVariable::TryWaitUntil ( CriticalSection cs,
const UtcTimestamp timestamp 
)
inline

Try to wait until the given timestamp until condition variable is notified.

The execution of the current thread (which shall have locked critical section) is blocked either until notified or until timestamp, whichever happens first.

Will block until the given timestamp in the worst case.

Parameters
cs- Critical section (must be locked)
timestamp- Timestamp to stop wait for the condition variable notification
Returns
'true' if the condition variable was successfully notified, 'false' if the timeout was occurred

Definition at line 131 of file condition_variable.h.

◆ TryWaitUntil() [2/2]

template<typename TPredicate >
bool CppCommon::ConditionVariable::TryWaitUntil ( CriticalSection cs,
const UtcTimestamp timestamp,
TPredicate  predicate 
)
inline

Try to wait until the given timestamp until condition variable is notified using the given predicate.

This method is equivalent to:

return TryWaitFor(cs, timestamp - UtcTimestamp(), predicate);

Will block until the given timestamp in the worst case.

Parameters
cs- Critical section (must be locked)
timestamp- Timestamp to stop wait for the condition variable notification
predicate- Predicate to check
Returns
'true' if the condition variable was successfully notified, 'false' if the timeout was occurred

Definition at line 148 of file condition_variable.h.

◆ Wait() [1/2]

void CppCommon::ConditionVariable::Wait ( CriticalSection cs)

Wait until condition variable is notified.

The execution of the current thread (which shall have locked critical section) is blocked until notified.

Will block.

Parameters
cs- Critical section (must be locked)
Examples
threads_condition_variable.cpp.

Definition at line 140 of file condition_variable.cpp.

◆ Wait() [2/2]

template<typename TPredicate >
void CppCommon::ConditionVariable::Wait ( CriticalSection cs,
TPredicate  predicate 
)

Wait until condition variable is notified using the given predicate.

This method is equivalent to:

while (!predicate()) Wait(cs);
void Wait(CriticalSection &cs)
Wait until condition variable is notified.

Will block.

Parameters
cs- Critical section (must be locked)
predicate- Predicate to check

Definition at line 12 of file condition_variable.inl.


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