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