.TH "Condition" 3o 2023-09-18 OCamldoc "OCaml library" .SH NAME Condition \- Condition variables to synchronize between threads. .SH Module Module Condition .SH Documentation .sp Module .BI "Condition" : .B sig end .sp Condition variables to synchronize between threads\&. .sp Condition variables are used when one thread wants to wait until another thread has finished doing something: the former thread \&'waits\&' on the condition variable, the latter thread \&'signals\&' the condition when it is done\&. Condition variables should always be protected by a mutex\&. The typical use is (if .ft B D .ft R is a shared data structure, .ft B m .ft R its mutex, and .ft B c .ft R is a condition variable): .EX .ft B .br \& Mutex\&.lock m; .br \& while (* some predicate P over D is not satisfied *) do .br \& Condition\&.wait c m .br \& done; .br \& (* Modify D *) .br \& if (* the predicate P over D is now satisfied *) then Condition\&.signal c; .br \& Mutex\&.unlock m .br \& .ft R .EE .sp .sp .sp .I type t .sp The type of condition variables\&. .sp .I val create : .B unit -> t .sp Return a new condition variable\&. .sp .I val wait : .B t -> Mutex.t -> unit .sp .ft B wait c m .ft R atomically unlocks the mutex .ft B m .ft R and suspends the calling process on the condition variable .ft B c .ft R \&. The process will restart after the condition variable .ft B c .ft R has been signalled\&. The mutex .ft B m .ft R is locked again before .ft B wait .ft R returns\&. .sp .I val signal : .B t -> unit .sp .ft B signal c .ft R restarts one of the processes waiting on the condition variable .ft B c .ft R \&. .sp .I val broadcast : .B t -> unit .sp .ft B broadcast c .ft R restarts all processes waiting on the condition variable .ft B c .ft R \&. .sp