Return to Snippet

Revision: 17595
at September 10, 2009 06:27 by keigoi


Updated Code
let allocation _ = ignore (String.create 100000)

let r = ref false;;

let rec forever x = allocation (); r:=true; forever x;;

let rec wait _ = if !r then print_string "end.\n" else (allocation (); wait ()) ;;

Thread.create (fun _ -> forever ()) ();;

wait ();; (* blocks forever if we comment out both occurrence of `allocation' *)

Revision: 17594
at September 10, 2009 05:18 by keigoi


Updated Code
let r = ref false;;

let rec forever x = (* Thread.yield (); *) r:=true; forever x;;

Thread.create (fun _ -> forever ()) ();;

let rec wait _ = if !r then print_string "end.\n" else ((* Thread.yield (); *) wait ()) ;;

wait ();; (* blocks forever if we comment out either occurrence of `Thread.yield' *)

Revision: 17593
at September 10, 2009 05:17 by keigoi


Updated Code
let r = ref false;;

let rec forever x = (* Thread.yield (); *) r:=true; forever x;;

Thread.create (fun _ -> forever ()) ();;

let rec wait _ = if !r then print_string "end.\n" else ((* Thread.yield (); *) wait ()) ;;

wait ();; (* blocks forever if we comment out either `Thread.yield' *)

Revision: 17592
at September 10, 2009 04:34 by keigoi


Initial Code
let r = ref false;;

let rec forever x = (* Thread.yield (); *) r:=true; forever x;;

Thread.create (fun _ -> forever ()) ();;

let rec wait _ = if !r then print_string "end.\n" else ((* Thread.yield (); *) wait ()) ;;

wait ();; (* blocks forever if we comment out both yield *)

Initial URL


Initial Description
<p>this example checks that when the context switch occurs in the O'Caml runtime.
compile it with:</p>

<ol>
<li>ocamlc -vmthread -I +threads -I unix unix.cmxa threads.cmxa filename.ml</li>
<li>ocamlc -thread -I +threads -I unix unix.cmxa threads.cmxa filename.ml</li>
<li>ocamlopt -thread -I +threads -I unix unix.cmxa threads.cmxa filename.ml</li>
</ol>

<p>both 1. and 2. terminates, while 3. not (on my OCaml 3.11.1, on Mac OS X).</p>

<p><strong>See also:</strong><a href="http://snipplr.com/view/19509/">http://snipplr.com/view/19509/</a></p>

Initial Title
OCaml threads context switch example

Initial Tags


Initial Language
Other