OCaml memory allocation and context switching


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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.