Introduction
Concurrency is the word used to describe causal independence
between a number of actions, such as the execution of a number of
instructions ``at the same time''. This is also the definition which we give
of the term ``parallel'' in the introduction of this fourth part. The
processes of the Unix library presented in the preceding
chapter could be considered as concurrent to the extent that the
Unix system provides the appearance of their simultaneous execution
on a uniprocessor machine. But the notion of process and concurrency
does not apply only to those obtained by the fork system
call.
The Objective CAML language possesses a library for lightweight processes
(threads.) The principle difference between a thread and a process
is in the sharing or non-sharing of memory between the different
child processes of the same program. Only the context of execution
differs between two threads: the code and memory sections of the data
are shared. Threads do not improve the execution time of an
application. Their principal attraction is to make it possible to
express the programming of concurrent algorithms within a language.
The nature of the chosen language, imperative or functional, affects
the model of concurrency. For an imperative program, as every thread
can modify the communal/shared memory, we are in a shared memory
model. Communication between processes can be achieved by values
written and read in this memory. For a purely functional program,
that is to say, without side effects, even though the memory is
shared, the calculations which each process executes do not act on this
shared memory. In this case, the model used is that of separate
memory and interaction between processes must be achieved by
communication of values though channels.
The Objective CAML language implements both models in its thread library.
The Thread module makes it possible to start new processes
corresponding to a function call with its argument. Modules
Mutex and Condition provide the synchronization
tools for mutual exclusion and waiting on a condition. The
Event model implements a means of communication of language
values by events. These values can themselves be functional, thus
making it possible to exchange calculations to be carried out between
threads. As always in Objective CAML it is possible to mix the two models.
This library is portable to the different systems where OCAML runs.
Unlike the Unix module, the Thread library
facilitates the use of processes on machines that are not running
Unix.