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 thunk)` invokes the procedure thunk with no arguments in a new thread of control. The `thread` procedure returns immediately with a thread descriptor value. When the invocation of thunk returns, the thread created to invoke thunk terminates.

Example: ``

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

Each thread has its own parameter settings (see section 9.4), such as the current directory or current exception handler. A newly-created thread inherits the parameter settings of the creating thread, except

- the `error-escape-handler` parameter, which is initialized to the default error escape handler; and
- the `current-exception-handler` parameter, which is initialized to the value of `initial-exception-handler`.

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 be ``garbage collected'' only if it is unreachable and blocked on an unreachable semaphore.

- Thread Utilities