signature THREAD structure Thread : THREAD
The Thread structure provides access to first-class threads. A thread encapsulates a concurrent computation. The spawn keyword starts a new thread, as does triggering a by-need future.
A thread is initially created in state RUNNABLE. The runnable threads are scheduled in a round-robin fashion. When a computation requests the value of a future, the thread becomes BLOCKED until the future is bound, whereafter it becomes RUNNABLE again. When the computation has been fully performed (or an exception is raised for which there is no handler), the thread becomes TERMINATED.
The functions in this structure allow to observe a thread's status, raise an exception in a thread, and to explicitly suspend and resume threads.
See also: Future, Lock
signature THREAD = sig type thread type t = thread datatype state = RUNNABLE | BLOCKED | TERMINATED exception Terminate exception Terminated val thread : (unit -> unit) -> thread val spawnThread : (unit -> 'a) -> thread * 'a val current : unit -> thread val state : thread -> state val yield : thread -> unit val sleep : Time.time -> unit val raiseIn : thread * exn -> unit val terminate : thread -> unit val suspend : thread -> unit val resume : thread -> unit val isSuspended : thread -> bool end
The type of first-class threads. A reference to a first-class thread can be used to observe and control execution of a concurrent computation.
The type of thread states.
raised by terminate to terminate a thread. Should never be raised explicitly.
indicates that a thread control operation was applied to a terminated thread. Should only be caught to perform cleanup actions, and should always be re-raised.
spawns a new thread thr which computes f (). Returns thr.
spawns a new thread thr which computes f (). Returns a pair of thr and the result of applying f, which is a future which will be bound to the result of f ().
returns the calling thread, that is, the thread executing current ().
returns the current state of thr.
causes the scheduler to stop executing thread thr, if it is currently being executed, and select another thread for execution. Has no effect if thr is not currently being executed.
causes the calling thread to stop executing and not be rescheduled for the time specified by t. If t is zero or negative, immediately returns. Raises Overflow if t is larger than the longest possible time period that can be handled by the system.
raises the exception ex in thread thr. If thr is terminated, instead raises Terminated in the calling thread. If thr is blocked, makes thr runnable again. Requests exn.
attempts to terminate thr by raising exception Terminate in it. Equivalent to
raiseIn (thr, Terminated)
suspends thr. If thr is being executed, then it yields. thr is not rescheduled until resumed again. Note that this does not change the status of thr, that is, thr can be suspended and at the same time be runnable, blocked, or terminated.
resumes thr. If thr is runnable, makes thr available for scheduling again. Does not change the status of thr.
returns true if thr is suspended, false otherwise.