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
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
46 result 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 that are statically allocated. The effect shall be equivalent
56 to dynamic initialization by a call to pthread_cond_init() with parame‐
57 ter attr specified as NULL, except that no error checks are performed.
58
60 If successful, the pthread_cond_destroy() and pthread_cond_init() func‐
61 tions shall return zero; otherwise, an error number shall be returned
62 to indicate the error.
63
64 The [EBUSY] and [EINVAL] error checks, if implemented, shall act as if
65 they were performed immediately at the beginning of processing for the
66 function and caused an error return prior to modifying the state of the
67 condition variable specified by cond.
68
70 The pthread_cond_destroy() function may fail if:
71
72 EBUSY The implementation has detected an attempt to destroy the object
73 referenced by cond while it is referenced (for example, while
74 being used in a pthread_cond_wait() or pthread_cond_timedwait())
75 by another thread.
76
77 EINVAL The value specified by cond is invalid.
78
79
80 The pthread_cond_init() function shall fail if:
81
82 EAGAIN The system lacked the necessary resources (other than memory) to
83 initialize another condition variable.
84
85 ENOMEM Insufficient memory exists to initialize the condition variable.
86
87
88 The pthread_cond_init() function may fail if:
89
90 EBUSY The implementation has detected an attempt to reinitialize the
91 object referenced by cond, a previously initialized, but not yet
92 destroyed, condition variable.
93
94 EINVAL The value specified by attr is invalid.
95
96
97 These functions shall not return an error code of [EINTR].
98
99 The following sections are informative.
100
102 A condition variable can be destroyed immediately after all the threads
103 that are blocked on it are awakened. For example, consider the follow‐
104 ing code:
105
106
107 struct list {
108 pthread_mutex_t lm;
109 ...
110 }
111
112
113 struct elt {
114 key k;
115 int busy;
116 pthread_cond_t notbusy;
117 ...
118 }
119
120
121 /* Find a list element and reserve it. */
122 struct elt *
123 list_find(struct list *lp, key k)
124 {
125 struct elt *ep;
126
127
128 pthread_mutex_lock(&lp->lm);
129 while ((ep = find_elt(l, k) != NULL) && ep->busy)
130 pthread_cond_wait(&ep->notbusy, &lp->lm);
131 if (ep != NULL)
132 ep->busy = 1;
133 pthread_mutex_unlock(&lp->lm);
134 return(ep);
135 }
136
137
138 delete_elt(struct list *lp, struct elt *ep)
139 {
140 pthread_mutex_lock(&lp->lm);
141 assert(ep->busy);
142 ... remove ep from list ...
143 ep->busy = 0; /* Paranoid. */
144 (A) pthread_cond_broadcast(&ep->notbusy);
145 pthread_mutex_unlock(&lp->lm);
146 (B) pthread_cond_destroy(&rp->notbusy);
147 free(ep);
148 }
149
150 In this example, the condition variable and its list element may be
151 freed (line B) immediately after all threads waiting for it are awak‐
152 ened (line A), since the mutex and the code ensure that no other thread
153 can touch the element to be deleted.
154
156 None.
157
159 See pthread_mutex_init(); a similar rationale applies to condition
160 variables.
161
163 None.
164
166 pthread_cond_broadcast(), pthread_cond_signal(), pthread_cond_timed‐
167 wait(), the Base Definitions volume of IEEE Std 1003.1-2001,
168 <pthread.h>
169
171 Portions of this text are reprinted and reproduced in electronic form
172 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
173 -- Portable Operating System Interface (POSIX), The Open Group Base
174 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
175 Electrical and Electronics Engineers, Inc and The Open Group. In the
176 event of any discrepancy between this version and the original IEEE and
177 The Open Group Standard, the original IEEE and The Open Group Standard
178 is the referee document. The original Standard can be obtained online
179 at http://www.opengroup.org/unix/online.html .
180
181
182
183IEEE/The Open Group 2003 PTHREAD_COND_DESTROY(3P)