Plasma GitLab Archive
Projects Blog Knowledge

Multi-threaded applications

If an already existing package is planned to be used in a multi-threaded environment, some language constructs must be replaced by different ones. For example, global variables must be protected by locks (mutexes). Because of this it is common practice to provide two versions of such packages, one for single-, one for multi-threaded applications (or, another technique, there is a special add-on module that initializes a library for multi-threaded usage).

The predicate "mt" should be used to recognize multi-threaded applications. For example, a package could consist of two archives, p.cma, and p_mt.cma, where the latter archive is designed for multi-threaded execution. The META file would contain the following lines causing that always the appropriate archive is linked:

archive(byte) = "p.cma"
archive(byte,mt) = "p_mt.cma"

When querying the package database the option "-predicates mt" must be included in the commands in order to select the appropriate entries.

When compiling or linking it is necessary to specify either the "-thread" or the "-vmthread" option anyway. The compiler frontends detects these options, and automatically set the "mt" predicate. The following command is an example for this; note that the "threads" package means the standard library of Ocaml providing thread support:

ocamlfind ocamlc -thread -package threads,p,q -c

For some operating systems, Ocaml supports the native multithreading libraries conforming to the POSIX interface. As the linker options are generally different, another predicate must be set to get these libraries linked in. The predicate "mt_posix" indicates POSIX threads while "mt_vm" indicates bytecode (virtual machine) threads.

This web site is published by Informatikb├╝ro Gerd Stolpmann
Powered by Caml