OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / glibc-linuxthreads / original / man3 / pthread_cond_init.3
1 .TH PTHREAD_COND 3 LinuxThreads
2
3
4 .SH NAME
5 pthread_cond_init, pthread_cond_destroy, pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait \- operations on conditions
6
7 .SH SYNOPSIS
8 .B #include <pthread.h>
9
10 .BI "pthread_cond_t " cond " = PTHREAD_COND_INITIALIZER;"
11
12 .BI "int pthread_cond_init(pthread_cond_t *" cond ", pthread_condattr_t *" cond_attr ");"
13
14 .BI "int pthread_cond_signal(pthread_cond_t *" cond ");"
15
16 .BI "int pthread_cond_broadcast(pthread_cond_t *" cond ");"
17
18 .BI "int pthread_cond_wait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ");"
19
20 .BI "int pthread_cond_timedwait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ", const struct timespec *" abstime ");"
21
22 .BI "int pthread_cond_destroy(pthread_cond_t *" cond ");"
23
24 .SH DESCRIPTION
25
26 A condition (short for ``condition variable'') is a synchronization
27 device that allows threads to suspend execution and relinquish the
28 processors until some predicate on shared data is satisfied. The basic
29 operations on conditions are: signal the condition (when the
30 predicate becomes true), and wait for the condition, suspending the
31 thread execution until another thread signals the condition.
32
33 A condition variable must always be associated with a mutex, to avoid
34 the race condition where a thread prepares to wait on a condition
35 variable and another thread signals the condition just before the
36 first thread actually waits on it.
37
38 .B "pthread_cond_init"
39 initializes the condition variable 
40 .IR "cond" ,
41 using the
42 condition attributes specified in 
43 .IR "cond_attr" ,
44 or default attributes
45 if 
46 .I "cond_attr"
47 is 
48 .BR "NULL" .
49 The LinuxThreads implementation supports no
50 attributes for conditions, hence the 
51 .I "cond_attr"
52 parameter is actually
53 ignored.
54
55 Variables of type 
56 .B "pthread_cond_t"
57 can also be initialized
58 statically, using the constant 
59 .BR "PTHREAD_COND_INITIALIZER" .
60
61 .B "pthread_cond_signal"
62 restarts one of the threads that are waiting on
63 the condition variable 
64 .IR "cond" .
65 If no threads are waiting on 
66 .IR "cond" ,
67 nothing happens. If several threads are waiting on 
68 .IR "cond" ,
69 exactly one
70 is restarted, but it is not specified which.
71
72 .B "pthread_cond_broadcast"
73 restarts all the threads that are waiting on
74 the condition variable 
75 .IR "cond" .
76 Nothing happens if no threads are
77 waiting on 
78 .IR "cond" .
79
80 .B "pthread_cond_wait"
81 atomically unlocks the 
82 .I "mutex"
83 (as per
84 .BR "pthread_unlock_mutex" )
85 and waits for the condition variable 
86 .I "cond"
87 to
88 be signaled. The thread execution is suspended and does not consume
89 any CPU time until the condition variable is signaled. The 
90 .I "mutex"
91 must be locked by the calling thread on entrance to
92 .BR "pthread_cond_wait" .
93 Before returning to the calling thread,
94 .B "pthread_cond_wait"
95 re-acquires 
96 .I "mutex"
97 (as per 
98 .BR "pthread_lock_mutex" ).
99
100 Unlocking the mutex and suspending on the condition variable is done
101 atomically. Thus, if all threads always acquire the mutex before
102 signaling the condition, this guarantees that the condition cannot be
103 signaled (and thus ignored) between the time a thread locks the mutex
104 and the time it waits on the condition variable.
105
106 .B "pthread_cond_timedwait"
107 atomically unlocks 
108 .I "mutex"
109 and waits on
110 .IR "cond" ,
111 as 
112 .B "pthread_cond_wait"
113 does, but it also bounds the duration
114 of the wait. If 
115 .I "cond"
116 has not been signaled within the amount of time
117 specified by 
118 .IR "abstime" ,
119 the mutex 
120 .I "mutex"
121 is re-acquired and
122 .B "pthread_cond_timedwait"
123 returns the error 
124 .BR "ETIMEDOUT" .
125 The 
126 .I "abstime"
127 parameter specifies an absolute time, with the same
128 origin as 
129 .BR "time" (2)
130 and 
131 .BR "gettimeofday" (2):
132 an 
133 .I "abstime"
134 of 0
135 corresponds to 00:00:00 GMT, January 1, 1970.
136
137 .B "pthread_cond_destroy"
138 destroys a condition variable, freeing the
139 resources it might hold. No threads must be waiting on the condition
140 variable on entrance to 
141 .BR "pthread_cond_destroy" .
142 In the LinuxThreads
143 implementation, no resources are associated with condition variables,
144 thus 
145 .B "pthread_cond_destroy"
146 actually does nothing except checking that
147 the condition has no waiting threads.
148
149 .SH CANCELLATION
150
151 .B "pthread_cond_wait"
152 and 
153 .B "pthread_cond_timedwait"
154 are cancellation
155 points. If a thread is cancelled while suspended in one of these
156 functions, the thread immediately resumes execution, then locks again
157 the 
158 .I "mutex"
159 argument to 
160 .B "pthread_cond_wait"
161 and
162 .BR "pthread_cond_timedwait" ,
163 and finally executes the cancellation.
164 Consequently, cleanup handlers are assured that 
165 .I "mutex"
166 is locked when
167 they are called.
168
169 .SH "ASYNC-SIGNAL SAFETY"
170
171 The condition functions are not async-signal safe, and should not be
172 called from a signal handler. In particular, calling
173 .B "pthread_cond_signal"
174 or 
175 .B "pthread_cond_broadcast"
176 from a signal
177 handler may deadlock the calling thread.
178
179 .SH "RETURN VALUE"
180
181 All condition variable functions return 0 on success and a non-zero
182 error code on error.
183
184 .SH ERRORS
185
186 .BR "pthread_cond_init" ,
187 .BR "pthread_cond_signal" ,
188 .BR "pthread_cond_broadcast" ,
189 and 
190 .B "pthread_cond_wait"
191 never return an error code.
192
193 The 
194 .B "pthread_cond_timedwait"
195 function returns the following error codes
196 on error:
197 .RS
198 .TP
199 .B "ETIMEDOUT"
200 the condition variable was not signaled until the timeout specified by
201 .I "abstime"
202
203 .TP
204 .B "EINTR"
205 .B "pthread_cond_timedwait"
206 was interrupted by a signal
207 .RE
208
209 The 
210 .B "pthread_cond_destroy"
211 function returns the following error code
212 on error:
213 .RS
214 .TP
215 .B "EBUSY"
216 some threads are currently waiting on 
217 .IR "cond" .
218 .RE
219
220 .SH AUTHOR
221 Xavier Leroy <Xavier.Leroy@inria.fr>
222
223 .SH "SEE ALSO"
224 .BR "pthread_condattr_init" (3),
225 .BR "pthread_mutex_lock" (3),
226 .BR "pthread_mutex_unlock" (3),
227 .BR "gettimeofday" (2),
228 .BR "nanosleep" (2).
229
230 .SH EXAMPLE
231
232 Consider two shared variables 
233 .I "x"
234 and 
235 .IR "y" ,
236 protected by the mutex 
237 .IR "mut" ,
238 and a condition variable 
239 .I "cond"
240 that is to be signaled whenever 
241 .I "x"
242 becomes greater than 
243 .IR "y" .
244
245 .RS
246 .ft 3
247 .nf
248 .sp
249 int x,y;
250 pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
251 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
252 .ft
253 .LP
254 .RE
255 .fi
256
257 Waiting until 
258 .I "x"
259 is greater than 
260 .I "y"
261 is performed as follows:
262
263 .RS
264 .ft 3
265 .nf
266 .sp
267 pthread_mutex_lock(&mut);
268 while (x <= y) {
269         pthread_cond_wait(&cond, &mut);
270 }
271 /* operate on x and y */
272 pthread_mutex_unlock(&mut);
273 .ft
274 .LP
275 .RE
276 .fi
277
278 Modifications on 
279 .I "x"
280 and 
281 .I "y"
282 that may cause 
283 .I "x"
284 to become greater than
285 .I "y"
286 should signal the condition if needed:
287
288 .RS
289 .ft 3
290 .nf
291 .sp
292 pthread_mutex_lock(&mut);
293 /* modify x and y */
294 if (x > y) pthread_cond_broadcast(&cond);
295 pthread_mutex_unlock(&mut);
296 .ft
297 .LP
298 .RE
299 .fi
300
301 If it can be proved that at most one waiting thread needs to be waken
302 up (for instance, if there are only two threads communicating through
303 .I "x"
304 and 
305 .IR "y" ),
306 .B "pthread_cond_signal"
307 can be used as a slightly more
308 efficient alternative to 
309 .BR "pthread_cond_broadcast" .
310 In doubt, use
311 .BR "pthread_cond_broadcast" .
312
313 To wait for 
314 .I "x"
315 to becomes greater than 
316 .I "y"
317 with a timeout of 5
318 seconds, do:
319
320 .RS
321 .ft 3
322 .nf
323 .sp
324 struct timeval now;
325 struct timespec timeout;
326 int retcode;
327
328 pthread_mutex_lock(&mut);
329 gettimeofday(&now);
330 timeout.tv_sec = now.tv_sec + 5;
331 timeout.tv_nsec = now.tv_usec * 1000;
332 retcode = 0;
333 while (x <= y && retcode != ETIMEDOUT) {
334         retcode = pthread_cond_timedwait(&cond, &mut, &timeout);
335 }
336 if (retcode == ETIMEDOUT) {
337         /* timeout occurred */
338 } else {
339         /* operate on x and y */
340 }
341 pthread_mutex_unlock(&mut);
342 .ft
343 .LP
344 .RE
345 .fi