Semaphore(3)
Semaphore(3) π¦ OCaml library
okay so bestie, this is the Semaphore(3) page from the OCaml library, and we're about to make thread synchronization make sense π
β¨
π NAME π
Semaphore β Semaphores
π¦ Module π¦
Module Semaphore
πΈ Documentation πΈ
π§ Module Semaphore
Module Semaphore : sig end
β¨ Semaphores
A semaphore is a thread synchronization device that can be used to control access to a shared resource. No cap.
Two flavors of semaphores are provided: counting semaphores and binary semaphores. Both iconic, both here π«Ά
Since 4.12 ποΈ
π Counting semaphores
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.
The two basic operations on semaphores are:
release(also calledV,post,up, andsignal) β increments the value of the counter. This corresponds to producing one more unit of the shared resource and making it available to others. πacquire(also calledP,wait,down, andpend) β waits until the counter is greater than zero and decrements it. This corresponds to consuming one unit of the shared resource. π
module Counting : sig end
π Binary semaphores
Binary semaphores are a variant of counting semaphores where semaphores can only take two values, 0 and 1.
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".
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.
A binary semaphore can be used instead of a mutex (see module Mutex ) 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. π«Ά
module Binary : sig end
π Source/version: OCamldoc; date: 2026-06-22; manual: Semaphore(3). Byeee bestie! π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms