mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-22 09:06:41 -05:00
8 lines
No EOL
3.4 KiB
HTML
8 lines
No EOL
3.4 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Condition (ocaml.Condition)</title><link rel="stylesheet" href="../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> – <a href="../index.html">ocaml</a> » Condition</nav><header class="odoc-preamble"><h1>Module <code><span>Condition</span></code></h1><p>Condition variables to synchronize between threads.</p><p>Condition variables are used when one thread wants to wait until another thread has finished doing something: the former thread 'waits' on the condition variable, the latter thread 'signals' the condition when it is done. Condition variables should always be protected by a mutex. The typical use is (if <code>D</code> is a shared data structure, <code>m</code> its mutex, and <code>c</code> is a condition variable):</p><pre class="language-ocaml"><code>Mutex.lock m;
|
||
while (* some predicate P over D is not satisfied *) do
|
||
Condition.wait c m
|
||
done;
|
||
(* Modify D *)
|
||
if (* the predicate P over D is now satisfied *) then Condition.signal c;
|
||
Mutex.unlock m</code></pre></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span></code></div><div class="spec-doc"><p>The type of condition variables.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-create"><a href="#val-create" class="anchor"></a><code><span><span class="keyword">val</span> create : <span>unit <span class="arrow">-></span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Return a new condition variable.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-wait"><a href="#val-wait" class="anchor"></a><code><span><span class="keyword">val</span> wait : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> <span><a href="../Mutex/index.html#type-t">Mutex.t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>wait c m</code> atomically unlocks the mutex <code>m</code> and suspends the calling process on the condition variable <code>c</code>. The process will restart after the condition variable <code>c</code> has been signalled. The mutex <code>m</code> is locked again before <code>wait</code> returns.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-signal"><a href="#val-signal" class="anchor"></a><code><span><span class="keyword">val</span> signal : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>signal c</code> restarts one of the processes waiting on the condition variable <code>c</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-broadcast"><a href="#val-broadcast" class="anchor"></a><code><span><span class="keyword">val</span> broadcast : <span><a href="#type-t">t</a> <span class="arrow">-></span></span> unit</span></code></div><div class="spec-doc"><p><code>broadcast c</code> restarts all processes waiting on the condition variable <code>c</code>.</p></div></div></div></body></html> |