Return to Snippet

Revision: 17615
at September 10, 2009 21:51 by keigoi


Updated Code
let allocation _ = let rec gen x = if x=0 then [] else x::gen (x-1) in ignore (gen 100000)
let allocation2 _ = ignore (String.create 1000000) (* process runs forever if we use this allocation *)

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 ();;

Revision: 17614
at September 10, 2009 21:50 by keigoi


Updated Code
let allocation _ = let rec gen x = if x=0 then [] else x::gen (x-1) in ignore (gen 100000)
let allocation2 _ = (* ignore (String.create 1000000) *) (* process runs forever if we use this allocation *)

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 ();;

Revision: 17613
at September 10, 2009 12:10 by keigoi


Updated Code
let allocation _ = let rec gen x = if x=0 then [] else x::gen (x-1) in gen 100000
let allocation2 _ = ignore (String.create 1000000) *) (* process runs forever if we use this allocation *)

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 ();;

Revision: 17612
at September 10, 2009 12:09 by keigoi


Updated Code
let allocation _ = let rec gen x = if x=0 then [] else x::gen (x-1) in gen 100000
let allocation2 _ = ignore (String.create 1000000) *) (* process runs forever if we use this allocation *)

let r = ref false;;

let rec forever x = allocation (); Gc.minor (); Gc.major (); r:=true; forever x;;

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

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

wait ();;

Revision: 17611
at September 10, 2009 12:04 by keigoi


Initial Code
let allocation _ = let rec gen x = if x=0 then [] else x::gen (x-1) in gen 100000
let allocation2 _ = ignore (String.create 1000000) *) (* process runs forever if we use this allocation *)

let r = ref false;;

let rec forever x = allocation (); Gc.minor (); Gc.major (); r:=true; forever x;;

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

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

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

Initial URL


Initial Description
<p>OCaml runtime is known to do context switch at the point allocation occurs (confirmed on current version 3.11.1).
This snippet shows that context switch never occurs if the thread does not do any allocation, i/o nor synchronization.
compile with:</p>

<p><code>ocamlopt -thread -I +threads -I unix unix.cmxa threads.cmxa filename.ml</code></p>

<p>the function <code>allocation</code> allocates some memory on OCaml side, while <code>allocation2</code> does on C side. 
In latter case allocation never occur on OCaml side, hence context doesn't switch, and one of the thread runs forever, while the other starves.</p>

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

Initial Title
OCaml memory allocation and context switching

Initial Tags


Initial Language
Other