]>
Commit | Line | Data |
---|---|---|
13151ec5 AC |
1 | .\" Copyright, Xavier Leroy <Xavier.Leroy@inria.fr> |
2 | .\" Copyright 2023, Alejandro Colomar <alx@kernel.org> | |
3 | .\" | |
4 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft | |
5 | .\" | |
6 | .TH pthread_cond_init 3 (date) "Linux man-pages (unreleased)" | |
74235f15 AC |
7 | . |
8 | . | |
87d09778 | 9 | .SH NAME |
8a00cac7 AC |
10 | pthread_cond_init, |
11 | pthread_cond_signal, | |
12 | pthread_cond_broadcast, | |
13 | pthread_cond_wait, | |
14 | pthread_cond_timedwait, | |
15 | pthread_cond_destroy | |
16 | \- | |
17 | operations on conditions | |
74235f15 AC |
18 | . |
19 | . | |
87d09778 | 20 | .SH SYNOPSIS |
c1c253d0 | 21 | .B #include <pthread.h> |
c6d039a3 | 22 | .P |
c1c253d0 | 23 | .BI "pthread_cond_t " cond " = PTHREAD_COND_INITIALIZER;" |
c6d039a3 | 24 | .P |
c1c253d0 | 25 | .BI "int pthread_cond_init(pthread_cond_t *" cond ", pthread_condattr_t *" cond_attr ");" |
c1c253d0 | 26 | .BI "int pthread_cond_signal(pthread_cond_t *" cond ");" |
c1c253d0 | 27 | .BI "int pthread_cond_broadcast(pthread_cond_t *" cond ");" |
c1c253d0 | 28 | .BI "int pthread_cond_wait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ");" |
c1c253d0 | 29 | .BI "int pthread_cond_timedwait(pthread_cond_t *" cond ", pthread_mutex_t *" mutex ", const struct timespec *" abstime ");" |
c1c253d0 | 30 | .BI "int pthread_cond_destroy(pthread_cond_t *" cond ");" |
74235f15 AC |
31 | . |
32 | . | |
87d09778 | 33 | .SH DESCRIPTION |
8a00cac7 AC |
34 | A condition (short for ``condition variable'') |
35 | is a synchronization device that allows threads | |
36 | to suspend execution and relinquish the processors | |
37 | until some predicate on shared data is satisfied. | |
38 | The basic operations on conditions are: | |
39 | signal the condition (when the predicate becomes true), | |
40 | and wait for the condition, | |
41 | suspending the thread execution until another thread signals the condition. | |
c6d039a3 | 42 | .P |
8a00cac7 AC |
43 | A condition variable must always be associated with a mutex, |
44 | to avoid the race condition where | |
45 | a thread prepares to wait on a condition variable | |
46 | and another thread signals the condition | |
47 | just before the first thread actually waits on it. | |
c6d039a3 | 48 | .P |
8a00cac7 AC |
49 | \fBpthread_cond_init\fP initializes the condition variable \fIcond\fP, |
50 | using the condition attributes specified in \fIcond_attr\fP, | |
51 | or default attributes if \fIcond_attr\fP is \fBNULL\fP. | |
52 | The LinuxThreads implementation supports no attributes for conditions, | |
53 | hence the \fIcond_attr\fP parameter is actually ignored. | |
c6d039a3 | 54 | .P |
8a00cac7 AC |
55 | Variables of type \fBpthread_cond_t\fP can also be initialized statically, |
56 | using the constant \fBPTHREAD_COND_INITIALIZER\fP. | |
c6d039a3 | 57 | .P |
8a00cac7 AC |
58 | \fBpthread_cond_signal\fP restarts one of the threads that |
59 | are waiting on the condition variable \fIcond\fP. | |
60 | If no threads are waiting on \fIcond\fP, | |
61 | nothing happens. | |
62 | If several threads are waiting on \fIcond\fP, | |
63 | exactly one is restarted, | |
64 | but it is not specified which. | |
c6d039a3 | 65 | .P |
8a00cac7 AC |
66 | \fBpthread_cond_broadcast\fP restarts all the threads that |
67 | are waiting on the condition variable \fIcond\fP. | |
68 | Nothing happens if no threads are waiting on \fIcond\fP. | |
c6d039a3 | 69 | .P |
8a00cac7 AC |
70 | \fBpthread_cond_wait\fP atomically unlocks the \fImutex\fP |
71 | (as per \fBpthread_unlock_mutex\fP) | |
72 | and waits for the condition variable \fIcond\fP to be signaled. | |
73 | The thread execution is suspended and does not consume any CPU time | |
74 | until the condition variable is signaled. | |
75 | The \fImutex\fP must be locked by the calling thread | |
76 | on entrance to \fBpthread_cond_wait\fP. | |
77 | Before returning to the calling thread, | |
78 | \fBpthread_cond_wait\fP re-acquires \fImutex\fP | |
79 | (as per \fBpthread_lock_mutex\fP). | |
c6d039a3 | 80 | .P |
8a00cac7 AC |
81 | Unlocking the mutex and suspending on the condition variable is done atomically. |
82 | Thus, | |
83 | if all threads always acquire the mutex before signaling the condition, | |
84 | this guarantees that the condition cannot be signaled (and thus ignored) | |
85 | between the time a thread locks the mutex | |
87d09778 | 86 | and the time it waits on the condition variable. |
c6d039a3 | 87 | .P |
8a00cac7 AC |
88 | \fBpthread_cond_timedwait\fP atomically unlocks \fImutex\fP |
89 | and waits on \fIcond\fP, | |
90 | as \fBpthread_cond_wait\fP does, | |
91 | but it also bounds the duration of the wait. | |
92 | If \fIcond\fP has not been signaled | |
93 | within the amount of time specified by \fIabstime\fP, | |
94 | the mutex \fImutex\fP is re-acquired | |
95 | and \fBpthread_cond_timedwait\fP returns the error \fBETIMEDOUT\fP. | |
96 | The \fIabstime\fP parameter specifies an absolute time, | |
97 | with the same origin as \fBtime\fP(2) and \fBgettimeofday\fP(2): | |
98 | an \fIabstime\fP of 0 | |
87d09778 | 99 | corresponds to 00:00:00 GMT, January 1, 1970. |
c6d039a3 | 100 | .P |
8a00cac7 AC |
101 | \fBpthread_cond_destroy\fP destroys a condition variable, |
102 | freeing the resources it might hold. | |
103 | No threads must be waiting on the condition variable | |
104 | on entrance to \fBpthread_cond_destroy\fP. | |
105 | In the LinuxThreads implementation, | |
106 | no resources are associated with condition variables, | |
107 | thus \fBpthread_cond_destroy\fP actually does nothing | |
108 | except checking that the condition has no waiting threads. | |
74235f15 AC |
109 | . |
110 | . | |
87d09778 | 111 | .SH CANCELLATION |
8a00cac7 | 112 | \fBpthread_cond_wait\fP and \fBpthread_cond_timedwait\fP |
ee7b1c77 | 113 | are cancelation points. |
8a00cac7 AC |
114 | If a thread is cancelled while suspended in one of these functions, |
115 | the thread immediately resumes execution, | |
116 | then locks again the \fImutex\fP | |
117 | argument to \fBpthread_cond_wait\fP and \fBpthread_cond_timedwait\fP, | |
ee7b1c77 | 118 | and finally executes the cancelation. |
8a00cac7 AC |
119 | Consequently, |
120 | cleanup handlers are assured that \fImutex\fP is locked | |
121 | when they are called. | |
74235f15 AC |
122 | . |
123 | . | |
87d09778 | 124 | .SH "ASYNC-SIGNAL SAFETY" |
8a00cac7 AC |
125 | The condition functions are not async-signal safe, |
126 | and should not be called from a signal handler. | |
127 | In particular, | |
128 | calling \fBpthread_cond_signal\fP or \fBpthread_cond_broadcast\fP | |
129 | from a signal handler | |
130 | may deadlock the calling thread. | |
74235f15 AC |
131 | . |
132 | . | |
87d09778 | 133 | .SH "RETURN VALUE" |
8a00cac7 AC |
134 | All condition variable functions return 0 on success |
135 | and a non-zero error code on error. | |
74235f15 AC |
136 | . |
137 | . | |
87d09778 | 138 | .SH ERRORS |
8a00cac7 AC |
139 | \fBpthread_cond_init\fP, |
140 | \fBpthread_cond_signal\fP, | |
141 | \fBpthread_cond_broadcast\fP, | |
142 | and \fBpthread_cond_wait\fP | |
143 | never return an error code. | |
c6d039a3 | 144 | .P |
8a00cac7 AC |
145 | The \fBpthread_cond_timedwait\fP function returns |
146 | the following error codes on error: | |
87d09778 AC |
147 | .RS |
148 | .TP | |
c1c253d0 | 149 | \fBETIMEDOUT\fP |
8a00cac7 AC |
150 | The condition variable was not signaled |
151 | until the timeout specified by \fIabstime\fP. | |
87d09778 | 152 | .TP |
c1c253d0 | 153 | \fBEINTR\fP |
8a00cac7 | 154 | \fBpthread_cond_timedwait\fP was interrupted by a signal. |
87d09778 | 155 | .RE |
c6d039a3 | 156 | .P |
8a00cac7 AC |
157 | The \fBpthread_cond_destroy\fP function returns |
158 | the following error code on error: | |
87d09778 AC |
159 | .RS |
160 | .TP | |
c1c253d0 | 161 | \fBEBUSY\fP |
8a00cac7 | 162 | Some threads are currently waiting on \fIcond\fP. |
87d09778 | 163 | .RE |
74235f15 AC |
164 | . |
165 | . | |
87d09778 | 166 | .SH "SEE ALSO" |
c1c253d0 AC |
167 | \fBpthread_condattr_init\fP(3), |
168 | \fBpthread_mutex_lock\fP(3), | |
169 | \fBpthread_mutex_unlock\fP(3), | |
170 | \fBgettimeofday\fP(2), | |
171 | \fBnanosleep\fP(2). | |
74235f15 AC |
172 | . |
173 | . | |
87d09778 | 174 | .SH EXAMPLE |
8a00cac7 AC |
175 | Consider two shared variables \fIx\fP and \fIy\fP, |
176 | protected by the mutex \fImut\fP, | |
177 | and a condition variable \fIcond\fP | |
178 | that is to be signaled | |
179 | whenever \fIx\fP becomes greater than \fIy\fP. | |
c6d039a3 | 180 | .P |
87d09778 AC |
181 | .RS |
182 | .ft 3 | |
183 | .nf | |
184 | .sp | |
185 | int x,y; | |
186 | pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; | |
187 | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; | |
188 | .ft | |
c6d039a3 | 189 | .P |
87d09778 AC |
190 | .RE |
191 | .fi | |
c6d039a3 | 192 | .P |
c1c253d0 | 193 | Waiting until \fIx\fP is greater than \fIy\fP is performed as follows: |
c6d039a3 | 194 | .P |
87d09778 AC |
195 | .RS |
196 | .ft 3 | |
197 | .nf | |
198 | .sp | |
199 | pthread_mutex_lock(&mut); | |
200 | while (x <= y) { | |
201 | pthread_cond_wait(&cond, &mut); | |
202 | } | |
203 | /* operate on x and y */ | |
204 | pthread_mutex_unlock(&mut); | |
205 | .ft | |
c6d039a3 | 206 | .P |
87d09778 AC |
207 | .RE |
208 | .fi | |
c6d039a3 | 209 | .P |
8a00cac7 AC |
210 | Modifications on \fIx\fP and \fIy\fP |
211 | that may cause \fIx\fP to become greater than \fIy\fP | |
212 | should signal the condition if needed: | |
c6d039a3 | 213 | .P |
87d09778 AC |
214 | .RS |
215 | .ft 3 | |
216 | .nf | |
217 | .sp | |
218 | pthread_mutex_lock(&mut); | |
219 | /* modify x and y */ | |
220 | if (x > y) pthread_cond_broadcast(&cond); | |
221 | pthread_mutex_unlock(&mut); | |
222 | .ft | |
c6d039a3 | 223 | .P |
87d09778 AC |
224 | .RE |
225 | .fi | |
c6d039a3 | 226 | .P |
8a00cac7 AC |
227 | If it can be proved that at most one waiting thread needs to be waken up |
228 | (for instance, | |
229 | if there are only two threads communicating through \fIx\fP and \fIy\fP), | |
230 | \fBpthread_cond_signal\fP can be used as | |
231 | a slightly more efficient alternative to \fBpthread_cond_broadcast\fP. | |
232 | In doubt, | |
233 | use \fBpthread_cond_broadcast\fP. | |
c6d039a3 | 234 | .P |
8a00cac7 AC |
235 | To wait for \fIx\fP to become greater than \fIy\fP |
236 | with a timeout of 5 seconds, | |
237 | do: | |
c6d039a3 | 238 | .P |
87d09778 AC |
239 | .RS |
240 | .ft 3 | |
241 | .nf | |
242 | .sp | |
243 | struct timeval now; | |
244 | struct timespec timeout; | |
245 | int retcode; | |
74235f15 | 246 | \& |
87d09778 AC |
247 | pthread_mutex_lock(&mut); |
248 | gettimeofday(&now); | |
249 | timeout.tv_sec = now.tv_sec + 5; | |
250 | timeout.tv_nsec = now.tv_usec * 1000; | |
251 | retcode = 0; | |
252 | while (x <= y && retcode != ETIMEDOUT) { | |
253 | retcode = pthread_cond_timedwait(&cond, &mut, &timeout); | |
254 | } | |
255 | if (retcode == ETIMEDOUT) { | |
256 | /* timeout occurred */ | |
257 | } else { | |
258 | /* operate on x and y */ | |
259 | } | |
260 | pthread_mutex_unlock(&mut); | |
261 | .ft | |
c6d039a3 | 262 | .P |
87d09778 AC |
263 | .RE |
264 | .fi |