[plt-scheme] newbie question: returning values from threads

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Jun 16 07:02:50 EDT 2004

Kirk Miller wrote:

> I am trying to access multiple xml-rpc servers in
> parallel since each call takes a long time. I don't 
> know how to propagate the answer back out of a thread.
> I make a call starting a thread for each host.

Use a variable shared between the producer and the consumer.

     (let ((result1 'unfinished)
           (result2 'unfinished))
             ; start two workers
       (let ((worker1 (thread (lambda () (sleep (random 5)) (set! result1 42))))
             (worker2 (thread (lambda () (sleep (random 5)) (set! result2 3.14)))))
             ; wait until one of them is finished, then return its result
         (if (eq? worker1 (object-wait-multiple/enable-break #f worker1 worker2))
             result1
             result2)))


-- 
Jens Axel Søgaard




Posted on the users mailing list.