[plt-scheme] Threading vs. System Threads

From: Gregory Cooper (greg at cs.brown.edu)
Date: Wed Feb 12 12:07:52 EST 2003

Actually, what I've been working on is a thread communication library for
PLT Scheme.  My goal is to provide the essence of Erlang-style processes
and message-passing, which involve the following three constructs:

(spawn expr ...)

creates a new thread to evaluate the given expressions, returning the
thread's id, which can be used as a destination for messages;

(! tid val)

sends val to tid; this call returns (void) immediately, and the message is
queued in tid's mailbox until tid is ready to process it;

(receive [after timeout timeout-expr ...] ; (optional)
	 [pat expr ...]

blocks the current thread until either the timeout (if given) expires or
the thread receives a message matching one of the given patterns; the
patterns can bind variables for use in the corresponding expressions,
which are evaluated upon receipt of the message.

I have an implementation of this interface that uses MzScheme's
(preemptive) threads and performs quite well in general.  On my machine,
for example, I can create several thousand threads in DrScheme without
straining the system much.  However, in a restricted setting, I've found I
can use a custom, cooperative threading model to improve performance
significantly.  In particular, I can cut the spawning time by a factor of
25, reduce the resource requirements by a factor of 50, and also modestly
reduce communication time (by up to a factor of 2).


Posted on the users mailing list.