Posted By

keigoi on 09/10/09


Tagged

OCaml


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

keigoi


OCaml threads context switch example


 / Published in: Other
 

this example checks that when the context switch occurs in the O'Caml runtime. compile it with:

  1. ocamlc -vmthread -I +threads -I unix unix.cmxa threads.cmxa filename.ml
  2. ocamlc -thread -I +threads -I unix unix.cmxa threads.cmxa filename.ml
  3. ocamlopt -thread -I +threads -I unix unix.cmxa threads.cmxa filename.ml

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

See also:http://snipplr.com/view/19509/

  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  

You need to login to post a comment.