3 The MLWorks Pervasive Library

3.5 Threads: the Threads structure

The MLWorks threads interface is provided by the structure MLWorks.Threads. Conceptually, the MLWorks thread mechanism consists of a set of concurrently evaluating expression closures, or "threads", that share memory.

All threads are assigned a unique identifier, of type thread_id, and a thread number. All threads have a status, represented with the 'a result datatype.

Warning: You should rely only on thread identifiers for uniquely identifying threads; thread numbers are not important. Thread numbers are handed out sequentially as threads are created, and are provided as a simple reference for, say, application-debugging purposes. Thread numbers are not necessarily unique: after a sufficiently large number, they may wrap around and start again.

At startup, MLWorks consists of two threads: a master thread, and a single evaluation thread. When running in TTY or GUI mode, this latter thread is linked to the shell interface; otherwise, it executes a prespecified function (for example, a function specified by MLWorks.Deliver.deliver above).

The threads are run in a scheduler loop. By default, they behave like coroutines. That is, only one thread executes at a time until it either finishes or yields control to another, at which point it sleeps waiting to be woken by a "yield" command. Threads can also be run interleaved, using the MLWorks.Threads.Internal.Preemption mechanism, which interrupts processes after a user-specified number of milliseconds.

The 'a result datatype is used to record the status of a thread. A thread can be polled for its status with the result function.

Thread pre-emption mode implements the interleaved execution of threads. You specify a pre-emption interval in milliseconds using the set_interval function. Each thread is allowed to proceed evaluation for this time before being "pre-empted" by the next thread on the scheduling loop.

Warning: If you are interleaving a thread with the interactive GUI shell, any printed output from your thread is liable to be interpreted by the GUI as input to the shell.

Threads
'a thread
thread_id
'a result
fork
result
sleep
wake
yield
all
children
get_id
get_num
id
kill
parent
raise_in
state
yield_to
set_handler
reset_fatal_status
get_interval
set_interval
on
start
stop

MLWorks Reference Manual (version 1.0) - 3 DEC 1996

Generated with Harlequin WebMaker