[plt-scheme] Waiting for multiple events
On Jul 29, Gregory Woodhouse wrote:
> have a set of events I want to occur before my main thread proceeds,
> but I don't care about the order. (Actually, it doesn't matter that
> much if I wait for them in a given order, but in this case, I don't
> need that.)
>
> The idea is that I use for/list to create a set of client threads in
> my test suite, like so
>
> (let ([clients
> (for/list ((i (in-range 1 5)))
> (thread
> (lambda ()
> (let-values ([(in out) (tcp-connect "localhost" port-
> no)])
> ...
>
> The list clients contains the client threads I've just spawned. Now,
> what I do to wait for them seems a little crazy, but it seems to work
>
>
> ...
> (map sync clients))
> (printf "Stopping server\n")
> (stop)))
>
> Mapping sync to a list of events seems strange, but it seems to work.
> Intuitively, I suspect that the compiler will carry out (sync
> thd-1 ..., (sync thd-2 ..., etc. in "parallel".
`map' should work fine, but you don't need to collect the results.
You can do some cute things similarly, like a parallel `begin':
(define-syntax-rule (pbegin expr ...)
(for-each thread-wait (list (thread (lambda () expr)) ...)))
BTW, a cute PLT-specific exercise is to implement a `pmap' which is a
parallel version of `map'. (PLT-specific because the real hunt is for
the features that make it easy.)
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!