1PTHREAD_COND_DESTROY(3P) POSIX Programmer's Manual PTHREAD_COND_DESTROY(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 pthread_cond_destroy, pthread_cond_init — destroy and initialize condi‐
13 tion variables
14
16 #include <pthread.h>
17
18 int pthread_cond_destroy(pthread_cond_t *cond);
19 int pthread_cond_init(pthread_cond_t *restrict cond,
20 const pthread_condattr_t *restrict attr);
21 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
22
24 The pthread_cond_destroy() function shall destroy the given condition
25 variable specified by cond; the object becomes, in effect, uninitial‐
26 ized. An implementation may cause pthread_cond_destroy() to set the
27 object referenced by cond to an invalid value. A destroyed condition
28 variable object can be reinitialized using pthread_cond_init(); the
29 results of otherwise referencing the object after it has been destroyed
30 are undefined.
31
32 It shall be safe to destroy an initialized condition variable upon
33 which no threads are currently blocked. Attempting to destroy a condi‐
34 tion variable upon which other threads are currently blocked results in
35 undefined behavior.
36
37 The pthread_cond_init() function shall initialize the condition vari‐
38 able referenced by cond with attributes referenced by attr. If attr is
39 NULL, the default condition variable attributes shall be used; the
40 effect is the same as passing the address of a default condition vari‐
41 able attributes object. Upon successful initialization, the state of
42 the condition variable shall become initialized.
43
44 See Section 2.9.9, Synchronization Object Copies and Alternative Map‐
45 pings for further requirements.
46
47 Attempting to initialize an already initialized condition variable
48 results in undefined behavior.
49
50 In cases where default condition variable attributes are appropriate,
51 the macro PTHREAD_COND_INITIALIZER can be used to initialize condition
52 variables. The effect shall be equivalent to dynamic initialization by
53 a call to pthread_cond_init() with parameter attr specified as NULL,
54 except that no error checks are performed.
55
56 The behavior is undefined if the value specified by the cond argument
57 to pthread_cond_destroy() does not refer to an initialized condition
58 variable.
59
60 The behavior is undefined if the value specified by the attr argument
61 to pthread_cond_init() does not refer to an initialized condition vari‐
62 able attributes object.
63
65 If successful, the pthread_cond_destroy() and pthread_cond_init() func‐
66 tions shall return zero; otherwise, an error number shall be returned
67 to indicate the error.
68
70 The pthread_cond_init() function shall fail if:
71
72 EAGAIN The system lacked the necessary resources (other than memory) to
73 initialize another condition variable.
74
75 ENOMEM Insufficient memory exists to initialize the condition variable.
76
77 These functions shall not return an error code of [EINTR].
78
79 The following sections are informative.
80
82 A condition variable can be destroyed immediately after all the threads
83 that are blocked on it are awakened. For example, consider the follow‐
84 ing code:
85
86
87 struct list {
88 pthread_mutex_t lm;
89 ...
90 }
91
92 struct elt {
93 key k;
94 int busy;
95 pthread_cond_t notbusy;
96 ...
97 }
98
99 /* Find a list element and reserve it. */
100 struct elt *
101 list_find(struct list *lp, key k)
102 {
103 struct elt *ep;
104
105 pthread_mutex_lock(&lp->lm);
106 while ((ep = find_elt(l, k) != NULL) && ep->busy)
107 pthread_cond_wait(&ep->notbusy, &lp->lm);
108 if (ep != NULL)
109 ep->busy = 1;
110 pthread_mutex_unlock(&lp->lm);
111 return(ep);
112 }
113
114 delete_elt(struct list *lp, struct elt *ep)
115 {
116 pthread_mutex_lock(&lp->lm);
117 assert(ep->busy);
118 ... remove ep from list ...
119 ep->busy = 0; /* Paranoid. */
120 (A) pthread_cond_broadcast(&ep->notbusy);
121 pthread_mutex_unlock(&lp->lm);
122 (B) pthread_cond_destroy(&ep->notbusy);
123 free(ep);
124 }
125
126 In this example, the condition variable and its list element may be
127 freed (line B) immediately after all threads waiting for it are awak‐
128 ened (line A), since the mutex and the code ensure that no other thread
129 can touch the element to be deleted.
130
132 None.
133
135 If an implementation detects that the value specified by the cond argu‐
136 ment to pthread_cond_destroy() does not refer to an initialized condi‐
137 tion variable, it is recommended that the function should fail and
138 report an [EINVAL] error.
139
140 If an implementation detects that the value specified by the cond argu‐
141 ment to pthread_cond_destroy() or pthread_cond_init() refers to a con‐
142 dition variable that is in use (for example, in a pthread_cond_wait()
143 call) by another thread, or detects that the value specified by the
144 cond argument to pthread_cond_init() refers to an already initialized
145 condition variable, it is recommended that the function should fail and
146 report an [EBUSY] error.
147
148 If an implementation detects that the value specified by the attr argu‐
149 ment to pthread_cond_init() does not refer to an initialized condition
150 variable attributes object, it is recommended that the function should
151 fail and report an [EINVAL] error.
152
153 See also pthread_mutex_destroy().
154
156 None.
157
159 pthread_cond_broadcast(), pthread_cond_timedwait(),
160 pthread_mutex_destroy()
161
162 The Base Definitions volume of POSIX.1‐2017, <pthread.h>
163
165 Portions of this text are reprinted and reproduced in electronic form
166 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
167 table Operating System Interface (POSIX), The Open Group Base Specifi‐
168 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
169 Electrical and Electronics Engineers, Inc and The Open Group. In the
170 event of any discrepancy between this version and the original IEEE and
171 The Open Group Standard, the original IEEE and The Open Group Standard
172 is the referee document. The original Standard can be obtained online
173 at http://www.opengroup.org/unix/online.html .
174
175 Any typographical or formatting errors that appear in this page are
176 most likely to have been introduced during the conversion of the source
177 files to man page format. To report such errors, see https://www.ker‐
178 nel.org/doc/man-pages/reporting_bugs.html .
179
180
181
182IEEE/The Open Group 2017 PTHREAD_COND_DESTROY(3P)