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

Named condition variable synchronization primitive. More...

#include <named_condition_variable.h>

Public Member Functions

 NamedConditionVariable (const std::string &name)
 Default class constructor. More...
 
 NamedConditionVariable (const NamedConditionVariable &)=delete
 
 NamedConditionVariable (NamedConditionVariable &&cv)=delete
 
 ~NamedConditionVariable ()
 
NamedConditionVariableoperator= (const NamedConditionVariable &)=delete
 
NamedConditionVariableoperator= (NamedConditionVariable &&cv)=delete
 
const std::string & name () const
 Get the condition variable name. More...
 
void NotifyOne ()
 Notify one of waiting thread about event occurred. More...
 
void NotifyAll ()
 Notify all waiting threads about event occurred. More...
 
void Wait ()
 Wait until condition variable is notified. More...
 
template<typename TPredicate >
void Wait (TPredicate predicate)
 Wait until condition variable is notified using the given predicate. More...
 
bool TryWaitFor (const Timespan &timespan)
 Try to wait for the given timespan until condition variable is notified. More...
 
template<typename TPredicate >
bool TryWaitFor (const Timespan &timespan, TPredicate predicate)
 Try to wait for the given timespan until condition variable is notified using the given predicate. More...
 
bool TryWaitUntil (const UtcTimestamp &timestamp)
 Try to wait until the given timestamp until condition variable is notified. More...
 
template<typename TPredicate >
bool TryWaitUntil (const UtcTimestamp &timestamp, TPredicate predicate)
 Try to wait until the given timestamp until condition variable is notified using the given predicate. More...
 

Detailed Description

Named condition variable synchronization primitive.

Named condition variable behaves as a simple condition variable but could be shared between processes on the same machine.

Thread-safe.

Examples
threads_named_condition_variable.cpp.

Definition at line 26 of file named_condition_variable.h.

Constructor & Destructor Documentation

◆ NamedConditionVariable() [1/3]

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

Default class constructor.

Parameters
name- Condition variable name

Definition at line 243 of file named_condition_variable.cpp.

◆ NamedConditionVariable() [2/3]

CppCommon::NamedConditionVariable::NamedConditionVariable ( const NamedConditionVariable )
delete

◆ NamedConditionVariable() [3/3]

CppCommon::NamedConditionVariable::NamedConditionVariable ( NamedConditionVariable &&  cv)
delete

◆ ~NamedConditionVariable()

CppCommon::NamedConditionVariable::~NamedConditionVariable ( )

Definition at line 254 of file named_condition_variable.cpp.

Member Function Documentation

◆ name()

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

Get the condition variable name.

Definition at line 260 of file named_condition_variable.cpp.

◆ NotifyAll()

void CppCommon::NamedConditionVariable::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_named_condition_variable.cpp.

Definition at line 263 of file named_condition_variable.cpp.

◆ NotifyOne()

void CppCommon::NamedConditionVariable::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_named_condition_variable.cpp.

Definition at line 262 of file named_condition_variable.cpp.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ TryWaitFor() [1/2]

bool CppCommon::NamedConditionVariable::TryWaitFor ( const Timespan timespan)

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

The execution of the current thread is blocked during timespan, or until notified (if the latter happens first).

Will block for the given timespan in the worst case.

Parameters
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 267 of file named_condition_variable.cpp.

◆ TryWaitFor() [2/2]

template<typename TPredicate >
bool CppCommon::NamedConditionVariable::TryWaitFor ( 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(timeout - UtcTimestamp()))
return predicate();
return true;
bool TryWaitFor(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
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 named_condition_variable.inl.

◆ TryWaitUntil() [1/2]

bool CppCommon::NamedConditionVariable::TryWaitUntil ( const UtcTimestamp timestamp)
inline

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

The execution of the current thread is blocked either until notified or until timestamp, whichever happens first.

Will block until the given timestamp in the worst case.

Parameters
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 124 of file named_condition_variable.h.

◆ TryWaitUntil() [2/2]

template<typename TPredicate >
bool CppCommon::NamedConditionVariable::TryWaitUntil ( 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(timestamp - UtcTimestamp(), predicate);

Will block until the given timestamp in the worst case.

Parameters
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 140 of file named_condition_variable.h.

◆ Wait() [1/2]

void CppCommon::NamedConditionVariable::Wait ( )

Wait until condition variable is notified.

The execution of the current thread is blocked until notified.

Will block.

Examples
threads_named_condition_variable.cpp.

Definition at line 265 of file named_condition_variable.cpp.

◆ Wait() [2/2]

template<typename TPredicate >
void CppCommon::NamedConditionVariable::Wait ( TPredicate  predicate)

Wait until condition variable is notified using the given predicate.

This method is equivalent to:

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

Will block.

Parameters
predicate- Predicate to check

Definition at line 12 of file named_condition_variable.inl.


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