📄 pthread_cond_broadcast.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2004 IEEE and The Open Group, All Rights Reserved --><title>pthread_cond_broadcast</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="pthread_cond_broadcast"></a> <a name="tag_03_515"></a><!-- pthread_cond_broadcast --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_515_01"></a>NAME</h4><blockquote>pthread_cond_broadcast, pthread_cond_signal - broadcast or signal a condition</blockquote><h4><a name="tag_03_515_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('THR')">THR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/pthread.h.html">pthread.h</a>><br><br> int pthread_cond_broadcast(pthread_cond_t *</tt><i>cond</i><tt>);<br> int pthread_cond_signal(pthread_cond_t *</tt><i>cond</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_515_03"></a>DESCRIPTION</h4><blockquote><p>These functions shall unblock threads blocked on a condition variable.</p><p>The <i>pthread_cond_broadcast</i>() function shall unblock all threads currently blocked on the specified condition variable<i>cond</i>.</p><p>The <i>pthread_cond_signal</i>() function shall unblock at least one of the threads that are blocked on the specified conditionvariable <i>cond</i> (if any threads are blocked on <i>cond</i>).</p><p>If more than one thread is blocked on a condition variable, the scheduling policy shall determine the order in which threads areunblocked. When each thread unblocked as a result of a <i>pthread_cond_broadcast</i>() or <i>pthread_cond_signal</i>() returns fromits call to <a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> or <a href="../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a>, the thread shall own the mutex with which it called<a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> or <a href="../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a>. The thread(s) that are unblocked shall contend forthe mutex according to the scheduling policy (if applicable), and as if each had called <a href="../functions/pthread_mutex_lock.html"><i>pthread_mutex_lock</i>()</a>.</p><p>The <i>pthread_cond_broadcast</i>() or <i>pthread_cond_signal</i>() functions may be called by a thread whether or not itcurrently owns the mutex that threads calling <a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> or <ahref="../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a> have associated with the condition variableduring their waits; however, if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling<i>pthread_cond_broadcast</i>() or <i>pthread_cond_signal</i>().</p><p>The <i>pthread_cond_broadcast</i>() and <i>pthread_cond_signal</i>() functions shall have no effect if there are no threadscurrently blocked on <i>cond</i>.</p></blockquote><h4><a name="tag_03_515_04"></a>RETURN VALUE</h4><blockquote><p>If successful, the <i>pthread_cond_broadcast</i>() and <i>pthread_cond_signal</i>() functions shall return zero; otherwise, anerror number shall be returned to indicate the error.</p></blockquote><h4><a name="tag_03_515_05"></a>ERRORS</h4><blockquote><p>The <i>pthread_cond_broadcast</i>() and <i>pthread_cond_signal</i>() function may fail if:</p><dl compact><dt>[EINVAL]</dt><dd>The value <i>cond</i> does not refer to an initialized condition variable.</dd></dl><p>These functions shall not return an error code of [EINTR].</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_515_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_515_07"></a>APPLICATION USAGE</h4><blockquote><p>The <i>pthread_cond_broadcast</i>() function is used whenever the shared-variable state has been changed in a way that more thanone thread can proceed with its task. Consider a single producer/multiple consumer problem, where the producer can insert multipleitems on a list that is accessed one item at a time by the consumers. By calling the <i>pthread_cond_broadcast</i>() function, theproducer would notify all consumers that might be waiting, and thereby the application would receive more throughput on amulti-processor. In addition, <i>pthread_cond_broadcast</i>() makes it easier to implement a read-write lock. The<i>pthread_cond_broadcast</i>() function is needed in order to wake up all waiting readers when a writer releases its lock.Finally, the two-phase commit algorithm can use this broadcast function to notify all clients of an impending transactioncommit.</p><p>It is not safe to use the <i>pthread_cond_signal</i>() function in a signal handler that is invoked asynchronously. Even if itwere safe, there would still be a race between the test of the Boolean <a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> that could not be efficiently eliminated.</p><p>Mutexes and condition variables are thus not suitable for releasing a waiting thread by signaling from code running in a signalhandler.</p></blockquote><h4><a name="tag_03_515_08"></a>RATIONALE</h4><blockquote><h5><a name="tag_03_515_08_01"></a>Multiple Awakenings by Condition Signal</h5><p>On a multi-processor, it may be impossible for an implementation of <i>pthread_cond_signal</i>() to avoid the unblocking of morethan one thread blocked on a condition variable. For example, consider the following partial implementation of <a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> and <i>pthread_cond_signal</i>(), executed by two threads inthe order given. One thread is trying to wait on the condition variable, another is concurrently executing<i>pthread_cond_signal</i>(), while a third thread is already waiting.</p><pre><tt>pthread_cond_wait(mutex, cond): value = cond->value; /* 1 */ pthread_mutex_unlock(mutex); /* 2 */ pthread_mutex_lock(cond->mutex); /* 10 */ if (value == cond->value) { /* 11 */ me->next_cond = cond->waiter; cond->waiter = me; pthread_mutex_unlock(cond->mutex); unable_to_run(me); } else pthread_mutex_unlock(cond->mutex); /* 12 */ pthread_mutex_lock(mutex); /* 13 */<br>pthread_cond_signal(cond): pthread_mutex_lock(cond->mutex); /* 3 */ cond->value++; /* 4 */ if (cond->waiter) { /* 5 */ sleeper = cond->waiter; /* 6 */ cond->waiter = sleeper->next_cond; /* 7 */ able_to_run(sleeper); /* 8 */ } pthread_mutex_unlock(cond->mutex); /* 9 */</tt></pre><p>The effect is that more than one thread can return from its call to <a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> or <a href="../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a> as a result of one call to<i>pthread_cond_signal</i>(). This effect is called "spurious wakeup". Note that the situation is self-correcting in that thenumber of threads that are so awakened is finite; for example, the next thread to call <a href="../functions/pthread_cond_wait.html"><i>pthread_cond_wait</i>()</a> after the sequence of events above blocks.</p><p>While this problem could be resolved, the loss of efficiency for a fringe condition that occurs only rarely is unacceptable,especially given that one has to check the predicate associated with a condition variable anyway. Correcting this problem wouldunnecessarily reduce the degree of concurrency in this basic building block for all higher-level synchronization operations.</p><p>An added benefit of allowing spurious wakeups is that applications are forced to code a predicate-testing-loop around thecondition wait. This also makes the application tolerate superfluous condition broadcasts or signals on the same condition variablethat may be coded in some other part of the application. The resulting applications are thus more robust. Therefore,IEEE Std 1003.1-2001 explicitly documents that spurious wakeups may occur.</p></blockquote><h4><a name="tag_03_515_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_515_10"></a>SEE ALSO</h4><blockquote><p><a href="pthread_cond_destroy.html"><i>pthread_cond_destroy</i>()</a>, <a href="pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a>, the Base Definitions volume of IEEE Std 1003.1-2001,<a href="../basedefs/pthread.h.html"><i><pthread.h></i></a></p></blockquote><h4><a name="tag_03_515_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 5. Included for alignment with the POSIX Threads Extension.</p></blockquote><h4><a name="tag_03_515_12"></a>Issue 6</h4><blockquote><p>The <i>pthread_cond_broadcast</i>() and <i>pthread_cond_signal</i>() functions are marked as part of the Threads option.</p><p>The APPLICATION USAGE section is added.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -