OCaml threads context switch example


/ Published in: Other
Save to your folder(s)

<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>


Copy this code and paste it in your HTML
  1. let allocation _ = ignore (String.create 100000)
  2.  
  3. let r = ref false;;
  4.  
  5. let rec forever x = allocation (); r:=true; forever x;;
  6.  
  7. let rec wait _ = if !r then print_string "end.\n" else (allocation (); wait ()) ;;
  8.  
  9. Thread.create (fun _ -> forever ()) ();;
  10.  
  11. wait ();; (* blocks forever if we comment out both occurrence of `allocation' *)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.