[plt-scheme] The concurrency package in SICP
On Sat, 30 Dec 2006, Joshua Ewulo wrote:
> To who ever can help: I am currently in section 3.4 (Concurrency: Time
> is of the Essence) section of Structure and Interpretation of Computer
> Programs and I would like to know what package to download in order to
> work on the exercises in this section.
Hi Joshua,
Since PLT Scheme has a thread system, we can take advantage of its
primitives. You might find:
http://list.cs.brown.edu/pipermail/plt-scheme/2006-January/011098.html
helpful. To implement MAKE-SERIALIZER, we can use the semaphore
functions.
Here's one possible implementation of those functions:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module concurrency mzscheme
(provide parallel-execute
make-serializer)
(define (parallel-execute . thunks)
(for-each thread thunks))
(define (make-serializer)
(let ([mutex (make-semaphore 1)])
(lambda (thunk)
(call-with-semaphore mutex thunk)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Unfortunately, the granularity for a thread's context switch is a bit
large, so you might not see the interplay that you'd like using native
threads. According to the message thread above, you might be able to use
engines to get finer control over context switching, and Jens Axel
Soegaard has a nice implementation here:
http://list.cs.brown.edu/pipermail/plt-scheme/2002-September/000620.html
It might not be too bad to take his work and adapt it to provide the
PARALLEL-EXECUTE and MAKE-SERIALIZER primitives you'll want.