last update:2010-01-13
"Concurrent Cell" is a synchronous message passing style multi-thread utility library for objective caml. The library has basic inter-thread shared variables(ivar, mvar) and more rich protocols such as queue, broadcast, rpc similar to John Reppy's Concurrent ML. Features are as follows:
(* to compile: ocamlfind ocamlc -thread -package ccell -linkpkg example.ml *)
open Ccell
open Event
(* for util *)
let spawn_loop f x =
let rec forever f x =
let v = f x in forever f v
in
ignore (Thread.create (forever f) x)
(* make a random number generator thread with broadcast channel *)
let make_server () =
let broadcast_channel =
Bcast.make ()
in
let server () =
Bcast.send broadcast_channel (Random.float 1.);
Thread.delay 0.5
in
spawn_loop server ();
broadcast_channel
(* make a random number printer client *)
let make_client broadcast_channel =
let port =
Bcast.make_port broadcast_channel
in
let client () =
Format.printf "%f\n@?" (sync (Bcast.receive_port port))
in
spawn_loop client ()
let _ =
(* setup *)
Random.self_init ();
let channel =
make_server ()
in
make_client channel;
make_client channel;
sync (never ())
result:
/home/user/svns/ccell> ./example 0.032788 0.032788 0.237625 0.237625 0.863285 0.863285 ^C
Concurrent Cell is pure objective caml library. To compile, Just type:
$ omake $ omake install;