Previous Up Next

Chapter 27  The threads library

The threads 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.

The threads 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.

Two implementations of the threads library are available, depending on the capabilities of the operating system:

Programs that use system threads must be linked as follows:

        ocamlc -thread other options unix.cma threads.cma other files
        ocamlopt -thread other options unix.cmxa threads.cmxa other files

Compilation units that use the threads library must also be compiled with the -thread option (see chapter 8).

Programs that use VM-level threads must be compiled with the -vmthread option to ocamlc (see chapter 8), and be linked as follows:

        ocamlc -vmthread other options threads.cma other files

Compilation units that use threads library must also be compiled with the -vmthread option (see chapter 8).


Previous Up Next