PIP 5.6.1
Platform-Independent Primitives
Public Member Functions | List of all members
PIConditionVariable Class Reference

Condition variable used together with external PIMutex. More...

#include <piconditionvar.h>

Public Member Functions

 PIConditionVariable ()
 Constructs condition variable.
 
virtual ~PIConditionVariable ()
 Destroys condition variable.
 
void notifyOne ()
 Wakes one waiting thread, if any.
 
void notifyAll ()
 Wakes all threads currently waiting on this variable.
 
virtual void wait (PIMutex &lk)
 Waits until notification, temporarily releasing lk while blocked.
 
virtual void wait (PIMutex &lk, const std::function< bool()> &condition)
 Waits until condition becomes true, rechecking it after each wakeup while lk is locked. More...
 
virtual bool waitFor (PIMutex &lk, PISystemTime timeout)
 Waits for at most timeout and returns true if awakened before it expires.
 
virtual bool waitFor (PIMutex &lk, PISystemTime timeout, const std::function< bool()> &condition)
 Waits until condition becomes true or timeout expires. More...
 

Detailed Description

Condition variable used together with external PIMutex.

Member Function Documentation

◆ wait()

void PIConditionVariable::wait ( PIMutex lk,
const std::function< bool()> &  condition 
)
virtual

Waits until condition becomes true, rechecking it after each wakeup while lk is locked.

The execution of the current thread (which shall have locked with lk method PIMutex::lock()) is blocked until notified.

At the moment of blocking the thread, the function automatically calls lk.unlock() (PIMutex::unlock()), allowing other locked threads to continue.

Once notified (explicitly, by some other thread), the function unblocks and calls lk.lock() (PIMutex::lock()), leaving lk in the same state as when the function was called. Then the function returns (notice that this last mutex locking may block again the thread before returning).

Generally, the function is notified to wake up by a call in another thread either to member notifyOne() or to member notifyAll(). But certain implementations may produce spurious wake-up calls without any of these functions being called. Therefore, users of this function shall ensure their condition for resumption is met.

If condition is specified, the function only blocks if condition returns false, and notifications can only unblock the thread when it becomes true (which is specially useful to check against spurious wake-up calls).

Parameters
lklock object used by method wait for data protection
conditionA callable object or function that takes no arguments and returns a value that can be evaluated as a bool. This is called repeatedly until it evaluates to true.

◆ waitFor()

bool PIConditionVariable::waitFor ( PIMutex lk,
PISystemTime  timeout,
const std::function< bool()> &  condition 
)
virtual

Waits until condition becomes true or timeout expires.

The execution of the current thread (which shall have locked with lk method PIMutex::lock()) is blocked during timeout, or until notified (if the latter happens first).

At the moment of blocking the thread, the function automatically calls lk.lock() (PIMutex::lock()), allowing other locked threads to continue.

Once notified or once timeout has passed, the function unblocks and calls lk.unlock() (PIMutex::unlock()), leaving lk in the same state as when the function was called. Then the function returns (notice that this last mutex locking may block again the thread before returning).

Generally, the function is notified to wake up by a call in another thread either to member notifyOne() or to member notifyAll(). But certain implementations may produce spurious wake-up calls without any of these functions being called. Therefore, users of this function shall ensure their condition for resumption is met.

If condition is specified, the function only blocks if condition returns false, and notifications can only unblock the thread when it becomes true (which is especially useful to check against spurious wake-up calls).

Parameters
lklock object used by method wait for data protection
conditionA callable object or function that takes no arguments and returns a value that can be evaluated as a bool. This is called repeatedly until it evaluates to true.
Returns
false if timeout reached or true if wakeup condition is true