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:
64-bit Ubuntu quad core
The OCaml native-code compiler, version 4.06.0


Sun, 05 Nov 2017 22:41:18 GMT

MAKE:
mv threadring.ocaml-3.ocaml threadring.ocaml-3.ml
/opt/src/ocaml-4.06.0/bin/ocamlopt -noassert -unsafe -fPIC -nodynlink -inline 100 -O3 -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.22s to complete and log all make actions

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

PROGRAM OUTPUT:
292