Chapter 7


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.


(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 7.4), such as the current directory or current exception handler. A newly-created thread inherits the parameter settings of the creating thread, except

When a thread is created, it is placed into the management of the current custodian (See section 9.2). A thread that has not terminated can be ``garbage collected'' only if it is unreachable and blocked on an unreachable semaphore.7

7.1  Thread Utilities

(current-thread) returns the thread descriptor for the currently executing thread.

(thread? v) returns #t if v is a thread descriptor, #f otherwise.

(sleep [x]) causes the current thread to sleep for at least x seconds, where x is a non-negative real number. The x argument defaults to 0 (allowing other threads to execute when operating system threads are not used). The value of x can be non-integral to request a sleep duration to any precision, but the precision of the actual sleep time is unspecified.

(thread-running? thread) returns #t if thread has not terminated, #f otherwise.

(thread-wait thread) blocks execution of the current thread until thread has terminated. Note that (thread-wait (current-thread)) deadlocks the current thread, but a break can end the deadlock (if breaking is enabled; see section 6.6).

(kill-thread thread) terminates the specified thread immediately. Terminating the main thread exits the application. If thread is already not running, kill-thread does nothing. Otherwise, if the current custodian (see section 9.2) does not manage thread (and none of its subordinates manages thread), the exn:misc exception is raised.

All of the MzScheme (and MrEd) primitives are kill-safe; that is, killing a thread never interferes with the application of primitives in other threads. For example, if a thread is killed while extracting a character from an input port, the character is either completely consumed or not consumed, and other threads can safely use the port.

(break-thread thread) registers a break with the specified thread. If breaking is disabled in thread, the break will be ignored until breaks are re-enabled (see section 6.6).

(call-in-nested-thread thunk [custodian]) creates a nested thread managed by custodian to execute thunk.8 The current thread blocks until thunk returns, and the result of the call-in-nested-thread call is the result returned by thunk. The default value of custodian is the current custodian.

The nested thread's exception handler is initialized to a procedure that jumps to the beginning of the thread and transfers the exception to the original thread. The handler thus terminates the nested thread and re-raises the exception in the original thread.

If the thread created by call-in-nested-thread dies before thunk returns, the exn:thread exception is raised in the original thread. If the original thread is killed before thunk returns, a break is queued for the nested thread.

If a break is queued for the original thread (with break-thread) while the nested thread is running, the break is redirected to the nested thread. If a break is already queued on the original thread when the nested thread is created, the break is moved to the nested thread. If a break remains queued on the nested thread when it completes, the break is moved to the original thread.

7.2  Semaphores

A semaphore is a value that is used to synchronize MzScheme threads. Each semaphore has an internal counter; when this counter is zero, the semaphore can block a thread's execution (through semaphore-wait) until another thread increments the counter (using semaphore-post). The maximum value for a semaphore's internal counter is platform-specific, but always at least 10000. A semaphores's counter is updated in a single-threaded manner, of course, so tat semaphore can be used for reliable synchronization.

See also object-wait-multiple in section 7.3.

7.3  Synchronizing Multiple Objects with Timeout

(object-wait-multiple timeout v ···1) blocks as long as all of the vs are in a blocking state, as defined below, or until timeout seconds have passed. The timeout argument can be a real number or #f; if timeout is #f, then object-wait-multiple does not return until some v is unblocked.

When at least one v is unblocked, it is returned as the result. If multiple vs are unblocked before the call to object-wait-multple, the returned v is the earliest unblocked v according to argument order. If multiple vs become unblocked later, any one of the unblocked vs can be returned (but only one).

If the returned v is a semaphore, then the semaphore's internal count is decremented, just as with semaphore-wait. Otherwise, the returned v is unmodified. Any v that is not returned by object-wait-multiple is unmodified.

If object-wait-multiple returns because timeout seconds have passed, the return value is #f. Each v is checked at least once, so a timeout value of 0 can be used for polling.

Only certain kinds of values, lised below, are waitable. If any other kind of value is provided to object-wait-multiple, the exn:application:mismatch exception is raised.9

