package algaeff

  1. Overview
  2. Docs

Effects for making concurrent execution immediately fail.

module M = Algaeff.Mutex.Make ()

let () = M.run @@ fun () ->
  let ten = M.exclusively @@ fun () -> 10 in
  let nine = M.exclusively @@ fun () -> 9 in
  (* this will print out 19 *)
  print_int (ten + nine)

let _ = M.run @@ fun () ->
  M.exclusively @@ fun () ->
  (* this raise M.Locked *)
  M.exclusively @@ fun () ->
  100

Note that the exception S.Locked would be immediately raised for any attempt to lock an already locked mutex. The typical application of this component is to prevent erroneous concurrent API access, not to provide synchronization. Therefore, no waiting would happen.

It is impossible to implement meaningful synchronization unless this module also handles lightweight threading. For applications that need synchronization between lightweight threads (so that one thread would wait for another thread to unlock the mutex), check out other libraries such as the Eio and Affect.

module type S = sig ... end

The signature of mutex effects.

module Make () : S

The implementation of mutex effects. Make is generative so that one can use multiple mutexes at the same time.

OCaml

Innovation. Community. Security.