mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-17 08:06:43 -05:00
2 lines
No EOL
3.9 KiB
HTML
2 lines
No EOL
3.9 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Semaphore (ocaml.Stdlib.Semaphore)</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> » <a href="../index.html">Stdlib</a> » Semaphore</nav><header class="odoc-preamble"><h1>Module <code><span>Stdlib.Semaphore</span></code></h1><p>Semaphores</p><p>A semaphore is a thread synchronization device that can be used to control access to a shared resource.</p><p>Two flavors of semaphores are provided: counting semaphores and binary semaphores.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.12</li></ul></header><nav class="odoc-toc"><ul><li><a href="#counting-semaphores">Counting semaphores</a></li><li><a href="#binary-semaphores">Binary semaphores</a></li></ul></nav><div class="odoc-content"><h3 id="counting-semaphores"><a href="#counting-semaphores" class="anchor"></a>Counting semaphores</h3><p>A counting semaphore is a counter that can be accessed concurrently by several threads. The typical use is to synchronize producers and consumers of a resource by counting how many units of the resource are available.</p><p>The two basic operations on semaphores are:</p><ul><li>"release" (also called "V", "post", "up", and "signal"), which increments the value of the counter. This corresponds to producing one more unit of the shared resource and making it available to others.</li><li>"acquire" (also called "P", "wait", "down", and "pend"), which waits until the counter is greater than zero and decrements it. This corresponds to consuming one unit of the shared resource.</li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.12</li></ul><div class="odoc-spec"><div class="spec module anchored" id="module-Counting"><a href="#module-Counting" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Counting/index.html">Counting</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><h3 id="binary-semaphores"><a href="#binary-semaphores" class="anchor"></a>Binary semaphores</h3><p>Binary semaphores are a variant of counting semaphores where semaphores can only take two values, 0 and 1.</p><p>A binary semaphore can be used to control access to a single shared resource, with value 1 meaning "resource is available" and value 0 meaning "resource is unavailable".</p><p>The "release" operation of a binary semaphore sets its value to 1, and "acquire" waits until the value is 1 and sets it to 0.</p><p>A binary semaphore can be used instead of a mutex (see module <a href="../Mutex/index.html"><code>Mutex</code></a>) when the mutex discipline (of unlocking the mutex from the thread that locked it) is too restrictive. The "acquire" operation corresponds to locking the mutex, and the "release" operation to unlocking it, but "release" can be performed in a thread different than the one that performed the "acquire". Likewise, it is safe to release a binary semaphore that is already available.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.12</li></ul><div class="odoc-spec"><div class="spec module anchored" id="module-Binary"><a href="#module-Binary" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Binary/index.html">Binary</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div></div></body></html> |