moonpool/dev/ocaml/Stdlib/Mutex/index.html
2023-08-29 18:39:53 +00:00

4 lines
No EOL
4.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Mutex (ocaml.Stdlib.Mutex)</title><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><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> &#x00BB; <a href="../index.html">Stdlib</a> &#x00BB; Mutex</nav><header class="odoc-preamble"><h1>Module <code><span>Stdlib.Mutex</span></code></h1><p>Locks for mutual exclusion.</p><p>Mutexes (mutual-exclusion locks) are used to implement critical sections and protect shared mutable data structures against concurrent accesses. The typical use is (if <code>m</code> is the mutex associated with the data structure <code>D</code>):</p><pre class="language-ocaml"><code>Mutex.lock m;
(* Critical section that operates over D *);
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 mutexes.</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">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Return a new mutex.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-lock"><a href="#val-lock" class="anchor"></a><code><span><span class="keyword">val</span> lock : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Lock the given mutex. Only one thread can have the mutex locked at any time. A thread that attempts to lock a mutex already locked by another thread will suspend until the other thread unlocks the mutex.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Sys_error</span> <p>if the mutex is already locked by the thread calling <a href="#val-lock"><code>Mutex.lock</code></a>.</p></li></ul><ul class="at-tags"><li class="before"><span class="at-tag">before</span> <span class="value">4.12</span> <p><a href="../index.html#exception-Sys_error"><code>Sys_error</code></a> was not raised for recursive locking (platform-dependent behaviour)</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-try_lock"><a href="#val-try_lock" class="anchor"></a><code><span><span class="keyword">val</span> try_lock : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p>Same as <a href="#val-lock"><code>Mutex.lock</code></a>, but does not suspend the calling thread if the mutex is already locked: just return <code>false</code> immediately in that case. If the mutex is unlocked, lock it and return <code>true</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-unlock"><a href="#val-unlock" class="anchor"></a><code><span><span class="keyword">val</span> unlock : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Unlock the given mutex. Other threads suspended trying to lock the mutex will restart. The mutex must have been previously locked by the thread that calls <a href="#val-unlock"><code>Mutex.unlock</code></a>.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Sys_error</span> <p>if the mutex is unlocked or was locked by another thread.</p></li></ul><ul class="at-tags"><li class="before"><span class="at-tag">before</span> <span class="value">4.12</span> <p><a href="../index.html#exception-Sys_error"><code>Sys_error</code></a> was not raised when unlocking an unlocked mutex or when unlocking a mutex from a different thread.</p></li></ul></div></div></div></body></html>