Test for stack alignment.
[glibc.git] / linuxthreads / man / pthread_cond_init.man
blob4913062fd21d74e5aacfe2b7aa88d029b1ebf3d2
1 .TH PTHREAD_COND 3 LinuxThreads
3 .XREF pthread_cond_signal
4 .XREF pthread_cond_broadcast
5 .XREF pthread_cond_wait
6 .XREF pthread_cond_timedwait
7 .XREF pthread_cond_destroy
9 .SH NAME
10 pthread_cond_init, pthread_cond_destroy, pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait \- operations on conditions
12 .SH SYNOPSIS
13 #include <pthread.h>
15 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
17 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
19 int pthread_cond_signal(pthread_cond_t *cond);
21 int pthread_cond_broadcast(pthread_cond_t *cond);
23 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
25 int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
27 int pthread_cond_destroy(pthread_cond_t *cond);
29 .SH DESCRIPTION
31 A condition (short for ``condition variable'') is a synchronization
32 device that allows threads to suspend execution and relinquish the
33 processors until some predicate on shared data is satisfied. The basic
34 operations on conditions are: signal the condition (when the
35 predicate becomes true), and wait for the condition, suspending the
36 thread execution until another thread signals the condition.
38 A condition variable must always be associated with a mutex, to avoid
39 the race condition where a thread prepares to wait on a condition
40 variable and another thread signals the condition just before the
41 first thread actually waits on it.
43 !pthread_cond_init! initializes the condition variable |cond|, using the
44 condition attributes specified in |cond_attr|, or default attributes
45 if |cond_attr| is !NULL!. The LinuxThreads implementation supports no
46 attributes for conditions, hence the |cond_attr| parameter is actually
47 ignored.
49 Variables of type !pthread_cond_t! can also be initialized
50 statically, using the constant !PTHREAD_COND_INITIALIZER!.
52 !pthread_cond_signal! restarts one of the threads that are waiting on
53 the condition variable |cond|. If no threads are waiting on |cond|,
54 nothing happens. If several threads are waiting on |cond|, exactly one
55 is restarted, but it is not specified which.
57 !pthread_cond_broadcast! restarts all the threads that are waiting on
58 the condition variable |cond|. Nothing happens if no threads are
59 waiting on |cond|.
61 !pthread_cond_wait! atomically unlocks the |mutex| (as per
62 !pthread_unlock_mutex!) and waits for the condition variable |cond| to
63 be signaled. The thread execution is suspended and does not consume
64 any CPU time until the condition variable is signaled. The |mutex|
65 must be locked by the calling thread on entrance to
66 !pthread_cond_wait!. Before returning to the calling thread,
67 !pthread_cond_wait! re-acquires |mutex| (as per !pthread_lock_mutex!).
69 Unlocking the mutex and suspending on the condition variable is done
70 atomically. Thus, if all threads always acquire the mutex before
71 signaling the condition, this guarantees that the condition cannot be
72 signaled (and thus ignored) between the time a thread locks the mutex
73 and the time it waits on the condition variable.
75 !pthread_cond_timedwait! atomically unlocks |mutex| and waits on
76 |cond|, as !pthread_cond_wait! does, but it also bounds the duration
77 of the wait. If |cond| has not been signaled within the amount of time
78 specified by |abstime|, the mutex |mutex| is re-acquired and
79 !pthread_cond_timedwait! returns the error !ETIMEDOUT!.
80 The |abstime| parameter specifies an absolute time, with the same
81 origin as !time!(2) and !gettimeofday!(2): an |abstime| of 0
82 corresponds to 00:00:00 GMT, January 1, 1970.
84 !pthread_cond_destroy! destroys a condition variable, freeing the
85 resources it might hold. No threads must be waiting on the condition
86 variable on entrance to !pthread_cond_destroy!. In the LinuxThreads
87 implementation, no resources are associated with condition variables,
88 thus !pthread_cond_destroy! actually does nothing except checking that
89 the condition has no waiting threads.
91 .SH CANCELLATION
93 !pthread_cond_wait! and !pthread_cond_timedwait! are cancellation
94 points. If a thread is cancelled while suspended in one of these
95 functions, the thread immediately resumes execution, then locks again
96 the |mutex| argument to !pthread_cond_wait! and
97 !pthread_cond_timedwait!, and finally executes the cancellation.
98 Consequently, cleanup handlers are assured that |mutex| is locked when
99 they are called.
101 .SH "ASYNC-SIGNAL SAFETY"
103 The condition functions are not async-signal safe, and should not be
104 called from a signal handler. In particular, calling
105 !pthread_cond_signal! or !pthread_cond_broadcast! from a signal
106 handler may deadlock the calling thread.
108 .SH "RETURN VALUE"
110 All condition variable functions return 0 on success and a non-zero
111 error code on error.
113 .SH ERRORS
115 !pthread_cond_init!, !pthread_cond_signal!, !pthread_cond_broadcast!,
116 and !pthread_cond_wait! never return an error code.
118 The !pthread_cond_timedwait! function returns the following error codes
119 on error:
122 !ETIMEDOUT!
123 the condition variable was not signaled until the timeout specified by
124 |abstime|
127 !EINTR!
128 !pthread_cond_timedwait! was interrupted by a signal
131 The !pthread_cond_destroy! function returns the following error code
132 on error:
135 !EBUSY!
136 some threads are currently waiting on |cond|.
139 .SH AUTHOR
140 Xavier Leroy <Xavier.Leroy@inria.fr>
142 .SH "SEE ALSO"
143 !pthread_condattr_init!(3),
144 !pthread_mutex_lock!(3),
145 !pthread_mutex_unlock!(3),
146 !gettimeofday!(2),
147 !nanosleep!(2).
149 .SH EXAMPLE
151 Consider two shared variables |x| and |y|, protected by the mutex |mut|,
152 and a condition variable |cond| that is to be signaled whenever |x|
153 becomes greater than |y|.
156 .ft 3
159 int x,y;
160 pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
161 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
167 Waiting until |x| is greater than |y| is performed as follows:
170 .ft 3
173 pthread_mutex_lock(&mut);
174 while (x <= y) {
175         pthread_cond_wait(&cond, &mut);
177 /* operate on x and y */
178 pthread_mutex_unlock(&mut);
184 Modifications on |x| and |y| that may cause |x| to become greater than
185 |y| should signal the condition if needed:
188 .ft 3
191 pthread_mutex_lock(&mut);
192 /* modify x and y */
193 if (x > y) pthread_cond_broadcast(&cond);
194 pthread_mutex_unlock(&mut);
200 If it can be proved that at most one waiting thread needs to be waken
201 up (for instance, if there are only two threads communicating through
202 |x| and |y|), !pthread_cond_signal! can be used as a slightly more
203 efficient alternative to !pthread_cond_broadcast!. In doubt, use
204 !pthread_cond_broadcast!.
206 To wait for |x| to becomes greater than |y| with a timeout of 5
207 seconds, do:
210 .ft 3
213 struct timeval now;
214 struct timespec timeout;
215 int retcode;
217 pthread_mutex_lock(&mut);
218 gettimeofday(&now);
219 timeout.tv_sec = now.tv_sec + 5;
220 timeout.tv_nsec = now.tv_usec * 1000;
221 retcode = 0;
222 while (x <= y && retcode != ETIMEDOUT) {
223         retcode = pthread_cond_timedwait(&cond, &mut, &timeout);
225 if (retcode == ETIMEDOUT) {
226         /* timeout occurred */
227 } else {
228         /* operate on x and y */
230 pthread_mutex_unlock(&mut);