Chapter 23 | The threads library

The `threads` library allows concurrent programming in Objective Caml. 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.
Unix:

> Programs that use the `threads` library must be linked as follows:

>             ocamlc -thread other options threads.cma other files

> The `-thread` option selects a special, thread-safe version of the standard library (see chapter 8). The `-thread` option must also be given when compiling any source file that references modules from the thread library (`Thread`, `Mutex`, ...).
> The default thread implementation cannot be used in native-code programs compiled with `ocamlopt`. If your operating system provides POSIX 1003.1c compliant threads, you can select an alternate implementation when configuring Objective Caml (use the `-with-pthread` option to `configure`) which also supports native-code programs. Programs that use this alternate implementation of the `threads` library must be linked as follows:

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

> Depending on the operating system, extra system libraries can be necessary. For instance, under Solaris 2.5, add `-cclib -lposix4` at the end of the command line.

Windows:

> Programs that use the `threads` library must be linked as follows:

>             ocamlc -thread other options threads.cma other files

> All object files on the command line must also have been compiled with the `-thread` option, which selects a special, thread-safe version of the standard library (see chapter 8).

- Module `Thread`: lightweight threads
- Module `Mutex`: locks for mutual exclusion
- Module `Condition`: condition variables to synchronize between threads
- Module `Event`: first-class synchronous communication
- Module `ThreadUnix`: thread-compatible system calls