alice
library
manual.

Alice Project

The Lock structure


________ Synopsis ____________________________________________________

    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.

See also: Future, Thread


________ Import ______________________________________________________

Imported implicitly.


________ Interface ___________________________________________________

    signature LOCK =
    sig
	type lock
	type t = lock

	val lock : unit -> lock
	val sync : lock -> ('a -> 'b) -> ('a -> 'b)
    end
  

________ Description _________________________________________________

type lock
type t = alt

The type of synchronisation locks.

lock ()

Creates a new lock.

sync lock f

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.


________ Example _____________________________________________________

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
  


last modified 1970/01/01 01:00