<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <meta name="generator" content="hevea 2.00"> <link rel="stylesheet" type="text/css" href="manual.css"> <title>The threads library</title> </head> <body> <a href="manual038.html"><img src="previous_motif.gif" alt="Previous"></a> <a href="index.html"><img src="contents_motif.gif" alt="Up"></a> <a href="manual040.html"><img src="next_motif.gif" alt="Next"></a> <hr> <h1 class="chapter" id="sec467">Chapter 25  The threads library</h1> <p> <a id="c:threads"></a></p><p>The <span class="c007">threads</span> library allows concurrent programming in OCaml. It provides multiple threads of control (also called lightweight processes) that execute concurrently in the same memory space. Threads communicate by in-place modification of shared data structures, or by sending and receiving data on communication channels.</p><p>The <span class="c007">threads</span> library is implemented by time-sharing on a single processor. It will not take advantage of multi-processor machines. Using this library will therefore never make programs run faster. However, many programs are easier to write when structured as several communicating processes.</p><p>Two implementations of the <span class="c007">threads</span> library are available, depending on the capabilities of the operating system: </p><ul class="itemize"><li class="li-itemize"> System threads. This implementation builds on the OS-provided threads facilities: POSIX 1003.1c threads for Unix, and Win32 threads for Windows. When available, system threads support both bytecode and native-code programs. </li><li class="li-itemize">VM-level threads. This implementation performs time-sharing and context switching at the level of the OCaml virtual machine (bytecode interpreter). It is available on Unix systems, and supports only bytecode programs. It cannot be used with native-code programs. </li></ul><p> Programs that use system threads must be linked as follows: </p><pre> ocamlc -thread <span class="c013">other options</span> unix.cma threads.cma <span class="c013">other files</span> ocamlopt -thread <span class="c013">other options</span> unix.cmxa threads.cmxa <span class="c013">other files</span> </pre><p> Compilation units that use the <span class="c007">threads</span> library must also be compiled with the <span class="c007">-thread</span> option (see chapter <a href="manual022.html#c%3Acamlc">8</a>).</p><p>Programs that use VM-level threads must be compiled with the <span class="c007">-vmthread</span> option to <span class="c007">ocamlc</span> (see chapter <a href="manual022.html#c%3Acamlc">8</a>), and be linked as follows: </p><pre> ocamlc -vmthread <span class="c013">other options</span> threads.cma <span class="c013">other files</span> </pre><p> Compilation units that use <span class="c007">threads</span> library must also be compiled with the <span class="c007">-vmthread</span> option (see chapter <a href="manual022.html#c%3Acamlc">8</a>).</p><ul class="ftoc2"><li class="li-links"> <a href="libref/Thread.html">Module <span class="c007">Thread</span>: lightweight threads</a> </li><li class="li-links"><a href="libref/Mutex.html">Module <span class="c007">Mutex</span>: locks for mutual exclusion</a> </li><li class="li-links"><a href="libref/Condition.html">Module <span class="c007">Condition</span>: condition variables to synchronize between threads</a> </li><li class="li-links"><a href="libref/Event.html">Module <span class="c007">Event</span>: first-class synchronous communication</a> </li><li class="li-links"><a href="libref/ThreadUnix.html">Module <span class="c007">ThreadUnix</span>: thread-compatible system calls</a> </li></ul> <hr> <a href="manual038.html"><img src="previous_motif.gif" alt="Previous"></a> <a href="index.html"><img src="contents_motif.gif" alt="Up"></a> <a href="manual040.html"><img src="next_motif.gif" alt="Next"></a> </body> </html>