PLCnext API Documentation 25.0.2.69
|
Condition variables can be used to synchronize between multiple threads More...
#include <ConditionVariable.hpp>
Public Member Functions | |
ConditionVariable (void) | |
Constructs an ConditionVariable instance. | |
~ConditionVariable (void) | |
Destructs this instance and frees all resources. | |
template<class Predicate > | |
void | Wait (Mutex &mutex, Predicate predicate) |
Blocks the calling thread until NotifyOne or NotifyAll is invoked and the predicate is fullfilled More... | |
template<class Predicate > | |
bool | WaitFor (Mutex &mutex, uint64 timeoutMs, Predicate predicate) |
Like Wait but returns if the signal is not deliverd within a specified time More... | |
template<class Predicate > | |
bool | WaitUntil (Mutex &mutex, TimePointClockSteady timePoint, Predicate predicate) |
Like Wait but returns if the signal is not deliverd until a specified time point More... | |
void | NotifyOne (void) |
Unblocks at least one thread waiting on this condition variable. | |
void | NotifyAll (void) |
Unblocks every thread waiting on this condition variable. | |
Condition variables can be used to synchronize between multiple threads
Condition variables can be used to signal one or multiple threads about the occurrence of a specific event, for example the arrival of a message in a message queue. This way threads do not have to activly poll for such event but can sleep until they are signaled from another thread.
void Arp::System::Commons::Threading::ConditionVariable::Wait | ( | Mutex & | mutex, |
Predicate | predicate | ||
) |
Blocks the calling thread until NotifyOne or NotifyAll is invoked and the predicate is fullfilled
The mutex is automatically unlocked when the calling thread enters the waiting state and reacquired when the blocked thread is woken up. Thus the calling thread has to hold the lock on the assigned mutex before Wait is actually called. It also has to release the lock after it finishes the processing after it returned from wait.
The predicate is internally checked. If it is not fullfilled when the Wait would return (spurios wakeup) then the calling thread will reenter sleeping state. In pseudo code this could be expressed in the following way
while (!predicate()) Wait(mutex);
mutex | A reference to a mutex which must be locked by the calling thread. |
bool Arp::System::Commons::Threading::ConditionVariable::WaitFor | ( | Mutex & | mutex, |
uint64 | timeoutMs, | ||
Predicate | predicate | ||
) |
Like Wait but returns if the signal is not deliverd within a specified time
If the condition is not met inside the specified timeout the lock on the mutex will also be reacquired but the function returns with false
as result.
mutex | A reference to a mutex which must be locked by the calling thread. |
timeoutMs | Maximal amount of milliseconds the calling thread is allowed to block. |
bool Arp::System::Commons::Threading::ConditionVariable::WaitUntil | ( | Mutex & | mutex, |
TimePointClockSteady | timePoint, | ||
Predicate | predicate | ||
) |
Like Wait but returns if the signal is not deliverd until a specified time point
If the condition is not met until the specified timeout time the lock on the mutex will also be reacquired but the function returns with false
as result.
mutex | A reference to a mutex which must be locked by the calling thread. |
timePoint | Timepoint until which the method will latest return |