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