signature LOCK structure Lock : LOCK
The Lock structure defines a simple means to implement monitor-like synchronisation of arbitrary sets of functions.
See the example at the and of this page for a common idiom for building up such synchronisation.
Imported implicitly.
signature LOCK = sig type lock type t = lock val lock : unit -> lock val sync : lock -> ('a -> 'b) -> ('a -> 'b) end
The type of synchronisation locks.
Creates a new lock.
Returns a function f', that has the same behaviour as f, but is synchronised with respect to lock. Only one function per lock can be evaluated at one time, other threads will block until that function returns. Locking is not reentrant.
The following structure provides two synchronised functions even and odd. Only one thread can run within the implementation of these functions.
signature MONITOR = sig val even : int -> unit val odd : int -> unit end structure Monitor : MONITOR = struct fun even 0 = () | even n = odd(n-1) and odd 0 = () | odd n = even(n-1) val lock = Lock.lock() val even = Lock.sync lock even val odd = Lock.sync lock odd end