1<!DOCTYPE html>
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
5<meta name="generator" content="hevea 2.18">
6<link rel="stylesheet" type="text/css" href="manual.css">
7<title>Chapter&#XA0;29&#XA0;&#XA0;The threads library</title>
8</head>
9<body>
10<a href="libstr.html"><img src="previous_motif.gif" alt="Previous"></a>
11<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
12<a href="libgraph.html"><img src="next_motif.gif" alt="Next"></a>
13<hr>
14<h1 class="chapter" id="sec535">Chapter&#XA0;29&#XA0;&#XA0;The threads library</h1>
15<p>
16<a id="c:threads"></a></p><p>The <span class="c003">threads</span> library allows concurrent programming in OCaml.
17It provides multiple threads of control (also called lightweight
18processes) that execute concurrently in the same memory space. Threads
19communicate by in-place modification of shared data structures, or by
20sending and receiving data on communication channels.</p><p>The <span class="c003">threads</span> library is implemented by time-sharing on a single
21processor. It will not take advantage of multi-processor machines.
22Using this library will therefore never make programs run
23faster. However, many programs are easier to write when structured as
24several communicating processes.</p><p>Two implementations of the <span class="c003">threads</span> library are available, depending
25on the capabilities of the operating system:
26</p><ul class="itemize"><li class="li-itemize">
27System threads. This implementation builds on the OS-provided threads
28facilities: POSIX 1003.1c threads for Unix, and Win32 threads for
29Windows. When available, system threads support both bytecode and
30native-code programs.
31</li><li class="li-itemize">VM-level threads. This implementation performs time-sharing and
32context switching at the level of the OCaml virtual machine (bytecode
33interpreter). It is available on Unix systems, and supports only
34bytecode programs. It cannot be used with native-code programs.
35</li></ul><p>
36Programs that use system threads must be linked as follows:
37</p><pre>
38        ocamlc -thread <span class="c009">other options</span> unix.cma threads.cma <span class="c009">other files</span>
39        ocamlopt -thread <span class="c009">other options</span> unix.cmxa threads.cmxa <span class="c009">other files</span>
40</pre><p>
41Compilation units that use the <span class="c003">threads</span> library must also be compiled with
42the <span class="c003">-thread</span> option (see chapter&#XA0;<a href="comp.html#c%3Acamlc">8</a>).</p><p>Programs that use VM-level threads must be compiled with the <span class="c003">-vmthread</span>
43option to <span class="c003">ocamlc</span> (see chapter&#XA0;<a href="comp.html#c%3Acamlc">8</a>), and be linked as follows:
44</p><pre>
45        ocamlc -vmthread <span class="c009">other options</span> threads.cma <span class="c009">other files</span>
46</pre><p>
47Compilation units that use <span class="c003">threads</span> library must also be compiled with
48the <span class="c003">-vmthread</span> option (see chapter&#XA0;<a href="comp.html#c%3Acamlc">8</a>).</p><ul class="ftoc2"><li class="li-links">
49<a href="libref/Thread.html">Module <span class="c003">Thread</span>: lightweight threads</a>
50</li><li class="li-links"><a href="libref/Mutex.html">Module <span class="c003">Mutex</span>: locks for mutual exclusion</a>
51</li><li class="li-links"><a href="libref/Condition.html">Module <span class="c003">Condition</span>: condition variables to synchronize between threads</a>
52</li><li class="li-links"><a href="libref/Event.html">Module <span class="c003">Event</span>: first-class synchronous communication</a>
53</li><li class="li-links"><a href="libref/ThreadUnix.html">Module <span class="c003">ThreadUnix</span>: thread-compatible system calls</a>
54</li></ul>
55<hr>
56<a href="libstr.html"><img src="previous_motif.gif" alt="Previous"></a>
57<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
58<a href="libgraph.html"><img src="next_motif.gif" alt="Next"></a>
59</body>
60</html>
61