pthread_cond_wait or pthread_cond_timedwait Subroutine

Purpose

Blocks the calling thread on a condition.

Library

Threads Library (libpthreads.a)

Syntax

#include <pthread.h>

int pthread_cond_wait (cond, mutex)
pthread_cond_t *cond;
pthread_mutex_t *mutex;

int pthread_cond_timedwait (cond, mutex, timeout)
pthread_cond_t *cond;
pthread_mutex_t *mutex;
const struct timespec *timeout;

Description

The pthread_cond_wait and pthread_cond_timedwait functions are used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behavior will result.

These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means atomically with respect to access by another thread to the mutex and then the condition variable. That is, if another thread is able to acquire the mutex after the about-to-block thread releases it, then a subsequent call to pthread_cond_signal or pthread_cond_broadcast in that thread behaves as if it were issued after the about-to-block thread has blocked.

Upon successful return, the mutex is locked and owned by the calling thread.

When you use condition variables, there is always a Boolean predicate involving shared variable that is associated with each condition wait that is true if the thread must proceed. Spurious wakeups from the pthread_cond_wait or pthread_cond_timedwait functions might occur. Since the return from pthread_cond_wait or pthread_cond_timedwait does not imply anything about the value of this predicate, the predicate must be reevaluated upon such return.

The effect of using more than one mutex for concurrent pthread_cond_wait or pthread_cond_timedwait operations on the same condition variable is undefined; that is, a condition variable becomes bound to a unique mutex when a thread waits on the condition variable, and this (dynamic) binding ends when the wait returns.

A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) reacquired before calling the first cancellation cleanup handler. The effect is as if the thread were unblocked, allowed to execute up to the point of returning from the call to pthread_cond_wait or pthread_cond_timedwait, but at that point notices the cancellation request and instead of returning to the caller of pthread_cond_wait or pthread_cond_timedwait, starts the thread cancellation activities, which include calling cancellation cleanup handlers.

A thread that is unblocked because it is canceled while blocked in a call to pthread_cond_wait or pthread_cond_timedwait does not consume any condition signal that may be directed concurrently at the condition variable if there are other threads blocked on the condition variable.

The pthread_cond_timedwait function is the same as pthread_cond_wait except that an error is returned if the absolute time specified by timeout passes (that is, system time equals or exceeds timeout) before the condition cond is signaled or broadcast, or if the absolute time that is specified by timeout has already been passed at the time of the call. When such time-outs occur, pthread_cond_timedwait releases the mutex and reacquires the mutex referenced by mutex. The function pthread_cond_timedwait is also a cancellation point. The absolute time that is specified by timeout can be either based on the system realtime clock or the system monotonic clock. The reference clock for the condition variable is set by calling pthread_condattr_setclock before its initialization with the corresponding condition attributes object.

If a signal is delivered to a thread they is waiting for a condition variable, upon return from the signal handler the thread resumes waiting for the condition variable as if it was not interrupted, or it returns zero due to spurious wakeup.

Parameters

Item Description
cond Specifies the condition variable to wait on.
mutex Specifies the mutex that is used to protect the condition variable. The mutex must be locked when the subroutine is called.
timeout Points to the absolute time structure that is specifying the blocked state timeout.

Return Values

Except if ETIMEDOUT, all these error checks act as if they were performed immediately at the beginning of processing for the function and cause an error return, in effect, before modifying the state of the mutex specified by mutex or the condition variable specified by cond.

Upon successful completion, a value of zero is returned. Otherwise, an error number is returned to indicate the error.

Error Codes

The pthread_cond_timedwait function fails if:

Item Description
ETIMEDOUT The time specified by timeout to pthread_cond_timedwait has passed.

The pthread_cond_wait and pthread_cond_timedwait subroutines fail if the following error codes are returned:

Item Description
EINVAL The value specified by cond, mutex, or timeout is invalid.
EINVAL Different mutexes were supplied for concurrent pthread_cond_wait or pthread_cond_timedwait operations on the same condition variable.
EINVAL The mutex was not owned by the current thread at the time of the call.
EPERM The mutex was not owned by the current thread at the time of the call, XPG_SUS_ENV is set to ON, and XPG_UNIX98 is not set.
ENOTRECOVERABLE The protected state of the mutex cannot be recovered.
EOWNERDEAD The mutex is a robust mutex, and the process of the thread that owns the mutex terminated while holding the mutex lock.

These subroutines do not return an EINTR error code.