1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<h3 class="section">Condition Variables</h3> <p>A <em>condition variable</em> is a way for a thread to block until some event occurs. A thread can wait on a condition variable, to be woken up when some other thread notifies the condition. </p> <p>A condition variable is associated with a mutex and, conceptually, with some condition. For proper operation, the mutex must be acquired, and then a waiting thread must loop, testing the condition and waiting on the condition variable. For example: </p> <div class="example"> <pre class="example">(with-mutex mutex
(while (not global-variable)
(condition-wait cond-var)))
</pre>
</div> <p>The mutex ensures atomicity, and the loop is for robustness—there may be spurious notifications. </p> <p>Similarly, the mutex must be held before notifying the condition. The typical, and best, approach is to acquire the mutex, make the changes associated with this condition, and then notify it: </p> <div class="example"> <pre class="example">(with-mutex mutex
(setq global-variable (some-computation))
(condition-notify cond-var))
</pre>
</div> <dl> <dt id="make-condition-variable">Function: <strong>make-condition-variable</strong> <em>mutex &optional name</em>
</dt> <dd><p>Make a new condition variable associated with <var>mutex</var>. If <var>name</var> is specified, it is a name given to the condition variable. It must be a string. The name is for debugging purposes only; it has no meaning to Emacs. </p></dd>
</dl> <dl> <dt id="condition-variable-p">Function: <strong>condition-variable-p</strong> <em>object</em>
</dt> <dd><p>This function returns <code>t</code> if <var>object</var> represents a condition variable, <code>nil</code> otherwise. </p></dd>
</dl> <dl> <dt id="condition-wait">Function: <strong>condition-wait</strong> <em>cond</em>
</dt> <dd>
<p>Wait for another thread to notify <var>cond</var>, a condition variable. This function will block until the condition is notified, or until a signal is delivered to this thread using <code>thread-signal</code>. </p> <p>It is an error to call <code>condition-wait</code> without holding the condition’s associated mutex. </p> <p><code>condition-wait</code> releases the associated mutex while waiting. This allows other threads to acquire the mutex in order to notify the condition. </p>
</dd>
</dl> <dl> <dt id="condition-notify">Function: <strong>condition-notify</strong> <em>cond &optional all</em>
</dt> <dd>
<p>Notify <var>cond</var>. The mutex with <var>cond</var> must be held before calling this. Ordinarily a single waiting thread is woken by <code>condition-notify</code>; but if <var>all</var> is not <code>nil</code>, then all threads waiting on <var>cond</var> are notified. </p> <p><code>condition-notify</code> releases the associated mutex while waiting. This allows other threads to acquire the mutex in order to wait on the condition. </p>
</dd>
</dl> <dl> <dt id="condition-name">Function: <strong>condition-name</strong> <em>cond</em>
</dt> <dd><p>Return the name of <var>cond</var>, as passed to <code>make-condition-variable</code>. </p></dd>
</dl> <dl> <dt id="condition-mutex">Function: <strong>condition-mutex</strong> <em>cond</em>
</dt> <dd><p>Return the mutex associated with <var>cond</var>. Note that the associated mutex cannot be changed. </p></dd>
</dl><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Condition-Variables.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Condition-Variables.html</a>
</p>
</div>
|