⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pthread_cond_wait.html

📁 posix标准英文,html格式
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<p>It is thus recommended that a condition wait be enclosed in the equivalent of a &quot;while loop&quot; that checks the predicate.</p><h5><a name="tag_03_518_08_02"></a>Timed Wait Semantics</h5><p>An absolute time measure was chosen for specifying the timeout parameter for two reasons. First, a relative time measure can beeasily implemented on top of a function that specifies absolute time, but there is a race condition associated with specifying anabsolute timeout on top of a function that specifies relative timeouts. For example, assume that <a href="../functions/clock_gettime.html"><i>clock_gettime</i>()</a> returns the current time and <i>cond_relative_timed_wait</i>() usesrelative timeouts:</p><pre><tt>clock_gettime(CLOCK_REALTIME, &amp;now)reltime = sleep_til_this_absolute_time -now;cond_relative_timed_wait(c, m, &amp;reltime);</tt></pre><p>If the thread is preempted between the first statement and the last statement, the thread blocks for too long. Blocking,however, is irrelevant if an absolute timeout is used. An absolute timeout also need not be recomputed if it is used multiple timesin a loop, such as that enclosing a condition wait.</p><p>For cases when the system clock is advanced discontinuously by an operator, it is expected that implementations process anytimed wait expiring at an intervening time as if that time had actually occurred.</p><h5><a name="tag_03_518_08_03"></a>Cancellation and Condition Wait</h5><p>A condition wait, whether timed or not, is a cancellation point. That is, the functions <i>pthread_cond_wait</i>() or<i>pthread_cond_timedwait</i>() are points where a pending (or concurrent) cancellation request is noticed. The reason for this isthat an indefinite wait is possible at these points-whatever event is being waited for, even if the program is totally correct,might never occur; for example, some input data being awaited might never be sent. By making condition wait a cancellation point,the thread can be canceled and perform its cancellation cleanup handler even though it may be stuck in some indefinite wait.</p><p>A side effect of acting on a cancellation request while a thread is blocked on a condition variable is to re-acquire the mutexbefore calling any of the cancellation cleanup handlers. This is done in order to ensure that the cancellation cleanup handler isexecuted in the same state as the critical code that lies both before and after the call to the condition wait function. This ruleis also required when interfacing to POSIX threads from languages, such as Ada or C++, which may choose to map cancellation onto alanguage exception; this rule ensures that each exception handler guarding a critical section can always safely depend upon thefact that the associated mutex has already been locked regardless of exactly where within the critical section the exception wasraised. Without this rule, there would not be a uniform rule that exception handlers could follow regarding the lock, and so codingwould become very cumbersome.</p><p>Therefore, since <i>some</i> statement has to be made regarding the state of the lock when a cancellation is delivered during await, a definition has been chosen that makes application coding most convenient and error free.</p><p>When acting on a cancellation request while a thread is blocked on a condition variable, the implementation is required toensure that the thread does not consume any condition signals directed at that condition variable if there are any other threadswaiting on that condition variable. This rule is specified in order to avoid deadlock conditions that could occur if these twoindependent requests (one acting on a thread and the other acting on the condition variable) were not processed independently.</p><h5><a name="tag_03_518_08_04"></a>Performance of Mutexes and Condition Variables</h5><p>Mutexes are expected to be locked only for a few instructions. This practice is almost automatically enforced by the desire ofprogrammers to avoid long serial regions of execution (which would reduce total effective parallelism).</p><p>When using mutexes and condition variables, one tries to ensure that the usual case is to lock the mutex, access shared data,and unlock the mutex. Waiting on a condition variable should be a relatively rare situation. For example, when implementing aread-write lock, code that acquires a read-lock typically needs only to increment the count of readers (under mutual-exclusion) andreturn. The calling thread would actually wait on the condition variable only when there is already an active writer. So theefficiency of a synchronization operation is bounded by the cost of mutex lock/unlock and not by condition wait. Note that in theusual case there is no context switch.</p><p>This is not to say that the efficiency of condition waiting is unimportant. Since there needs to be at least one context switchper Ada rendezvous, the efficiency of waiting on a condition variable is important. The cost of waiting on a condition variableshould be little more than the minimal cost for a context switch plus the time to unlock and lock the mutex.</p><h5><a name="tag_03_518_08_05"></a>Features of Mutexes and Condition Variables</h5><p>It had been suggested that the mutex acquisition and release be decoupled from condition wait. This was rejected because it isthe combined nature of the operation that, in fact, facilitates realtime implementations. Those implementations can atomically movea high-priority thread between the condition variable and the mutex in a manner that is transparent to the caller. This can preventextra context switches and provide more deterministic acquisition of a mutex when the waiting thread is signaled. Thus, fairnessand priority issues can be dealt with directly by the scheduling discipline. Furthermore, the current condition wait operationmatches existing practice.</p><h5><a name="tag_03_518_08_06"></a>Scheduling Behavior of Mutexes and Condition Variables</h5><p>Synchronization primitives that attempt to interfere with scheduling policy by specifying an ordering rule are consideredundesirable. Threads waiting on mutexes and condition variables are selected to proceed in an order dependent upon the schedulingpolicy rather than in some fixed order (for example, FIFO or priority). Thus, the scheduling policy determines which thread(s) areawakened and allowed to proceed.</p><h5><a name="tag_03_518_08_07"></a>Timed Condition Wait</h5><p>The <i>pthread_cond_timedwait</i>() function allows an application to give up waiting for a particular condition after a givenamount of time. An example of its use follows:</p><pre><tt>(void) pthread_mutex_lock(&amp;t.mn);        t.waiters++;    clock_gettime(CLOCK_REALTIME, &amp;ts);    ts.tv_sec += 5;    rc = 0;    while (! mypredicate(&amp;t) &amp;&amp; rc == 0)        rc = pthread_cond_timedwait(&amp;t.cond, &amp;t.mn, &amp;ts);    t.waiters--;    if (rc == 0) setmystate(&amp;t);(void) pthread_mutex_unlock(&amp;t.mn);</tt></pre><p>By making the timeout parameter absolute, it does not need to be recomputed each time the program checks its blocking predicate.If the timeout was relative, it would have to be recomputed before each call. This would be especially difficult since such codewould need to take into account the possibility of extra wakeups that result from extra broadcasts or signals on the conditionvariable that occur before either the predicate is true or the timeout is due.</p></blockquote><h4><a name="tag_03_518_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_518_10"></a>SEE ALSO</h4><blockquote><p><a href="pthread_cond_signal.html"><i>pthread_cond_signal</i>()</a>, <a href="pthread_cond_broadcast.html"><i>pthread_cond_broadcast</i>()</a>, the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001,<a href="../basedefs/pthread.h.html"><i>&lt;pthread.h&gt;</i></a></p></blockquote><h4><a name="tag_03_518_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_518_12"></a>Issue 6</h4><blockquote><p>The <i>pthread_cond_timedwait</i>() and <i>pthread_cond_wait</i>() functions are marked as part of the Threads option.</p><p>The Open Group Corrigendum U021/9 is applied, correcting the prototype for the <i>pthread_cond_wait</i>() function.</p><p>The DESCRIPTION is updated for alignment with IEEE&nbsp;Std&nbsp;1003.1j-2000 by adding semantics for the Clock Selectionoption.</p><p>The ERRORS section has an additional case for [EPERM] in response to IEEE PASC Interpretation 1003.1c #28.</p><p>The <b>restrict</b> keyword is added to the <i>pthread_cond_timedwait</i>() and <i>pthread_cond_wait</i>() prototypes foralignment with the ISO/IEC&nbsp;9899:1999 standard.</p><p>IEEE&nbsp;Std&nbsp;1003.1-2001/Cor&nbsp;2-2004, item XSH/TC2/D6/89 is applied, updating the DESCRIPTION for consistency with the<a href="../functions/pthread_cond_destroy.html"><i>pthread_cond_destroy</i>()</a> function that states it is safe to destroy aninitialized condition variable upon which no threads are currently blocked.</p><p>IEEE&nbsp;Std&nbsp;1003.1-2001/Cor&nbsp;2-2004, item XSH/TC2/D6/90 is applied, updating words in the DESCRIPTION from &quot;thecancelability enable state&quot; to &quot;the cancelability type&quot;.</p><p>IEEE&nbsp;Std&nbsp;1003.1-2001/Cor&nbsp;2-2004, item XSH/TC2/D6/91 is applied, updating the ERRORS section to remove the errorcase related to <i>abstime</i> from the <i>pthread_cond_wait</i>() function, and to make the error case related to <i>abstime</i>mandatory for <i>pthread_cond_timedwait</i>() for consistency with other functions.</p><p>IEEE&nbsp;Std&nbsp;1003.1-2001/Cor&nbsp;2-2004, item XSH/TC2/D6/92 is applied, adding a new paragraph to the RATIONALE sectionstating that an application should check the predicate on any return from this function.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; 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 + -