alice
library
manual.

Alice Project

The Thread structure


________ Synopsis ____________________________________________________

    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


________ Import ______________________________________________________

Imported implicitly.


________ Interface ___________________________________________________

    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
  

________ Description _________________________________________________

type thread
type t = thread

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.

datatype state = RUNNABLE | BLOCKED | TERMINATED

The type of thread states.

exception Terminate

raised by terminate to terminate a thread. Should never be raised explicitly.

exception Terminated

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.

thread f

spawns a new thread thr which computes f (). Returns thr.

spawnThread f

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 ().

current ()

returns the calling thread, that is, the thread executing current ().

state thr

returns the current state of thr.

yield 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.

sleep t

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.

raiseIn (thr, ex)

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.

terminate thr

attempts to terminate thr by raising exception Terminate in it. Equivalent to

raiseIn (thr, Terminated)
suspend thr

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.

resume thr

resumes thr. If thr is runnable, makes thr available for scheduling again. Does not change the status of thr.

isSuspended thr

returns true if thr is suspended, false otherwise.



last modified 2007/Mar/30 17:10