Chapter 4. Multithreading

4.1. Tasks, threads or processes
4.2. Multithreading Reference
mp:all-processes — Return the list of active processes.
mp:exit-process — Exit the task from which it is invoked.
mp:holding-lock-p — Determine whether current process holds the lock.
mp:interrupt-process — Interrupt a task.
mp:get-lock — Try to obtain a lock.
mp:giveup-lock — Release a lock we have obtained before.
mp:make-lock — Create a new lock.
mp:make-process — Create a new thread.
mp:process-active-p — Determine whether a task is running.
mp:process-enable — Start up a task which was not running.
mp:process-kill — Try to stop a running task.
mp:process-name — Return the name of a task.
mp:process-preset — Associate a function to a process.
mp:process-run-function — Create a task and execute a function in it.
mp:recursive-lock-p — Determine whether lock is recursive or not.
mp:with-lock — Synchronize a piece of code between different tasks.

4.1. Tasks, threads or processes

On most platforms, ECL supports native multithreading. That means there can be several tasks executing lisp code on parallel and sharing memory, variables and files. The interface for multitasking in ECL, like those of most other implementations, is based on a set of functions and types that resemble the multiprocessing capabilities of old Lisp Machines.

This backward compatibility is why tasks or threads are called "processes". However, they should not be confused with operating system processes, which are made of programs running in separate contexts and without access to each other's memory.

The implementation of threads in ECL is purely native and based on Posix Threads wherever avaiable. The use of native threads has advantanges. For instance, they allow for non-blocking file operations, so that while one task is reading a file, a different one is performing a computation.

As mentioned above, tasks share the same memory, as well as the set of open files and sockets. This manifests on two features. First of all, different tasks can operate on the same lisp objects, reading and writing their slots, or manipulating the same arrays. Second, while threads share global variables, constants and function definitions they can also have thread-local bindings to special variables that are not seen by other tasks.

The fact that different tasks have access to the same set of data allows both for flexibility and a greater risk. In order to control access to different resources, ECL provides the user with locks, as explained in the next section.