PLCnext API Documentation 25.0.2.69
Public Member Functions | List of all members
Arp::System::Commons::Threading::ConditionVariable Class Reference

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.
 

Detailed Description

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.

Member Function Documentation

◆ Wait()

template<class Predicate >
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);

Parameters
mutexA reference to a mutex which must be locked by the calling thread.

◆ WaitFor()

template<class Predicate >
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.

Parameters
mutexA reference to a mutex which must be locked by the calling thread.
timeoutMsMaximal amount of milliseconds the calling thread is allowed to block.
Returns
false if a timeout occurred before NotifyOne or NotifyAll was called, otherwise true is returned.

◆ WaitUntil()

template<class Predicate >
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.

Parameters
mutexA reference to a mutex which must be locked by the calling thread.
timePointTimepoint until which the method will latest return
Returns
false if a timeout occurred before NotifyOne or NotifyAll was called, otherwise true is returned.

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