[previous] [up] [next]     [contents] [index]
Next: Thread Utilities Up: Threads and Namespaces Previous: Threads and Namespaces

Threads

MzScheme supports multiple threads of control within a program. Threads are implemented for all operating systems, even when the operating system does not provide primitive thread support.

(thread f) invokes the procedure f with no arguments in a new thread of control. The thread procedure returns immediately with a thread descriptor value. When the invocation of f returns, the thread created to invoke f terminates.

For example:

  (thread (lambda () (sleep 2) (display 7) (newline))) ; => a thread descriptor 
  ; displays 7 after two seconds pass 

Each thread has its own error escape handler (see section 8.6) and parameterization (see section 9.4). When a thread is created, the current parameterization branch handler (see section 9.4.1.11) is called to get the initial parameterization for the thread; if the current parameterization branch handler does not return a parameterization, the exn:misc:parameterization exception is raised.

When a thread is created, it is placed into the management of the current custodian (See section 9.5). A thread that has not terminated can never be ``garbage collected,'' even, for example, if the thread is blocked on a semaphore that is not reachable from any other thread.





PLT