pthread_cond_init(3)

๐Ÿ’… pthread_cond_init(3) โ€” bestie, it's the condition variable page ๐Ÿ’–

okay so this is from the Library Functions Manual, and it's all about pthread_cond_init and its whole friend group, fr fr.


โœจ NAME โœจ

pthread_cond_init, pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait, pthread_cond_destroy โ€” operations on conditions ๐ŸŽ€


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <pthread.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int pthread_cond_init(pthread_cond_t *cond,
                      pthread_condattr_t *cond_attr);
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(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_destroy(pthread_cond_t *cond);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

A condition (short for "condition variable", iykyk) is a synchronization device. The whole point is that it lets threads suspend execution and relinquish the processors until some predicate on shared data is satisfied. The basic ops on conditions are:

  • signal the condition โ€” that's what you do when the predicate becomes true
  • wait for the condition โ€” that suspends the thread's execution until another thread signals the condition

A condition variable must ALWAYS be associated with a mutex, okay? That's non-negotiable. Why? To avoid the race condition where one thread is getting ready to wait on a condition variable and another thread signals the condition just before the first thread actually waits on it. ๐Ÿ˜ญ Classic chaos.

pthread_cond_init() initializes the condition variable cond, using the condition attributes specified in cond_attr, or default attributes if cond_attr is NULL. And here's the tea: the LinuxThreads implementation supports no attributes for conditions, so the cond_attr parameter is actually ignored. ๐Ÿ™ˆ

Variables of type pthread_cond_t can also be initialized statically, using the constant PTHREAD_COND_INITIALIZER. So cute, so convenient. โœจ

pthread_cond_signal() restarts one of the threads that are waiting on the condition variable cond. If no threads are waiting on cond, nothing happens. If several threads are waiting on cond, exactly one is restarted โ€” but it is not specified which. So don't go building your whole personality around knowing which one, bestie. ๐Ÿ’€

pthread_cond_broadcast() restarts all the threads that are waiting on the condition variable cond. Nothing happens if no threads are waiting on cond.

pthread_cond_wait() atomically unlocks the mutex (as per pthread_mutex_unlock()) and waits for the condition variable cond to be signaled. The thread execution is suspended and does not consume any CPU time until the condition variable is signaled. ๐Ÿ‘‘ The mutex must be locked by the calling thread on entrance to pthread_cond_wait(). Before returning to the calling thread, pthread_cond_wait() re-acquires mutex (as per pthread_mutex_lock()).

Unlocking the mutex and suspending on the condition variable is done atomically. So: if all threads always acquire the mutex before signaling the condition, this guarantees that the condition cannot be signaled (and thus ignored) between the time a thread locks the mutex and the time it waits on the condition variable. Check the CAVEATS below for more. ๐Ÿซถ

pthread_cond_timedwait() atomically unlocks mutex and waits on cond, exactly like pthread_cond_wait() does, but it also bounds the duration of the wait. If cond has not been signaled within the amount of time specified by abstime, the mutex mutex is re-acquired and pthread_cond_timedwait() returns the error ETIMEDOUT. The abstime parameter specifies an absolute time, with the same origin as time(2) and gettimeofday(2): an abstime of 0 corresponds to 00:00:00 GMT, January 1, 1970. Yes bestie, the epoch. โญ

pthread_cond_destroy() destroys a condition variable, freeing the resources it might hold. No threads must be waiting on the condition variable on entrance to pthread_cond_destroy(). ๐Ÿšจ In the LinuxThreads implementation, no resources are associated with condition variables, so pthread_cond_destroy() actually does nothing except checking that the condition has no waiting threads.


๐ŸŽ€ CANCELLATION ๐ŸŽ€

pthread_cond_wait() and pthread_cond_timedwait() are cancelation points. If a thread is cancelled while suspended in one of these functions, the thread immediately resumes execution, then locks again the mutex argument to pthread_cond_wait() and pthread_cond_timedwait(), and finally executes the cancelation. Consequently, cleanup handlers are assured that mutex is locked when they are called. ๐Ÿ’œ


๐ŸŒˆ ASYNC-SIGNAL SAFETY ๐ŸŒˆ

The condition functions are not async-signal safe, and should not be called from a signal handler. In particular, calling pthread_cond_signal() or pthread_cond_broadcast() from a signal handler may deadlock the calling thread. ๐Ÿ˜ญ Not the vibe. Please don't.


โญ RETURN VALUE โญ

All condition variable functions return 0 on success and a non-zero error code on error.


๐Ÿ”ฅ ERRORS ๐Ÿ”ฅ

pthread_cond_init(), pthread_cond_signal(), pthread_cond_broadcast(), and pthread_cond_wait() never return an error code. Slay, zero drama. ๐Ÿ’…

The pthread_cond_timedwait() function returns the following error codes on error:

  • ETIMEDOUT โ€” The condition variable was not signaled until the timeout specified by abstime.

The pthread_cond_destroy() function returns the following error code on error:

  • EBUSY โ€” Some threads are currently waiting on cond.

๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘


๐Ÿ’€ CAVEATS ๐Ÿ’€

The implementation of the provided functions until glibc 2.25 used an internal data lock. This lock did not support priority-inheritance and was subject to unbounded priority inversion, visible on a real-time system. ๐Ÿ˜ฌ

After the rewrite of the implementation in glibc 2.25 the usage of internal lock changed. The internal lock is always acquired by the signaling functions pthread_cond_signal() and pthread_cond_broadcast(). The waiting function acquires the lock if the waiting process was interrupted. The interruption can be caused for instance by a specified timeout, and denoted by the error value ETIMEDOUTA, or by a received signal, which is denoted by the error value EINTR.


๐Ÿงš EXAMPLE ๐Ÿงš

Consider two shared variables x and y, protected by the mutex mut, and a condition variable cond that is to be signaled whenever x becomes greater than y.

int x,y;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

Waiting until x is greater than y is performed as follows:

pthread_mutex_lock(&mut);
while (x <= y) {
        pthread_cond_wait(&cond, &mut);
}
/* operate on x and y */
pthread_mutex_unlock(&mut);

Modifications on x and y that may cause x to become greater than y should signal the condition if needed:

pthread_mutex_lock(&mut);
/* modify x and y */
if (x > y) pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mut);

If it can be proved that at most one waiting thread needs to be waken up (for instance, if there are only two threads communicating through x and y), pthread_cond_signal() can be used as a slightly more efficient alternative to pthread_cond_broadcast(). In doubt, use pthread_cond_broadcast(). ๐Ÿซถ

To wait for x to become greater than y with a timeout of 5 seconds, do:

struct timeval now;
struct timespec timeout;
int retcode;

pthread_mutex_lock(&mut);
gettimeofday(&now);
timeout.tv_sec = now.tv_sec + 5;
timeout.tv_nsec = now.tv_usec * 1000;
retcode = 0;
while (x <= y && retcode != ETIMEDOUT) {
        retcode = pthread_cond_timedwait(&cond, &mut, &timeout);
}
if (retcode == ETIMEDOUT) {
        /* timeout occurred */
} else {
        /* operate on x and y */
}
pthread_mutex_unlock(&mut);

and that's the whole page, bestie!! ๐ŸŽ€โœจ this one's from Linux man-pages 6.20, dated 2026-03-22, and it's pthread_cond_init(3) โ€” go forth and signal responsibly, mwah ๐Ÿ’–๐Ÿฆ‹๐ŸŒธ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms