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... | |
bool | wait_until (uint64_t millisec) |
Wait for a notification until the specified time. More... | |
bool | wait_for (uint32_t millisec) |
Wait for a notification 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 158 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:
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:
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: