1PTHREAD_COND_BROADCAST(3P) POSIX Programmer's ManualPTHREAD_COND_BROADCAST(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       pthread_cond_broadcast, pthread_cond_signal -  broadcast  or  signal  a
13       condition
14

SYNOPSIS

16       #include <pthread.h>
17
18       int pthread_cond_broadcast(pthread_cond_t *cond);
19       int pthread_cond_signal(pthread_cond_t *cond);
20
21

DESCRIPTION

23       These functions shall unblock threads blocked on a condition variable.
24
25       The  pthread_cond_broadcast()  function  shall unblock all threads cur‐
26       rently blocked on the specified condition variable cond.
27
28       The pthread_cond_signal() function shall unblock at least  one  of  the
29       threads  that  are blocked on the specified condition variable cond (if
30       any threads are blocked on cond).
31
32       If more than one thread is blocked on a condition variable, the  sched‐
33       uling  policy shall determine the order in which threads are unblocked.
34       When each thread unblocked as a result of a pthread_cond_broadcast() or
35       pthread_cond_signal()  returns  from its call to pthread_cond_wait() or
36       pthread_cond_timedwait(), the thread shall own the mutex with which  it
37       called  pthread_cond_wait()  or pthread_cond_timedwait(). The thread(s)
38       that are unblocked shall contend for the mutex according to the  sched‐
39       uling   policy   (if   applicable),   and   as   if   each  had  called
40       pthread_mutex_lock().
41
42       The pthread_cond_broadcast() or pthread_cond_signal() functions may  be
43       called  by  a  thread  whether  or not it currently owns the mutex that
44       threads calling pthread_cond_wait()  or  pthread_cond_timedwait()  have
45       associated  with the condition variable during their waits; however, if
46       predictable scheduling behavior is required, then that mutex  shall  be
47       locked    by    the    thread   calling   pthread_cond_broadcast()   or
48       pthread_cond_signal().
49
50       The pthread_cond_broadcast() and pthread_cond_signal() functions  shall
51       have no effect if there are no threads currently blocked on cond.
52

RETURN VALUE

54       If  successful,  the pthread_cond_broadcast() and pthread_cond_signal()
55       functions shall return  zero;  otherwise,  an  error  number  shall  be
56       returned to indicate the error.
57

ERRORS

59       The  pthread_cond_broadcast()  and  pthread_cond_signal()  function may
60       fail if:
61
62       EINVAL The value cond does not refer to an initialized condition  vari‐
63              able.
64
65
66       These functions shall not return an error code of [EINTR].
67
68       The following sections are informative.
69

EXAMPLES

71       None.
72

APPLICATION USAGE

74       The pthread_cond_broadcast() function is used whenever the shared-vari‐
75       able state has been changed in a way that more than one thread can pro‐
76       ceed  with its task. Consider a single producer/multiple consumer prob‐
77       lem, where the producer can insert multiple items on  a  list  that  is
78       accessed  one  item  at  a  time  by  the  consumers.   By  calling the
79       pthread_cond_broadcast() function, the producer would notify  all  con‐
80       sumers that might be waiting, and thereby the application would receive
81       more throughput on a multi-processor. In addition,  pthread_cond_broad‐
82       cast()   makes   it   easier   to  implement  a  read-write  lock.  The
83       pthread_cond_broadcast() function is needed in order  to  wake  up  all
84       waiting  readers  when  a  writer releases its lock.  Finally, the two-
85       phase commit algorithm can use this broadcast function  to  notify  all
86       clients of an impending transaction commit.
87
88       It  is  not  safe to use the pthread_cond_signal() function in a signal
89       handler that is invoked asynchronously. Even if  it  were  safe,  there
90       would   still   be   a   race   between   the   test   of  the  Boolean
91       pthread_cond_wait() that could not be efficiently eliminated.
92
93       Mutexes and condition variables are thus not suitable for  releasing  a
94       waiting thread by signaling from code running in a signal handler.
95

RATIONALE

97   Multiple Awakenings by Condition Signal
98       On  a  multi-processor,  it  may be impossible for an implementation of
99       pthread_cond_signal() to avoid the unblocking of more than  one  thread
100       blocked  on  a  condition variable. For example, consider the following
101       partial implementation  of  pthread_cond_wait()  and  pthread_cond_sig‐
102       nal(), executed by two threads in the order given. One thread is trying
103       to wait on the condition variable, another  is  concurrently  executing
104       pthread_cond_signal(), while a third thread is already waiting.
105
106
107              pthread_cond_wait(mutex, cond):
108                  value = cond->value; /* 1 */
109                  pthread_mutex_unlock(mutex); /* 2 */
110                  pthread_mutex_lock(cond->mutex); /* 10 */
111                  if (value == cond->value) { /* 11 */
112                      me->next_cond = cond->waiter;
113                      cond->waiter = me;
114                      pthread_mutex_unlock(cond->mutex);
115                      unable_to_run(me);
116                  } else
117                      pthread_mutex_unlock(cond->mutex); /* 12 */
118                  pthread_mutex_lock(mutex); /* 13 */
119
120
121              pthread_cond_signal(cond):
122                  pthread_mutex_lock(cond->mutex); /* 3 */
123                  cond->value++; /* 4 */
124                  if (cond->waiter) { /* 5 */
125                      sleeper = cond->waiter; /* 6 */
126                      cond->waiter = sleeper->next_cond; /* 7 */
127                      able_to_run(sleeper); /* 8 */
128                  }
129                  pthread_mutex_unlock(cond->mutex); /* 9 */
130
131       The  effect  is  that  more than one thread can return from its call to
132       pthread_cond_wait() or pthread_cond_timedwait() as a result of one call
133       to  pthread_cond_signal().  This  effect  is  called "spurious wakeup".
134       Note that the situation  is  self-correcting  in  that  the  number  of
135       threads that are so awakened is finite; for example, the next thread to
136       call pthread_cond_wait() after the sequence of events above blocks.
137
138       While this problem could be resolved, the  loss  of  efficiency  for  a
139       fringe  condition  that  occurs only rarely is unacceptable, especially
140       given that one has to check the predicate associated with  a  condition
141       variable anyway. Correcting this problem would unnecessarily reduce the
142       degree of concurrency in this basic building block for all higher-level
143       synchronization operations.
144
145       An  added benefit of allowing spurious wakeups is that applications are
146       forced to code a predicate-testing-loop around the condition wait. This
147       also makes the application tolerate superfluous condition broadcasts or
148       signals on the same condition variable that may be coded in some  other
149       part  of  the  application.  The  resulting  applications are thus more
150       robust. Therefore, IEEE Std 1003.1-2001 explicitly documents that  spu‐
151       rious wakeups may occur.
152

FUTURE DIRECTIONS

154       None.
155

SEE ALSO

157       pthread_cond_destroy(),  pthread_cond_timedwait(), the Base Definitions
158       volume of IEEE Std 1003.1-2001, <pthread.h>
159
161       Portions of this text are reprinted and reproduced in  electronic  form
162       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
163       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
164       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
165       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
166       event of any discrepancy between this version and the original IEEE and
167       The Open Group Standard, the original IEEE and The Open Group  Standard
168       is  the  referee document. The original Standard can be obtained online
169       at http://www.opengroup.org/unix/online.html .
170
171
172
173IEEE/The Open Group                  2003           PTHREAD_COND_BROADCAST(3P)
Impressum