PTHREAD_COND_DESTROY(3P)
π PTHREAD_COND_DESTROY(3P) β the POSIX Programmer's Manual one, bestie π
okay so before we even start: this page lives in the POSIX Programmer's Manual, and the Linux implementation of this interface may be different from what POSIX says (so go check the matching Linux man page for the deets), or honestly the interface might not even be implemented on Linux at all. π
β¨ PROLOG β¨
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
literally the same thing I just said, but now it's official π
π NAME π
pthread_cond_destroy, pthread_cond_init β destroy and initialize condition variables. that's it, that's the whole vibe. π¦
πΈ SYNOPSIS πΈ
#include <pthread.h>
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr);
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
π DESCRIPTION π
so pthread_cond_destroy() shall destroy the condition variable you hand it via cond, and after that the object is basically uninitialized. an implementation is allowed to set the thing cond points at to an invalid value. a destroyed condition variable can be brought back to life with pthread_cond_init(), but if you reference the object any other way after destroying it, the results are undefined. π no cap.
it shall be safe to destroy an initialized condition variable that has no threads currently blocked on it. but trying to destroy a condition variable while other threads are still blocked on it? that's undefined behavior, bestie. don't do it. π
pthread_cond_init() shall initialize the condition variable cond points at, using the attributes that attr points at. if attr is NULL, the default condition variable attributes get used β the effect is the same as passing the address of a default condition variable attributes object. once it initializes successfully, the condition variable's state becomes initialized. β¨
also see Section 2.9.9, Synchronization Object Copies and Alternative Mappings, for the further requirements. iykyk.
trying to initialize an already-initialized condition variable? undefined behavior. periodt.
when the default condition variable attributes are what you want, you can use the macro PTHREAD_COND_INITIALIZER to initialize condition variables. its effect shall be equivalent to dynamic initialization via a call to pthread_cond_init() with the attr parameter set to NULL β except that no error checks are performed. π
the behavior is undefined if the value you pass as the cond argument to pthread_cond_destroy() doesn't refer to an initialized condition variable.
and the behavior is undefined if the value you pass as the attr argument to pthread_cond_init() doesn't refer to an initialized condition variable attributes object.
π₯ RETURN VALUE π₯
if everything goes well, pthread_cond_destroy() and pthread_cond_init() shall return zero. otherwise, an error number shall be returned to tell you what went wrong. simple as. π«Ά
π ERRORS π
the pthread_cond_init() function shall fail if:
EAGAINβ the system lacked the necessary resources (other than memory) to initialize another condition variable. πENOMEMβ insufficient memory exists to initialize the condition variable. π
these functions shall not return an error code of [EINTR]. nope, never, not happening. β
the following sections are informative (aka: lore, not law).
π EXAMPLES π
you can destroy a condition variable immediately after all the threads blocked on it are awakened. for example, consider this code:
struct list {
pthread_mutex_t lm;
...
}
struct elt {
key k;
int busy;
pthread_cond_t notbusy;
...
}
/* Find a list element and reserve it. */
struct elt *
list_find(struct list *lp, key k)
{
struct elt *ep;
pthread_mutex_lock(&lp->lm);
while ((ep = find_elt(l, k) != NULL) && ep->busy)
pthread_cond_wait(&ep->notbusy, &lp->lm);
if (ep != NULL)
ep->busy = 1;
pthread_mutex_unlock(&lp->lm);
return(ep);
}
delete_elt(struct list *lp, struct elt *ep)
{
pthread_mutex_lock(&lp->lm);
assert(ep->busy);
... remove ep from list ...
ep->busy = 0; /* Paranoid. */
(A) pthread_cond_broadcast(&ep->notbusy);
pthread_mutex_unlock(&lp->lm);
(B) pthread_cond_destroy(&ep->notbusy);
free(ep);
}
in this example, the condition variable and its list element can be freed (that's line B) right after all the threads waiting for it get awakened (that's line A), because the mutex and the code make sure no other thread can touch the element that's getting deleted. chef's kiss, honestly. π©βπ³π
π§ APPLICATION USAGE π§
None.
yeah bestie, there's literally nothing here. iconic of them. π
β RATIONALE β
if an implementation notices that the value given as the cond argument to pthread_cond_destroy() doesn't refer to an initialized condition variable, it's recommended that the function should fail and report an [EINVAL] error.
if an implementation notices that the value given as the cond argument to pthread_cond_destroy() or pthread_cond_init() refers to a condition variable that's in use by another thread (like, say, inside a pthread_cond_wait() call) β or notices that the value given as the cond argument to pthread_cond_init() refers to an already initialized condition variable β it's recommended that the function should fail and report an [EBUSY] error.
if an implementation notices that the value given as the attr argument to pthread_cond_init() doesn't refer to an initialized condition variable attributes object, it's recommended that the function should fail and report an [EINVAL] error.
see also pthread_mutex_destroy().
π¦ FUTURE DIRECTIONS π¦
None.
again. nothing. the future is a blank canvas, babe. β¨
π SEE ALSO π
pthread_cond_broadcast()pthread_cond_timedwait()pthread_mutex_destroy()
the Base Definitions volume of POSIX.1β2017, <pthread.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! πβ¨ππ¦πΈπ π«Ά this lil bop of a document is brought to you by IEEE/The Open Group, dated 2017 β mwah, love that for us. go forth and initialize your condition variables responsibly, and remember: no destroying them while threads are still blocked. periodt. ππ
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms