pthread_cond_init.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
  5. <TITLE>PTHREAD_COND(3) manual page</TITLE>
  6. <META NAME="GENERATOR" CONTENT="OpenOffice.org 1.1.3 (Linux)">
  7. <META NAME="CREATED" CONTENT="20050504;16454400">
  8. <META NAME="CHANGED" CONTENT="20050505;19004700">
  9. <!-- manual page source format generated by PolyglotMan v3.2, -->
  10. <!-- available at http://polyglotman.sourceforge.net/ -->
  11. </HEAD>
  12. <BODY LANG="en-GB" BGCOLOR="#ffffff" DIR="LTR">
  13. <H4>POSIX Threads for Windows – REFERENCE - <A HREF="http://sources.redhat.com/pthreads-win32">Pthreads-w32</A></H4>
  14. <P><A HREF="index.html">Reference Index</A></P>
  15. <P><A HREF="#toc">Table of Contents</A></P>
  16. <H2><A HREF="#toc0" NAME="sect0">Name</A></H2>
  17. <P>pthread_cond_init, pthread_cond_destroy, pthread_cond_signal,
  18. pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait -
  19. operations on conditions
  20. </P>
  21. <H2><A HREF="#toc1" NAME="sect1">Synopsis</A></H2>
  22. <P><B>#include &lt;pthread.h&gt;</B>
  23. </P>
  24. <P><B>pthread_cond_t </B><I>cond</I> <B>= PTHREAD_COND_INITIALIZER;</B>
  25. </P>
  26. <P><B>int pthread_cond_init(pthread_cond_t *</B><I>cond</I><B>,
  27. pthread_condattr_t *</B><I>cond_attr</I><B>);</B>
  28. </P>
  29. <P><B>int pthread_cond_signal(pthread_cond_t *</B><I>cond</I><B>);</B>
  30. </P>
  31. <P><B>int pthread_cond_broadcast(pthread_cond_t *</B><I>cond</I><B>);</B>
  32. </P>
  33. <P><B>int pthread_cond_wait(pthread_cond_t *</B><I>cond</I><B>,
  34. pthread_mutex_t *</B><I>mutex</I><B>);</B>
  35. </P>
  36. <P><B>int pthread_cond_timedwait(pthread_cond_t *</B><I>cond</I><B>,
  37. pthread_mutex_t *</B><I>mutex</I><B>, const struct timespec
  38. *</B><I>abstime</I><B>);</B>
  39. </P>
  40. <P><B>int pthread_cond_destroy(pthread_cond_t *</B><I>cond</I><B>);</B>
  41. </P>
  42. <H2><A HREF="#toc2" NAME="sect2">Description</A></H2>
  43. <P>A condition (short for ‘‘condition variable’’) is a
  44. synchronization device that allows threads to suspend execution and
  45. relinquish the processors until some predicate on shared data is
  46. satisfied. The basic operations on conditions are: signal the
  47. condition (when the predicate becomes true), and wait for the
  48. condition, suspending the thread execution until another thread
  49. signals the condition.
  50. </P>
  51. <P>A condition variable must always be associated with a mutex, to
  52. avoid the race condition where a thread prepares to wait on a
  53. condition variable and another thread signals the condition just
  54. before the first thread actually waits on it.
  55. </P>
  56. <P><B>pthread_cond_init</B> initializes the condition variable <I>cond</I>,
  57. using the condition attributes specified in <I>cond_attr</I>, or
  58. default attributes if <I>cond_attr</I> is <B>NULL</B>.
  59. </P>
  60. <P>Variables of type <B>pthread_cond_t</B> can also be initialized
  61. statically, using the constant <B>PTHREAD_COND_INITIALIZER</B>. In
  62. the <B>Pthreads-w32</B> implementation, an application should still
  63. call <B>pthread_cond_destroy</B> at some point to ensure that any
  64. resources consumed by the condition variable are released.</P>
  65. <P><B>pthread_cond_signal</B> restarts one of the threads that are
  66. waiting on the condition variable <I>cond</I>. If no threads are
  67. waiting on <I>cond</I>, nothing happens. If several threads are
  68. waiting on <I>cond</I>, exactly one is restarted, but it is not
  69. specified which.
  70. </P>
  71. <P><B>pthread_cond_broadcast</B> restarts all the threads that are
  72. waiting on the condition variable <I>cond</I>. Nothing happens if no
  73. threads are waiting on <I>cond</I>.
  74. </P>
  75. <P><B>pthread_cond_wait</B> atomically unlocks the <I>mutex</I> (as
  76. per <B>pthread_unlock_mutex</B>) and waits for the condition variable
  77. <I>cond</I> to be signalled. The thread execution is suspended and
  78. does not consume any CPU time until the condition variable is
  79. signalled. The <I>mutex</I> must be locked by the calling thread on
  80. entrance to <B>pthread_cond_wait</B>. Before returning to the calling
  81. thread, <B>pthread_cond_wait</B> re-acquires <I>mutex</I> (as per
  82. <B>pthread_lock_mutex</B>).
  83. </P>
  84. <P>Unlocking the mutex and suspending on the condition variable is
  85. done atomically. Thus, if all threads always acquire the mutex before
  86. signalling the condition, this guarantees that the condition cannot
  87. be signalled (and thus ignored) between the time a thread locks the
  88. mutex and the time it waits on the condition variable.
  89. </P>
  90. <P><B>pthread_cond_timedwait</B> atomically unlocks <I>mutex</I> and
  91. waits on <I>cond</I>, as <B>pthread_cond_wait</B> does, but it also
  92. bounds the duration of the wait. If <I>cond</I> has not been
  93. signalled within the amount of time specified by <I>abstime</I>, the
  94. mutex <I>mutex</I> is re-acquired and <B>pthread_cond_timedwait</B>
  95. returns the error <B>ETIMEDOUT</B>. The <I>abstime</I> parameter
  96. specifies an absolute time, with the same origin as <A HREF="time.html"><B>time</B>(2)</A>
  97. and <A HREF="gettimeofday.html"><B>gettimeofday</B>(2)</A>.
  98. </P>
  99. <P><B>pthread_cond_destroy</B> destroys a condition variable, freeing
  100. the resources it might hold. No threads must be waiting on the
  101. condition variable on entrance to <B>pthread_cond_destroy</B>.</P>
  102. <H2><A HREF="#toc3" NAME="sect3">Cancellation</A></H2>
  103. <P><B>pthread_cond_wait</B> and <B>pthread_cond_timedwait</B> are
  104. cancellation points. If a thread is cancelled while suspended in one
  105. of these functions, the thread immediately resumes execution, then
  106. locks again the <I>mutex</I> argument to <B>pthread_cond_wait</B> and
  107. <B>pthread_cond_timedwait</B>, and finally executes the cancellation.
  108. Consequently, cleanup handlers are assured that <I>mutex</I> is
  109. locked when they are called.
  110. </P>
  111. <H2><A HREF="#toc4" NAME="sect4">Async-signal Safety</A></H2>
  112. <P>The condition functions are not async-signal safe, and should not
  113. be called from a signal handler. In particular, calling
  114. <B>pthread_cond_signal</B> or <B>pthread_cond_broadcast</B> from a
  115. signal handler may deadlock the calling thread.
  116. </P>
  117. <H2><A HREF="#toc5" NAME="sect5">Return Value</A></H2>
  118. <P>All condition variable functions return 0 on success and a
  119. non-zero error code on error.
  120. </P>
  121. <H2><A HREF="#toc6" NAME="sect6">Errors</A></H2>
  122. <P><B>pthread_cond_init</B>, <B>pthread_cond_signal</B>,
  123. <B>pthread_cond_broadcast</B>, and <B>pthread_cond_wait</B> never
  124. return an error code.
  125. </P>
  126. <P>The <B>pthread_cond_init</B> function returns the following error
  127. codes on error:
  128. </P>
  129. <DL>
  130. <DL>
  131. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  132. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  133. The <I>cond</I> argument is invalid.
  134. </DD><DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  135. <B>ENOMEM</B>
  136. </DT></DL>
  137. </DL>
  138. <BLOCKQUOTE STYLE="margin-left: 4cm">
  139. There was not enough memory to allocate the condition variable.
  140. </BLOCKQUOTE>
  141. <P>The <B>pthread_cond_signal</B> function returns the following
  142. error codes on error:
  143. </P>
  144. <DL>
  145. <DL>
  146. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  147. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  148. The <I>cond</I> argument is invalid.
  149. </DD></DL>
  150. </DL>
  151. <P>
  152. The <B>pthread_cond_broadcast</B> function returns the following
  153. error codes on error:
  154. </P>
  155. <DL>
  156. <DL>
  157. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  158. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  159. The <I>cond</I> argument is invalid.
  160. </DD></DL>
  161. </DL>
  162. <P>
  163. The <B>pthread_cond_wait</B> function returns the following error
  164. codes on error:
  165. </P>
  166. <DL>
  167. <DL>
  168. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  169. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  170. The <I>cond</I> argument is invalid.
  171. </DD><DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  172. <B>ENOMEM</B>
  173. </DT></DL>
  174. </DL>
  175. <BLOCKQUOTE STYLE="margin-left: 4cm">
  176. There was not enough memory to allocate the statically initialised
  177. condition variable. Statically initialised condition variables are
  178. dynamically allocated by the first thread to wait on them.</BLOCKQUOTE>
  179. <P>The <B>pthread_cond_timedwait</B> function returns the following
  180. error codes on error:
  181. </P>
  182. <DL>
  183. <DL>
  184. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  185. </DT></DL>
  186. </DL>
  187. <P STYLE="margin-left: 2cm">
  188. The <I>cond</I> argument is invalid.
  189. </P>
  190. <DL>
  191. <DL>
  192. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>ETIMEDOUT</B>
  193. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  194. The condition variable was not signalled before the timeout
  195. specified by <I>abstime</I>
  196. </DD><DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  197. <B>ENOMEM</B>
  198. </DT></DL>
  199. </DL>
  200. <BLOCKQUOTE STYLE="margin-left: 4cm">
  201. There was not enough memory to allocate the statically initialised
  202. condition variable. Statically initialised condition variables are
  203. dynamically allocated by the first thread to wait on them.
  204. </BLOCKQUOTE>
  205. <P>The <B>pthread_cond_destroy</B> function returns the following
  206. error code on error:
  207. </P>
  208. <DL>
  209. <DL>
  210. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EINVAL</B>
  211. </DT></DL>
  212. </DL>
  213. <P STYLE="margin-left: 2cm; margin-right: 1cm">
  214. The <I>cond</I> argument is invalid.
  215. </P>
  216. <DL>
  217. <DL>
  218. <DT STYLE="margin-right: 1cm; margin-bottom: 0.5cm"><B>EBUSY</B>
  219. </DT><DD STYLE="margin-right: 1cm; margin-bottom: 0.5cm">
  220. Some threads are currently waiting on <I>cond</I>.
  221. </DD></DL>
  222. </DL>
  223. <H2>
  224. <A HREF="#toc7" NAME="sect7">Author</A></H2>
  225. <P>Xavier Leroy &lt;[email protected]&gt;
  226. </P>
  227. <P>Modified by Ross Johnson for use with <A HREF="http://sources.redhat.com/pthreads-win32">Pthreads-w32</A>.</P>
  228. <H2><A HREF="#toc8" NAME="sect8">See Also</A></H2>
  229. <P><A HREF="pthread_condattr_init.html"><B>pthread_condattr_init</B>(3)</A>
  230. , <A HREF="pthread_mutex_lock.html"><B>pthread_mutex_lock</B>(3)</A>
  231. , <A HREF="pthread_mutex_unlock.html"><B>pthread_mutex_unlock</B>(3)</A>
  232. , <A HREF="pthread_cancel.html"><B>pthread_cancel(3)</B></A>.
  233. </P>
  234. <H2><A HREF="#toc9" NAME="sect9">Example</A></H2>
  235. <P>Consider two shared variables <I>x</I> and <I>y</I>, protected by
  236. the mutex <I>mut</I>, and a condition variable <I>cond</I> that is to
  237. be signaled whenever <I>x</I> becomes greater than <I>y</I>.
  238. </P>
  239. <PRE STYLE="margin-left: 1cm; margin-right: 1cm">int x,y;
  240. pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  241. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;</PRE><BLOCKQUOTE>
  242. Waiting until <I>x</I> is greater than <I>y</I> is performed as
  243. follows:
  244. </BLOCKQUOTE>
  245. <PRE STYLE="margin-left: 1.01cm">pthread_mutex_lock(&amp;mut);
  246. while (x &lt;= y) {
  247. pthread_cond_wait(&amp;cond, &amp;mut);
  248. }
  249. /* operate on x and y */
  250. pthread_mutex_unlock(&amp;mut);</PRE><BLOCKQUOTE STYLE="margin-left: 3.01cm">
  251. Modifications on <I>x</I> and <I>y</I> that may cause <I>x</I> to
  252. become greater than <I>y</I> should signal the condition if needed:
  253. </BLOCKQUOTE>
  254. <PRE STYLE="margin-left: 1.01cm">pthread_mutex_lock(&amp;mut);
  255. /* modify x and y */
  256. if (x &gt; y) pthread_cond_broadcast(&amp;cond);
  257. pthread_mutex_unlock(&amp;mut);</PRE><BLOCKQUOTE STYLE="margin-left: 3.01cm">
  258. If it can be proved that at most one waiting thread needs to be waken
  259. up (for instance, if there are only two threads communicating through
  260. <I>x</I> and <I>y</I>), <B>pthread_cond_signal</B> can be used as a
  261. slightly more efficient alternative to <B>pthread_cond_broadcast</B>.
  262. If in doubt, use <B>pthread_cond_broadcast</B>.
  263. </BLOCKQUOTE>
  264. <BLOCKQUOTE STYLE="margin-left: 3.01cm">To wait for <I>x</I> to
  265. become greater than <I>y</I> with a timeout of 5 seconds, do:
  266. </BLOCKQUOTE>
  267. <PRE STYLE="margin-left: 1.01cm">struct timeval now;
  268. struct timespec timeout;
  269. int retcode;
  270. pthread_mutex_lock(&amp;mut);
  271. gettimeofday(&amp;now);
  272. timeout.tv_sec = now.tv_sec + 5;
  273. timeout.tv_nsec = now.tv_usec * 1000;
  274. retcode = 0;
  275. while (x &lt;= y &amp;&amp; retcode != ETIMEDOUT) {
  276. retcode = pthread_cond_timedwait(&amp;cond, &amp;mut, &amp;timeout);
  277. }
  278. if (retcode == ETIMEDOUT) {
  279. /* timeout occurred */
  280. } else {
  281. /* operate on x and y */
  282. }
  283. pthread_mutex_unlock(&amp;mut);</PRE>
  284. <HR>
  285. <BLOCKQUOTE STYLE="margin-left: 0cm; margin-right: 0cm"><A NAME="toc"></A>
  286. <B>Table of Contents</B></BLOCKQUOTE>
  287. <UL>
  288. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect0" NAME="toc0">Name</A>
  289. </BLOCKQUOTE>
  290. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect1" NAME="toc1">Synopsis</A>
  291. </BLOCKQUOTE>
  292. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect2" NAME="toc2">Description</A>
  293. </BLOCKQUOTE>
  294. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect3" NAME="toc3">Cancellation</A>
  295. </BLOCKQUOTE>
  296. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect4" NAME="toc4">Async-signal
  297. Safety</A>
  298. </BLOCKQUOTE>
  299. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect5" NAME="toc5">Return
  300. Value</A>
  301. </BLOCKQUOTE>
  302. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect6" NAME="toc6">Errors</A>
  303. </BLOCKQUOTE>
  304. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect7" NAME="toc7">Author</A>
  305. </BLOCKQUOTE>
  306. <LI><BLOCKQUOTE STYLE="margin-right: 0cm; margin-bottom: 0cm"><A HREF="#sect8" NAME="toc8">See
  307. Also</A>
  308. </BLOCKQUOTE>
  309. <LI><BLOCKQUOTE STYLE="margin-right: 0cm"><A HREF="#sect9" NAME="toc9">Example</A>
  310. </BLOCKQUOTE>
  311. </UL>
  312. </BODY>
  313. </HTML>