Stdlib.Mutex(3)
πΈ Stdlib.Mutex β the manual page, but make it make sense πΈ
okay so this is Stdlib.Mutex, straight out of the OCaml library man pages (manual name: Stdlib.Mutex(3), iykyk). bestie, it's the mutex module. it's giving "concurrency safety era" and honestly? we're obsessed. π
β¨ NAME β¨
Stdlib.Mutex β no description
like... literally no description in the original. iconic of them to say nothing. slay I guess. π
π¦ Module π¦
Module Stdlib.Mutex
π Documentation π
Module Mutex : (module Stdlib__Mutex)
π§ the type, the moment, the icon
type t
The type of mutexes. that's it. that's the type. main character energy fr. β¨
π₯ val create : unit -> t
Returns a new mutex. fresh out the box, no cap. π«Ά
π val lock : t -> unit
Locks the given mutex. here's the tea: only one thread can have the mutex locked at any time. so if a thread tries to lock a mutex that's already locked by another thread? it just suspends β sits there, waiting, patient, until the other thread unlocks it. lowkey tragic but that's the whole point. π
Before 4.12 β° β Sys_error was not raised for recursive locking (and it was platform-dependent behaviour, which is giving chaos π).
Raises Sys_error if the mutex is already locked by the thread calling Mutex.lock. so like, don't do that. π
β val try_lock : t -> bool
Same as Mutex.lock, but with boundaries. it does not suspend the calling thread if the mutex is already locked β it just returns false immediately in that case. like a vibe check that fails fast. ngl very considerate. π
If the mutex is unlocked, it locks it and returns true. easy. periodt.
πΈ val unlock : t -> unit
Unlocks the given mutex. other threads that were suspended trying to lock it will restart β the group chat comes back online. π¦
Important, bestie: the mutex must have been previously locked by the thread that calls Mutex.unlock. like, who locked you?? has to be you. π
Before 4.12 β° β Sys_error was not raised when unlocking an unlocked mutex, or when unlocking a mutex from a different thread. wild times.
Raises Sys_error if the mutex is unlocked, or if it was locked by another thread. caught in 4k. π
π val protect : t -> (unit -> 'a) -> 'a
protect mutex f runs f() in a critical section where mutex is locked (using Mutex.lock). then it takes care of releasing mutex β whether f() returned a value or raised an exception. it's the responsible friend who makes sure everyone gets home safe. π«Ά
and the unlocking operation is guaranteed to always take place, even in the event an asynchronous exception (e.g. Sys.Break) is raised in some signal handler. no leaks, no drama, we love to see it. πβ¨
Since 5.1 π
lowkey the whole module is just four functions and a type but it's carrying the entire concurrency situation on its back. respect. π
and that's the page!! π source/version vibes: OCamldoc, dated 2026-06-22, filed under Stdlib.Mutex(3). go forth and lock responsibly 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