Semaphore.Binary(3)

πŸ’… Semaphore.Binary(3) β€” the OCaml library page where your girl explains binary semaphores

okay so bestie, welcome to Semaphore.Binary from the OCaml library manual β€” yes the whole thing is filed under Semaphore.Binary(3), and ngl the NAME section literally just says "no description", which is so mysterious of it. don't worry though, we're about to make it make sense πŸ’–


πŸŽ€ NAME πŸŽ€

Semaphore.Binary β€” no description 😭

like... that's it. that's the name section. the manpage said "no description" and honestly? iconic behaviour. but keep reading, because the good stuff is right there.


πŸ¦‹ Module πŸ¦‹

the module is, wait for it... Module Semaphore.Binary 🫢


✨ Documentation ✨

🌸 Module Binary 🌸

: sig end

it's a little signature block, that's the whole vibe. now let's meet the types and functions, because this is where the main character energy kicks in πŸ‘‘


πŸ’œ type t

this is the type of binary semaphores β€” periodt. it's the thing you're holding, the thing you're passing around, the whole point of the module. binary, as in two states, as in 0 or 1, as in available or unavailable, iykyk πŸŽ€


πŸ“ val make : bool -> t

make b returns a new binary semaphore, freshly made, still warm ✨

  • if b is true, the initial value of the semaphore is 1, which means "available" πŸ’–
  • if b is false, the initial value of the semaphore is 0, which means "unavailable" πŸ’€

so like, the boolean is literally just "start open or start closed". that's the whole decision. lowkey elegant ngl.


🌈 val release : t -> unit

release s sets the value of semaphore s to 1, putting it in the "available" state. and the bestie part? if other threads are waiting on s, then one of them is restarted 🫢

so you're not just unlocking the door β€” you're waking up one person in the queue to walk through it. one. not all of them. don't get it twisted, this is binary, not a party πŸŽ‰


🧚 val acquire : t -> unit

acquire s blocks the calling thread until the semaphore s has value 1 (like, is available), then it atomically sets it to 0 and returns πŸ’…

the key word there is atomically β€” check-and-grab happens in one indivisible step, so no two threads can both think they got it. no cap, that's the whole safety of the thing.

πŸ’€ warning vibe: this function blocks. if the semaphore is at 0 and nobody releases it, your thread is just... sitting there. waiting. thinking about its life. so only call it when you know someone's gonna release, bestie 😭

πŸ’… val try_acquire : t -> bool

try_acquire s is the impatient icon of this module β€” it immediately returns false if the semaphore s has value 0. no blocking, no waiting, no main-character patience required πŸ™ˆ

but if s has value 1? its value is atomically set to 0 and try_acquire s returns true ⭐

so it's the same grab as acquire, just with zero willingness to wait. get it now or get told no. slay.


and that's the whole page, bestie β€” four little functions and a type, and yet it's the entire foundation of "who gets to go next" in concurrent OCaml 🌸 go forth and semaphore responsibly ✨


πŸ’œ this page came to you courtesy of OCamldoc, dated 2026-06-22, and it's officially filed as Semaphore.Binary(3) β€” mwah, bye bestie πŸ«Άβœ¨πŸ’–

πŸ“¦ where this page comes from