containers.thread
CCSemaphore
containers
containers.data
containers.iter
containers.monomorphic
containers.sexp
containers.top
containers.unix
type t
A semaphore
val create : int -> t
create n creates a semaphore with initial value n.
create n
n
Invalid_argument
if n <= 0.
n <= 0
val get : t -> int
Current value.
val acquire : int -> t -> unit
acquire n s blocks until get s >= n, then atomically sets s := !s - n.
acquire n s
get s >= n
s := !s - n
val release : int -> t -> unit
release n s atomically sets s := !s + n.
release n s
s := !s + n
val with_acquire : n:int -> t -> f:(unit -> 'a) -> 'a
with_acquire ~n s ~f first acquires s with n units, calls f (), and then releases s with n units. Safely release the semaphore even if f () fails.
with_acquire ~n s ~f
s
f ()
val wait_until_at_least : n:int -> t -> f:(unit -> 'a) -> 'a
wait_until_at_least ~n s ~f waits until get s >= n, then calls f () and returns its result. Doesn't modify the semaphore.
wait_until_at_least ~n s ~f