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

📄 pthread_cond_timedwait.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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-2003 The Open Group, All Rights Reserved --><title>pthread_cond_timedwait</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="pthread_cond_timedwait"></a> <a name="tag_03_518"></a><!-- pthread_cond_timedwait --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright &copy; 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_518_01"></a>NAME</h4><blockquote>pthread_cond_timedwait, pthread_cond_wait - wait on a condition</blockquote><h4><a name="tag_03_518_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 &lt;<a href="../basedefs/pthread.h.html">pthread.h</a>&gt;<br><br> int pthread_cond_timedwait(pthread_cond_t *restrict</tt> <i>cond</i><tt>,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_mutex_t *restrict</tt> <i>mutex</i><tt>,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const struct timespec *restrict</tt> <i>abstime</i><tt>);<br> int pthread_cond_wait(pthread_cond_t *restrict</tt> <i>cond</i><tt>,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_mutex_t *restrict</tt> <i>mutex</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_518_03"></a>DESCRIPTION</h4><blockquote><p>The <i>pthread_cond_timedwait</i>() and <i>pthread_cond_wait</i>() functions shall block on a condition variable. They shall becalled with <i>mutex</i> locked by the calling thread or undefined behavior results.</p><p>These functions atomically release <i>mutex</i> and cause the calling thread to block on the condition variable <i>cond</i>;atomically here means &quot;atomically with respect to access by another thread to the mutex and then the condition variable&quot;. Thatis, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to <ahref="../functions/pthread_cond_broadcast.html"><i>pthread_cond_broadcast</i>()</a> or <a href="../functions/pthread_cond_signal.html"><i>pthread_cond_signal</i>()</a> in that thread shall behave as if it were issued after theabout-to-block thread has blocked.</p><p>Upon successful return, the mutex shall have been locked and shall be owned by the calling thread.</p><p>When using condition variables there is always a Boolean predicate involving shared variables associated with each conditionwait that is true if the thread should proceed. Spurious wakeups from the <i>pthread_cond_timedwait</i>() or<i>pthread_cond_wait</i>() functions may occur. Since the return from <i>pthread_cond_timedwait</i>() or <i>pthread_cond_wait</i>()does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.</p><p>The effect of using more than one mutex for concurrent <i>pthread_cond_timedwait</i>() or <i>pthread_cond_wait</i>() operationson the same condition variable is undefined; that is, a condition variable becomes bound to a unique mutex when a thread waits onthe condition variable, and this (dynamic) binding shall end when the wait returns.</p><p>A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set toPTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (ineffect) re-acquired before calling the first cancellation cleanup handler. The effect is as if the thread were unblocked, allowedto execute up to the point of returning from the call to <i>pthread_cond_timedwait</i>() or <i>pthread_cond_wait</i>(), but at thatpoint notices the cancellation request and instead of returning to the caller of <i>pthread_cond_timedwait</i>() or<i>pthread_cond_wait</i>(), starts the thread cancellation activities, which includes calling cancellation cleanup handlers.</p><p>A thread that has been unblocked because it has been canceled while blocked in a call to <i>pthread_cond_timedwait</i>() or<i>pthread_cond_wait</i>() shall not consume any condition signal that may be directed concurrently at the condition variable ifthere are other threads blocked on the condition variable.</p><p>The <i>pthread_cond_timedwait</i>() function shall be equivalent to <i>pthread_cond_wait</i>(), except that an error is returnedif the absolute time specified by <i>abstime</i> passes (that is, system time equals or exceeds <i>abstime</i>) before thecondition <i>cond</i> is signaled or broadcasted, or if the absolute time specified by <i>abstime</i> has already been passed atthe time of the call.</p><p><sup>[<a href="javascript:open_code('CS')">CS</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Ifthe Clock Selection option is supported, the condition variable shall have a clock attribute which specifies the clock that shallbe used to measure the time specified by the <i>abstime</i> argument. <img src="../images/opt-end.gif" alt="[Option End]" border="0"> When such timeouts occur, <i>pthread_cond_timedwait</i>() shall nonetheless release and re-acquire the mutex referenced by<i>mutex</i>. The <i>pthread_cond_timedwait</i>() function is also a cancellation point.</p><p>If a signal is delivered to a thread waiting for a condition variable, upon return from the signal handler the thread resumeswaiting for the condition variable as if it was not interrupted, or it shall return zero due to spurious wakeup.</p></blockquote><h4><a name="tag_03_518_04"></a>RETURN VALUE</h4><blockquote><p>Except in the case of [ETIMEDOUT], all these error checks shall act as if they were performed immediately at the beginning ofprocessing for the function and shall cause an error return, in effect, prior to modifying the state of the mutex specified by<i>mutex</i> or the condition variable specified by <i>cond</i>.</p><p>Upon successful completion, a value of zero shall be returned; otherwise, an error number shall be returned to indicate theerror.</p></blockquote><h4><a name="tag_03_518_05"></a>ERRORS</h4><blockquote><p>The <i>pthread_cond_timedwait</i>() function shall fail if:</p><dl compact><dt>[ETIMEDOUT]</dt><dd>The time specified by <i>abstime</i> to <i>pthread_cond_timedwait</i>() has passed.</dd></dl><p>The <i>pthread_cond_timedwait</i>() and <i>pthread_cond_wait</i>() functions may fail if:</p><dl compact><dt>[EINVAL]</dt><dd>The value specified by <i>cond</i>, <i>mutex</i>, or <i>abstime</i> is invalid.</dd><dt>[EINVAL]</dt><dd>Different mutexes were supplied for concurrent <i>pthread_cond_timedwait</i>() or <i>pthread_cond_wait</i>() operations on thesame condition variable.</dd><dt>[EPERM]</dt><dd>The mutex was not owned by the current thread at the time of the call.</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_518_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_518_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_518_08"></a>RATIONALE</h4><blockquote><h5><a name="tag_03_518_08_01"></a>Condition Wait Semantics</h5><p>It is important to note that when <i>pthread_cond_wait</i>() and <i>pthread_cond_timedwait</i>() return without error, theassociated predicate may still be false. Similarly, when <i>pthread_cond_timedwait</i>() returns with the timeout error, theassociated predicate may be true due to an unavoidable race between the expiration of the timeout and the predicate statechange.</p><p>Some implementations, particularly on a multi-processor, may sometimes cause multiple threads to wake up when the conditionvariable is signaled simultaneously on different processors.</p><p>In general, whenever a condition wait returns, the thread has to re-evaluate the predicate associated with the condition wait to

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -