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