PTHREAD...DESTROY(3POSIX)
π PTHREAD...DESTROY(3POSIX) π
okay bestie, welcome to the POSIX Programmer's Manual β this page is literally about destroying and initializing condition variables and it's giving main character energy fr fr. π
πΈ PROLOG πΈ
okay so first things first, quick heads up: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
pthread_cond_destroy, pthread_cond_init β destroy and initialize condition variables π
π¦ 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 given condition variable specified by cond. After that, the object becomes, in effect, uninitialized. An implementation may cause pthread_cond_destroy() to set the object referenced by cond to an invalid value. A destroyed condition variable object can be reinitialized using pthread_cond_init() β but the results of otherwise referencing the object after it has been destroyed are undefined. π«
It shall be safe to destroy an initialized condition variable upon which no threads are currently blocked. But attempting to destroy a condition variable upon which other threads are currently blocked results in undefined behavior. periodt.
The pthread_cond_init() function shall initialize the condition variable referenced by cond with attributes referenced by attr. If attr is NULL, the default condition variable attributes shall be used β the effect is the same as passing the address of a default condition variable attributes object. Upon successful initialization, the state of the condition variable shall become initialized. β¨
See Section 2.9.9, Synchronization Object Copies and Alternative Mappings for further requirements, bestie β yes it's required reading, sorry not sorry. π
Attempting to initialize an already initialized condition variable results in undefined behavior. π
In cases where default condition variable attributes are appropriate, the macro PTHREAD_COND_INITIALIZER can be used to initialize condition variables. The effect shall be equivalent to dynamic initialization by a call to pthread_cond_init() with parameter attr specified as NULL, except that no error checks are performed. cute, convenient, no notes. π
The behavior is undefined if the value specified by the cond argument to pthread_cond_destroy() does not refer to an initialized condition variable.
The behavior is undefined if the value specified by the attr argument to pthread_cond_init() does not refer to an initialized condition variable attributes object.
π«Ά RETURN VALUE π«Ά
If successful, the pthread_cond_destroy() and pthread_cond_init() functions shall return zero; otherwise, an error number shall be returned to indicate the error. slay.
π 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]. not me checking for EINTR and getting nothing back π
The following sections are informative.
π EXAMPLES π
A condition variable can be destroyed immediately after all the threads that are blocked on it are awakened. For example, consider the following code β this is the good stuff, iykyk:
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 may be freed (line B) immediately after all threads waiting for it are awakened (line A), since the mutex and the code ensure that no other thread can touch the element to be deleted. iconic. π
π APPLICATION USAGE π
None. literally nothing, we love a chill section. π
β RATIONALE β
If an implementation detects that the value specified by the cond argument to pthread_cond_destroy() does not refer to an initialized condition variable, it is recommended that the function should fail and report an [EINVAL] error.
If an implementation detects that the value specified by the cond argument to pthread_cond_destroy() or pthread_cond_init() refers to a condition variable that is in use (for example, in a pthread_cond_wait() call) by another thread, or detects that the value specified by the cond argument to pthread_cond_init() refers to an already initialized condition variable, it is recommended that the function should fail and report an [EBUSY] error.
If an implementation detects that the value specified by the attr argument to pthread_cond_init() does not refer to an initialized condition variable attributes object, it is recommended that the function should fail and report an [EINVAL] error.
See also pthread_mutex_destroy().
πΈ FUTURE DIRECTIONS πΈ
None. π€
π¦ 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 was brought to you by IEEE/The Open Group, 2017, living in the footer of PTHREAD...DESTROY(3POSIX) β go forth and destroy your condition variables responsibly π
β¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms