[plt-scheme] Creating a suspended thread
Marc,
local comment: I don't understand why you lock the done-mutex twice.
On Apr 13, 2009, at 4:39 PM, Marc Feeley wrote:
> (define (amb thunk1 thunk2)
> (let ((result #f)
> (result-mutex (make-mutex))
> (done-mutex (make-mutex)))
> (letrec ((child1
> (make-thread
> (lambda ()
> (let ((x (thunk1)))
> (mutex-lock! result-mutex #f #f)
> (set! result x)
> (thread-terminate! child2)
> (mutex-unlock! done-mutex)))))
> (child2
> (make-thread
> (lambda ()
> (let ((x (thunk2)))
> (mutex-lock! result-mutex #f #f)
> (set! result x)
> (thread-terminate! child1)
> (mutex-unlock! done-mutex))))))
> (mutex-lock! done-mutex #f #f)
> (thread-start! child1)
> (thread-start! child2)
> (mutex-lock! done-mutex #f #f)
> result)))
>
> Delayed activation is essential for creating mutually recursive
> threads.
global comment: I'd program this kind of situation with cml-style
sync primitives (available in PLT Scheme) not with mutexes and an
imperative style.
-- Matthias