1Semaphore(3) OCaml library Semaphore(3)
2
3
4
6 Semaphore - Semaphores
7
9 Module Semaphore
10
12 Module Semaphore
13 : sig end
14
15
16 Semaphores
17
18 A semaphore is a thread synchronization device that can be used to con‐
19 trol access to a shared resource.
20
21 Two flavors of semaphores are provided: counting semaphores and binary
22 semaphores.
23
24
25 Since 4.12
26
27
28
29
30
31
32
33 Counting semaphores
34 A counting semaphore is a counter that can be accessed concurrently by
35 several threads. The typical use is to synchronize producers and con‐
36 sumers of a resource by counting how many units of the resource are
37 available.
38
39 The two basic operations on semaphores are:
40
41 -"release" (also called "V", "post", "up", and "signal"), which incre‐
42 ments the value of the counter. This corresponds to producing one more
43 unit of the shared resource and making it available to others.
44
45 -"acquire" (also called "P", "wait", "down", and "pend"), which waits
46 until the counter is greater than zero and decrements it. This corre‐
47 sponds to consuming one unit of the shared resource.
48
49
50 module Counting : sig end
51
52
53
54
55
56
57 Binary semaphores
58 Binary semaphores are a variant of counting semaphores where semaphores
59 can only take two values, 0 and 1.
60
61 A binary semaphore can be used to control access to a single shared re‐
62 source, with value 1 meaning "resource is available" and value 0 mean‐
63 ing "resource is unavailable".
64
65 The "release" operation of a binary semaphore sets its value to 1, and
66 "acquire" waits until the value is 1 and sets it to 0.
67
68 A binary semaphore can be used instead of a mutex (see module Mutex )
69 when the mutex discipline (of unlocking the mutex from the thread that
70 locked it) is too restrictive. The "acquire" operation corresponds to
71 locking the mutex, and the "release" operation to unlocking it, but
72 "release" can be performed in a thread different than the one that per‐
73 formed the "acquire". Likewise, it is safe to release a binary sema‐
74 phore that is already available.
75
76 module Binary : sig end
77
78
79
80
81
82
83
84OCamldoc 2023-07-20 Semaphore(3)