| PTHREAD_COND_INIT(3) | Library Functions Manual | PTHREAD_COND_INIT(3) |
pthread_cond_init,
pthread_cond_destroy,
pthread_cond_wait,
pthread_cond_timedwait,
pthread_cond_broadcast,
pthread_cond_signal — block
and unblock threads with condition variables
POSIX Threads Library (libpthread,
-lpthread)
#include <pthread.h>
int
pthread_cond_init(pthread_cond_t
*cond, const pthread_condattr_t *attr);
int
pthread_cond_destroy(pthread_cond_t
*cond);
int
pthread_cond_wait(pthread_cond_t
*cond, pthread_mutex_t *mutex);
int
pthread_cond_timedwait(pthread_cond_t
*cond, pthread_mutex_t *mutex,
const struct timespec *abstime);
int
pthread_cond_broadcast(pthread_cond_t
*cond);
int
pthread_cond_signal(pthread_cond_t
*cond);
The
pthread_cond_init()
function creates a new condition variable with attributes specified by
attr. If attr is
NULL, the default attributes are used.
The
pthread_cond_destroy()
function frees the resources associated with the condition variable
cond.
The
pthread_cond_wait()
function atomically blocks the current thread by waiting on the condition
variable cond, and unlocks the mutex specified by
mutex. The waiting thread unblocks only after another
thread calls pthread_cond_broadcast() or
pthread_cond_signal() with the same condition
variable. The
pthread_cond_timedwait()
function does the same, but returns after the system time reaches
abstime. In all cases, both functions then reacquire
the lock on mutex before returning.
The
pthread_cond_broadcast()
function unblocks all threads waiting for the condition variable
cond. The
pthread_cond_signal()
function unblocks at least one thread waiting for the condition variable
cond.
These functions return zero for success and positive error numbers for failure.
pthread_cond_init() fails if:
EINVAL]ENOMEM]EAGAIN]The other functions fail if:
EINVAL]pthread_cond_destroy() additionally fails
if:
EBUSY]pthread_cond_timedwait() additionally
fails if:
ETIMEDOUT]These functions conform to ISO/IEC 9945-1:1996 (“POSIX.1”).
| July 3, 2025 | Debian |