Rev 57 | Rev 72 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 57 | Rev 62 | ||
---|---|---|---|
Line 293... | Line 293... | ||
293 | that allows events to be lost when there is no thread waiting for them. |
293 | that allows events to be lost when there is no thread waiting for them. |
294 | In order to support this, condition variables don't use direct hand-off |
294 | In order to support this, condition variables don't use direct hand-off |
295 | and operate in a way similar to the example below. A thread waiting for |
295 | and operate in a way similar to the example below. A thread waiting for |
296 | the condition becoming true does the following:</para> |
296 | the condition becoming true does the following:</para> |
297 | 297 | ||
- | 298 | <example> |
|
- | 299 | <title>Use of <code>condvar_wait_timeout</code>.</title> |
|
298 | <para><programlisting language="C"><function>mutex_lock</function>(<varname>mtx</varname>); |
300 | <programlisting language="C"><function>mutex_lock</function>(<varname>mtx</varname>); |
299 | while (!<varname>condition</varname>) |
301 | while (!<varname>condition</varname>) |
300 | <function>condvar_wait_timeout</function>(<varname>cv</varname>, <varname>mtx</varname>); |
302 | <function>condvar_wait_timeout</function>(<varname>cv</varname>, <varname>mtx</varname>); |
301 | /* <remark>the condition is true, do something</remark> */ |
303 | /* <remark>the condition is true, do something</remark> */ |
302 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting></para> |
304 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting> |
- | 305 | </example> |
|
303 | 306 | ||
304 | <para>A thread that causes the condition become true signals this event |
307 | <para>A thread that causes the condition become true signals this event |
305 | like this:</para> |
308 | like this:</para> |
306 | 309 | ||
- | 310 | <example> |
|
- | 311 | <title>Use of <code>condvar_signal</code>.</title> |
|
307 | <para><programlisting><function>mutex_lock</function>(<varname>mtx</varname>); |
312 | <programlisting><function>mutex_lock</function>(<varname>mtx</varname>); |
308 | <varname>condition</varname> = <constant>true</constant>; |
313 | <varname>condition</varname> = <constant>true</constant>; |
309 | <function>condvar_signal</function>(<varname>cv</varname>); /* <remark>condvar_broadcast(cv);</remark> */ |
314 | <function>condvar_signal</function>(<varname>cv</varname>); /* <remark>condvar_broadcast(cv);</remark> */ |
310 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting></para> |
315 | <function>mutex_unlock</function>(<varname>mtx</varname>);</programlisting></example> |
311 | 316 | ||
312 | <para>The wait operation, <code>condvar_wait_timeout</code>, always puts |
317 | <para>The wait operation, <code>condvar_wait_timeout</code>, always puts |
313 | the calling thread to sleep. The thread then sleeps until another thread |
318 | the calling thread to sleep. The thread then sleeps until another thread |
314 | invokes <code>condvar_broadcast</code> on the same condition variable or |
319 | invokes <code>condvar_broadcast</code> on the same condition variable or |
315 | until it is woken up by <code>condvar_signal</code>. The |
320 | until it is woken up by <code>condvar_signal</code>. The |