Multithreaded Programming Guide

Similar Synchronization Functions—Condition Variables

Initialize a Condition Variable

cond_init(3THR)

#include <thread.h>

int cond_init(cond_t *cv, int type, int arg);

Use cond_init(3THR) to initialize the condition variable pointed to by cv. The type can be one of the following (note that arg is currently ignored). (For POSIX threads, see pthread_condattr_init(3THR).)

Condition variables can also be initialized by allocation in zeroed memory, in which case a type of USYNC_THREAD is assumed.

Multiple threads must not initialize the same condition variable simultaneously. A condition variable must not be reinitialized while other threads might be using it.

Condition Variables With Intraprocess Scope

#include <thread.h>

cond_t cv;
int ret;

/* to be used within this process only */
ret = cond_init(cv, USYNC_THREAD, 0); 

Condition Variables With Interprocess Scope

#include <thread.h>

cond_t cv;
int ret;

/* to be used among all processes */
ret = cond_init(&cv, USYNC_PROCESS, 0); 

Destroy a Condition Variable

cond_destroy(3THR)

#include <thread.h>

int cond_destroy(cond_t *cv);

Use cond_destroy(3THR) to destroy state associated with the condition variable pointed to by cv. The space for storing the condition variable is not freed. (For POSIX threads, see pthread_condattr_destroy(3THR).)

Wait for a Condition

cond_wait(3THR)

#include <thread.h>

int cond_wait(cond_t *cv, mutex_t *mp);

Use cond_wait(3THR) to atomically release the mutex pointed to by mp and to cause the calling thread to block on the condition variable pointed to by cv. The blocked thread can be awakened by cond_signal(), cond_broadcast(), or when interrupted by delivery of a signal or a fork(). (For POSIX threads, see pthread_cond_wait(3THR).)

Wait for an Absolute Time

cond_timedwait(3THR)

#include <thread.h>

int cond_timedwait(cond_t *cv, mutex_t *mp, timestruct_t abstime);

Use cond_timedwait(3THR) as you would use cond_wait(), except that cond_timedwait() does not block past the time of day specified by abstime. (For POSIX threads, see pthread_cond_timedwait(3THR).)

cond_timedwait() always returns with the mutex locked and owned by the calling thread even when returning an error.

The cond_timedwait() function blocks until the condition is signaled or until the time of day specified by the last argument has passed. The timeout is specified as the time of day so the condition can be retested efficiently without recomputing the time-out value.

Wait for a Time Interval

cond_reltimedwait(3THR)

#include <thread.h>

int cond_reltimedwait(cond_t *cv, mutex_t *mp,
    timestruct_t reltime);

Use cond_reltimedwait(3THR) as you would use cond_timedwait(), except that cond_reltimedwait() takes a relative time interval value in its third argument rather than an absolute time of day value. (For POSIX threads see, pthread_cond_reltimedwait_np(3THR).

cond_reltimedwait() always returns with the mutex locked and owned by the calling thread even when returning an error. The cond_reltimedwait() function blocks until the condition is signaled or until the time interval specified by the last argument has elapsed.

Unblock One Thread

cond_signal(3THR)

#include <thread.h>

int cond_signal(cond_t *cv);

Use cond_signal(3THR) to unblock one thread that is blocked on the condition variable pointed to by cv. Call this function under protection of the same mutex used with the condition variable being signaled. Otherwise, the condition could be signaled between its test and cond_wait(), causing an infinite wait.

Unblock All Threads

cond_broadcast(3THR)

#include <thread.h>

int cond_broadcast(cond_t *cv);

Use cond_broadcast(3THR) to unblock all threads that are blocked on the condition variable pointed to by cv. When no threads are blocked on the condition variable then cond_broadcast() has no effect.