Chapter 29 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 on top of the threading facilities provided by the operating system: POSIX 1003.1c threads for Linux, MacOS, and other Unix-like systems; Win32 threads for Windows. Only one thread at a time is allowed to run OCaml code, hence opportunities for parallelism are limited to the parts of the program that run system or C library code. However, threads provide concurrency and can be used to structure programs as several communicating processes. Threads also efficiently support concurrent, overlapping I/O operations.
Programs that use threads must be linked as follows:
ocamlc -I +threads other options unix.cma threads.cma other files ocamlopt -I +threads other options unix.cmxa threads.cmxa other files
Compilation units that use the threads library must also be compiled with the -I +threads option (see chapter 11).
- Module Thread: lightweight threads
- Module Mutex: locks for mutual exclusion
- Module Condition: condition variables to synchronize between threads
- Module Semaphore: semaphores, another thread synchronization mechanism
- Module Event: first-class synchronous communication
© 1995-2021 INRIA.
https://www.ocaml.org/releases/4.13/htmlman/libthreads.html