Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Problems spawning threads
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-12-18 (05:18)
From: Edgar Friendly <thelema314@g...>
Subject: Problems spawning threads
I've gone over the Computer Language Benchmarks Game[1] problems, and I
can't figure out how help the administrators of this problem to get the
OCaml code for the thread-ring benchmark (contributed by Charles Martin)
to work on their computer.

Here's the code:

let size = 503

let n = int_of_string Sys.argv.(1)

let run id ichan ochan =
  let rec loop () =
    let token = Event.sync (Event.receive ichan) in
    if token = n then (print_int id; print_newline (); exit 0)
    else (Event.sync (Event.send ochan (token + 1)); loop ())
  in Thread.create loop ()

let () =
  let channels =
    Array.init size
      (fun _ -> Event.new_channel ()) in
  let threads =
    Array.init size
      (fun i -> run (i + 1) channels.(i) channels.((i + 1) mod size)) in
  Event.sync (Event.send channels.(0) 0);
  Thread.join threads.(0)

Here's the compilation and execution:

~/tmp/testzone $ /usr/bin/ocamlopt -thread -noassert -unsafe
-I /usr/lib/ocaml/contrib -ccopt -O3 -ccopt -fomit-frame-pointer
-ccopt -march=pentium4 unix.cmxa threads.cmxa -o

~/tmp/testzone $ ./threadring.ocaml_run 100
Fatal error: exception Sys_error("Thread.create: Cannot
allocate memory")

The code runs fine when compiled with ocamlc -vmthread ....  It works
fine on my Athlon with the same compilation options (except for

Interestingly enough, the administrator reports that the code works with
size=16, but fails for size=17.  Any ideas what's going on or how to fix
it?  Hopefully I'll have some confirmation soon that the (probably
useless) ccopts and other optimization options don't affect the error.

We also checked ulimit settings - his max user processes and stack size
are both higher than mine. (-u 4095, -s 160000k)  Any other ulimit
options that could interfere?  Last piece of information: his box runs
Gentoo Linux.