(object-wait-multiple/enable-break v ···1) is like object-wait-multiple, but breaking is enabled (see section 6.6) while waiting on the vs. If breaking is disabled when object-wait-multiple/enable-break is called, then either all vs remain unmodified or the exn:break exception is raised, but not both.

(object-waitable? v) returns #t if v is a waitable object, #f otherwise. See object-wait-multiple, above, for the list of waitable object types.

7.4  Parameters

A parameter is a thread-specific setting, such a the current output port or the current directory for resolving relative pathnames. A parameter procedure sets and retrieves the value of a specific parameter. For example, the current-output-port parameter procedure sets and retrieves a port value that is used by display when a specific output port is not provided. Applying a parameter procedure without an argument obtains the current value of a parameter in the current thread, and applying a parameter procedure to a single argument sets the parameter's value in the current thread (and returns void). For example, (current-output-port) returns the current default output port, while (current-output-port p) sets the default output port to p.

7.4.1  Built-in Parameters

MzScheme's built-in parameter procedures are listed in the following sections. The make-parameter procedure, described in section 7.4.2, creates a new parameter and returns a corresponding parameter procedure.  Current Directory  Ports  Parsing  Printing  Read-Eval-Print  Loading  Exceptions  Security  Exiting  Random Numbers  Locale  Modules

7.4.2  Parameter Utilities

(make-parameter v [guard-proc]) returns a new parameter procedure. The value of the parameter is initialized to v in all threads. If guard-proc is supplied, it is used as the parameter's guard procedure. A guard procedure takes one argument. Whenever the parameter procedure is applied to an argument, the argument is passed on to the guard procedure. The result returned by the guard procedure is used as the new parameter value. A guard procedure can raise an exception to reject a change to the parameter's value.

(parameter? v) returns #t if v is a parameter procedure, #f otherwise.

(parameter-procedure=? a b) returns #t if the parameter procedures a and b always modify the same parameter, #f otherwise.

The parameterize form evaluates an expression with temporarily values installed for a group of parameters. The syntax of parameterize is:

(parameterize ((parameter-expr value-expr) ···) body-expr ···1)

The result of a parameterize expression is the result of the last body-expr. The parameter-exprs determine the parameters to set, and the value-exprs determine the corresponding values to install before evaluating the body-exprs. All of the parameter-exprs are evaluated first (checked with check-parameter-procedure), then all value-exprs are evaluated, and then the parameters are set.

After the body-exprs are evaluated, each parameter's setting is restored to its original value in the dynamic context of the parameterize expression. More generally, the values specified by the value-exprs determine initial ``remembered'' values, and whenever control jumps into or out of the body-exprs, the value of each parameter is swapped with the corresponding ``remembered'' value.


(parameterize ([exit-handler (lambda (x) 'no-exit)]) 
  (exit)) ; => 'no-exit

(define p1 (make-parameter 1))
(define p2 (make-parameter 2))
(parameterize ([p1 3]
               [p2 (p1)]) 
  (cons (p1) (p2))) ; => '(3 . 1)

(let ([k (let/cc out 
           (parameterize ([p1 2]) 
             (p1 3) 
             (cons (let/cc k 
                     (out k)) 
  (if (procedure? k) 
      (k (p1))
      k)) ; =>  '(1 . 3)

(check-parameter-procedure v) returns v if it is a procedure that can take both 0 arguments and 1 argument, and raises exn:application:type otherwise. The check-parameter-procedure procedure is used in the expansion of parameterize.

7 In MrEd, a handler thread for an eventspace is blocked on an internal semaphore when its event queue is empty. Thus, the handler thread is collectable when the eventspace is unreachable and contains no visible windows or running timers.

8 The nested thread's current custodian is inherited from the creating thread, independent of the custodian argument.

9 An extension or embedding application can extend the set of waitable values.

10 Using the current global port print handler; see section

11 The "C" locale is also always available; setting the locale to "C" is the same as disabling locale sensitivity with #f only when string and character operations are restricted to the first 128 characters.