The Computer Language
Benchmarks Game

thread-ring OCaml #3 program

source code

(* The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
   contributed by Tomasz bla Fortuna *)

let size, n = 503, int_of_string Sys.argv.(1)
let mutex = 
  let f _ = let m = Mutex.create () in Mutex.lock m; m in
  Array.init size f
and data = Array.create size 0

let run id idata odata =
  let rec loop () =
    Mutex.lock mutex.(idata);
    if data.(idata) = n then (
      print_int id; print_newline (); exit 0;
    ) else (
      data.(odata) <- data.(idata) + 1;
      Mutex.unlock mutex.(odata);
      loop ();
    )
  in Thread.create loop ()

let _ =
  let thread =
    Array.init size
      (fun i -> run (i + 1) i ((i + 1) mod size)) in
  Mutex.unlock mutex.(0);
  Thread.join thread.(0)



    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
The OCaml native-code compiler, version 4.03.0


Tue, 26 Apr 2016 02:24:26 GMT

MAKE:
mv threadring.ocaml-3.ocaml threadring.ocaml-3.ml
/usr/local/bin/ocamlopt -noassert -unsafe -nodynlink -inline 100 -thread unix.cmxa threads.cmxa threadring.ocaml-3.ml -o threadring.ocaml-3.ocaml_run
File "threadring.ocaml-3.ml", line 9, characters 11-23:
Warning 3: deprecated: Array.create
Use Array.make instead.
rm threadring.ocaml-3.ml
0.42s to complete and log all make actions

COMMAND LINE:
./threadring.ocaml-3.ocaml_run 50000000

PROGRAM OUTPUT:
292