Names specified here
Name Description Notes Source Availability
cnd_broadcast() Signal to all threads on a condition variable ? (·) <threads.h> C11
cnd_destroy() Destroy a condition variable ? (·) <threads.h> C11
cnd_init() Initialize a condition variable ? (·) <threads.h> C11
cnd_signal() Signal to a thread on a condition variable ? (·) <threads.h> C11
cnd_t Condition-variable type ? T <threads.h> C11
cnd_timedwait() Wait on a condition variable for a limited time ? (·) <threads.h> C11
cnd_wait() Wait on a condition variable ? (·) <threads.h> C11

Names matching ^cnd_[a-z] might be added to <threads.h>.

A condition variable is a construct that allows threads to synchronize with each other. <threads.h> defines a type cnd_t to represent a condition variable. Any number of threads can wait on a condition variable, blocking until some other thread signals the variable, which can unblock one thread arbitrarily, or all threads.

#include <threads.h>
int cnd_init(cnd_t *cp);
void cnd_destroy(cnd_t *cp);

A condition variable c must be initialized with cnd_init(&c) before being used by any other function. Resources allocated for it by cnd_init should be released with cnd_destroy(&c). cnd_init returns thrd_success on success, thrd_nomem if there is insufficient memory to initialize the variable, or thrd_error on any other kind of failure.

#include <threads.h>
int cnd_wait(cnd_t *cp, mtx_t *mp);
int cnd_timedwait(cnd_t *cp, mtx_t *mp,
                  const struct timespec *tp);

While a thread has the lock on mutex m, it can wait indefinitely for a signal on condition variable c by calling cnd_wait(&c, &m), or it can wait for a limited time t with cnd_timedwait(&c, &m, &t). The mutex is unlocked just before the thread starts waiting, and every thread unblocked by a signal will first attempt to claim the lock before its call to cnd_wait or cnd_timedwait returns.

These functions return thrd_success when successfully signalled, and thrd_error if it was not possible to wait. cnd_timedwait returns thrd_timedout if the wait timed out before the thread was signalled.

#include <threads.h>
int cnd_signal(cnd_t *cp);
int cnd_broadcast(cnd_t *cp);

These functions signal threads waiting on the condition variable *cp. cnd_signal chooses one thread arbitrarily and signals it. cnd_broadcast signals all threads waiting on the variable. These functions return thrd_success on success and thrd_error on error.


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000