1PTHREAD_COND_DESTROY(P) POSIX Programmer's Manual PTHREAD_COND_DESTROY(P)
2
3
4
6 pthread_cond_destroy, pthread_cond_init - destroy and initialize condi‐
7 tion variables
8
10 #include <pthread.h>
11
12 int pthread_cond_destroy(pthread_cond_t *cond);
13 int pthread_cond_init(pthread_cond_t *restrict cond,
14 const pthread_condattr_t *restrict attr);
15 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
16
17
19 The pthread_cond_destroy() function shall destroy the given condition
20 variable specified by cond; the object becomes, in effect, uninitial‐
21 ized. An implementation may cause pthread_cond_destroy() to set the
22 object referenced by cond to an invalid value. A destroyed condition
23 variable object can be reinitialized using pthread_cond_init(); the
24 results of otherwise referencing the object after it has been destroyed
25 are undefined.
26
27 It shall be safe to destroy an initialized condition variable upon
28 which no threads are currently blocked. Attempting to destroy a condi‐
29 tion variable upon which other threads are currently blocked results in
30 undefined behavior.
31
32 The pthread_cond_init() function shall initialize the condition vari‐
33 able referenced by cond with attributes referenced by attr. If attr is
34 NULL, the default condition variable attributes shall be used; the
35 effect is the same as passing the address of a default condition vari‐
36 able attributes object. Upon successful initialization, the state of
37 the condition variable shall become initialized.
38
39 Only cond itself may be used for performing synchronization. The
40 result of referring to copies of cond in calls to pthread_cond_wait(),
41 pthread_cond_timedwait(), pthread_cond_signal(), pthread_cond_broad‐
42 cast(), and pthread_cond_destroy() is undefined.
43
44 Attempting to initialize an already initialized condition variable
45 results in undefined behavior.
46
47 In cases where default condition variable attributes are appropriate,
48 the macro PTHREAD_COND_INITIALIZER can be used to initialize condition
49 variables that are statically allocated. The effect shall be equivalent
50 to dynamic initialization by a call to pthread_cond_init() with parame‐
51 ter attr specified as NULL, except that no error checks are performed.
52
54 If successful, the pthread_cond_destroy() and pthread_cond_init() func‐
55 tions shall return zero; otherwise, an error number shall be returned
56 to indicate the error.
57
58 The [EBUSY] and [EINVAL] error checks, if implemented, shall act as if
59 they were performed immediately at the beginning of processing for the
60 function and caused an error return prior to modifying the state of the
61 condition variable specified by cond.
62
64 The pthread_cond_destroy() function may fail if:
65
66 EBUSY The implementation has detected an attempt to destroy the object
67 referenced by cond while it is referenced (for example, while
68 being used in a pthread_cond_wait() or pthread_cond_timedwait())
69 by another thread.
70
71 EINVAL The value specified by cond is invalid.
72
73
74 The pthread_cond_init() function shall fail if:
75
76 EAGAIN The system lacked the necessary resources (other than memory) to
77 initialize another condition variable.
78
79 ENOMEM Insufficient memory exists to initialize the condition variable.
80
81
82 The pthread_cond_init() function may fail if:
83
84 EBUSY The implementation has detected an attempt to reinitialize the
85 object referenced by cond, a previously initialized, but not yet
86 destroyed, condition variable.
87
88 EINVAL The value specified by attr is invalid.
89
90
91 These functions shall not return an error code of [EINTR].
92
93 The following sections are informative.
94
96 A condition variable can be destroyed immediately after all the threads
97 that are blocked on it are awakened. For example, consider the follow‐
98 ing code:
99
100
101 struct list {
102 pthread_mutex_t lm;
103 ...
104 }
105
106
107 struct elt {
108 key k;
109 int busy;
110 pthread_cond_t notbusy;
111 ...
112 }
113
114
115 /* Find a list element and reserve it. */
116 struct elt *
117 list_find(struct list *lp, key k)
118 {
119 struct elt *ep;
120
121
122 pthread_mutex_lock(&lp->lm);
123 while ((ep = find_elt(l, k) != NULL) && ep->busy)
124 pthread_cond_wait(&ep->notbusy, &lp->lm);
125 if (ep != NULL)
126 ep->busy = 1;
127 pthread_mutex_unlock(&lp->lm);
128 return(ep);
129 }
130
131
132 delete_elt(struct list *lp, struct elt *ep)
133 {
134 pthread_mutex_lock(&lp->lm);
135 assert(ep->busy);
136 ... remove ep from list ...
137 ep->busy = 0; /* Paranoid. */
138 (A) pthread_cond_broadcast(&ep->notbusy);
139 pthread_mutex_unlock(&lp->lm);
140 (B) pthread_cond_destroy(&rp->notbusy);
141 free(ep);
142 }
143
144 In this example, the condition variable and its list element may be
145 freed (line B) immediately after all threads waiting for it are awak‐
146 ened (line A), since the mutex and the code ensure that no other thread
147 can touch the element to be deleted.
148
150 None.
151
153 See pthread_mutex_init() ; a similar rationale applies to condition
154 variables.
155
157 None.
158
160 pthread_cond_broadcast() , pthread_cond_signal() , pthread_cond_timed‐
161 wait() , the Base Definitions volume of IEEE Std 1003.1-2001,
162 <pthread.h>
163
165 Portions of this text are reprinted and reproduced in electronic form
166 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
167 -- Portable Operating System Interface (POSIX), The Open Group Base
168 Specifications Issue 6, Copyright (C) 2001-2003 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
176
177IEEE/The Open Group 2003 PTHREAD_COND_DESTROY(P)