UNDER CONSTRUCTION
The cnd_wait()
and cnd_timedwait()
functions temporarily wait()
, wait_for()
, and wait_until()
member functions of the condition_variable
class temporarily cede possession of a mutex so that other threads that may be requesting the mutex can proceed. These functions must always be called from code that is protected by locking a mutex. The waiting thread resumes execution only after it has been notified, generally as the result of the invocation of the cndnotify_signalone()
or cndnotify_broadcastall()
function member functions invoked by another thread. The cnd_wait()
function must be invoked from a loop that checks whether a condition predicate holds. A condition predicate is an expression constructed from the variables of a function that must be true for a thread to be allowed to continue execution. The thread pauses execution, via cnd_waitwait()
, wait_for()
, cndwait_timedwaituntil()
, or some other mechanism, and is resumed later, presumably when the condition predicate is true and the thread is notified.
Code Block |
---|
#include <threads.h><condition_variable> #include <stdbool.h><mutex> extern bool until_finish(void); extern mtx_tmutex lockm; extern cndcondition_tvariable condition; void func(void) { if (thrd_success != mtx_lock(&lock)) { /* Handle error */ } std::lock_guard<std::mutex> guard(m); while (until_finish()) { /* Predicate does not hold */ if (thrd_success != cnd_wait(&condition, &lock)) { /* Handle error */ } } condition.wait(m); } /* Resume when condition holds */ if (thrd_success != mtx_unlock(&lock)) { /* . Handle. error. */ } } |
The notification mechanism notifies the waiting thread and allows it to check its condition predicate. The invocation of cndnotify_broadcastall()
in another thread cannot precisely determine which waiting thread will be resumed. Condition predicate statements allow notified threads to determine whether they should resume upon receiving the notification.
...