The ConditionVariable class is a synchronization primitive that allows threads to wait until a particular condition occurs. More...
#include <ConditionVariable.h>
Public Member Functions | |
ConditionVariable (Mutex &mutex) | |
Create and initialize a ConditionVariable object. More... | |
void | wait () |
Wait for a notification. More... | |
template<typename Predicate > | |
void | wait (Predicate pred) |
Wait for a predicate. More... | |
bool | wait_until (uint64_t millisec) |
Wait for a notification until the specified time. More... | |
cv_status | wait_until (Kernel::Clock::time_point abs_time) |
Wait for a notification until the specified time. More... | |
template<class Predicate > | |
bool | wait_until (Kernel::Clock::time_point abs_time, Predicate pred) |
Wait for a predicate until the specified time. More... | |
bool | wait_for (uint32_t millisec) |
Wait for a notification or timeout. More... | |
cv_status | wait_for (Kernel::Clock::duration_u32 rel_time) |
Wait for a notification or timeout. More... | |
template<class Predicate > | |
bool | wait_for (Kernel::Clock::duration rel_time, Predicate pred) |
Wait for a predicate or timeout. More... | |
void | notify_one () |
Notify one waiter on this condition variable that a condition changed. More... | |
void | notify_all () |
Notify all waiters on this condition variable that a condition changed. More... | |
~ConditionVariable () | |
ConditionVariable destructor. More... | |
The ConditionVariable class is a synchronization primitive that allows threads to wait until a particular condition occurs.
Use the condition variable in conjunction with a mutex to safely wait for or notify waiters of condition changes to a resource accessible by multiple threads.
The thread that intends to wait on a ConditionVariable must:
wait
, wait_for
or wait_until
. While the thread is waiting, the mutex is unlocked.wait_for
and wait_until
the timeout expires, the thread is awakened.The thread that intends to notify a ConditionVariable must:
notify_one
or notify_all
on the condition variable.All threads waiting on the condition variable wake when ConditionVariable::notify_all
is called. At least one thread waiting on the condition variable wakes when ConditionVariable::notify_one
is called.
While a thread is waiting for notification of a ConditionVariable, it releases the lock held on the mutex. The ConditionVariable reacquires the mutex lock before exiting the wait function.
ConditionVariable::notify_one
is unspecified if there are multiple waiters.ConditionVariable::notify_one
or ConditionVariable::notify_all
is called and there are one or more waiters, and one or more threads attempting to acquire the condition variable's mutex, the order in which the mutex is acquired is unspecified.ConditionVariable::notify_all
is called is undefined.ConditionVariable::wait
and ConditionVariable::wait_for
is undefined if the condition variable's mutex is locked more than once by the calling thread.Example:
Definition at line 168 of file ConditionVariable.h.
ConditionVariable | ( | Mutex & | mutex | ) |
Create and initialize a ConditionVariable object.
~ConditionVariable | ( | ) |
ConditionVariable destructor.
void notify_all | ( | ) |
Notify all waiters on this condition variable that a condition changed.
This function unblocks all of the threads waiting for the condition variable.
void notify_one | ( | ) |
Notify one waiter on this condition variable that a condition changed.
This function unblocks one of the threads waiting for the condition variable.
void wait | ( | ) |
Wait for a notification.
Wait causes the current thread to block until the condition variable receives a notification from another thread.
Example:
void wait | ( | Predicate | pred | ) |
Wait for a predicate.
Wait causes the current thread to block until the predicate is true.
pred | A function-like object such that pred() is convertible to bool |
Example:
Definition at line 237 of file ConditionVariable.h.
bool wait_for | ( | uint32_t | millisec | ) |
Wait for a notification or timeout.
Wait for
causes the current thread to block until the condition variable receives a notification from another thread, or the timeout specified by the millisec parameter is reached.
millisec | Timeout value or osWaitForever in case of no timeout. |
true
if a timeout occurred, false
otherwise.Example:
5s
rather than 5000
. cv_status wait_for | ( | Kernel::Clock::duration_u32 | rel_time | ) |
Wait for a notification or timeout.
Wait for
causes the current thread to block until the condition variable receives a notification from another thread, or the timeout specified by the millisec parameter is reached.
rel_time | Timeout value. |
cv_status::timeout
if a timeout occurred, cv_status::no_timeout
otherwise.Example:
bool wait_for | ( | Kernel::Clock::duration | rel_time, |
Predicate | pred | ||
) |
Wait for a predicate or timeout.
Wait for
causes the current thread to block until the predicate is true, or the timeout specified by the rel_time parameter is reached.
rel_time | Timeout value. |
pred | a function-like object such that pred() is convertible to bool |
Example:
Definition at line 486 of file ConditionVariable.h.
bool wait_until | ( | uint64_t | millisec | ) |
Wait for a notification until the specified time.
Wait until causes the current thread to block until the condition variable is notified, or a specific time given by millisec parameter is reached.
millisec | Absolute end time referenced to Kernel::get_ms_count() |
true
if a timeout occurred, false
otherwise.Example:
Kernel::Clock::now() + 5s
rather than Kernel::get_ms_count() + 5000
. cv_status wait_until | ( | Kernel::Clock::time_point | abs_time | ) |
Wait for a notification until the specified time.
Wait until causes the current thread to block until the condition variable is notified, or a specific time given by millisec parameter is reached.
abs_time | Absolute end time referenced to Kernel::Clock |
cv_status::timeout
if a timeout occurred, cv_status::no_timeout
otherwise.Example:
bool wait_until | ( | Kernel::Clock::time_point | abs_time, |
Predicate | pred | ||
) |
Wait for a predicate until the specified time.
Wait until causes the current thread to block until the predicate is true, or a specific time given by abs_time parameter is reached.
abs_time | Absolute end time referenced to Kernel::Clock |
pred | A function-like object such that pred() is convertible to bool |
Example:
Definition at line 361 of file ConditionVariable.h.