#include <nng/nng.h>
#include <nng/supplemental/util/platform.h>
void nng_cv_wait(nng_cv *cv);
The nng_cv_wait()
waits for the condition variable cv to be signaled
by another thread calling either nng_cv_wake()
or
nng_cv_wake1()
.
The caller must have have ownership of the mutex that was used when cv was allocated. This function will drop the ownership of that mutex, and reacquire it atomically just before returning to the caller. (The waiting is done without holding the mutex.)
Note
|
Any condition may be used or checked, but the condition must be checked, as it is possible for this function to wake up spuriously. The best way to do this is inside a loop that repeats until the condition tests for true. |
The following example demonstrates use of this function:
Example 1: Waiting for the condition
nng_mtx_lock(m); // assume cv was allocated using m
while (!condition_true) {
nng_cv_wait(cv);
}
// condition_true is true
nng_mtx_unlock(m);
Example 2: Signaling the condition
nng_mtx_lock(m);
condition_true = true;
nng_cv_wake(cv);
nng_mtx_unlock(m);