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

From: Robby Findler (robby at cs.uchicago.edu)
Date: Thu Jun 17 02:49:22 EDT 2004

You're missing some syncronization. The lookup of result is happening
before the assignment. Rather than trying to use set! like that, it's
probably better to do this:

(define (thread/result thnk)
  (let ([c (make-channel)])
    (thread (lambda () (channel-put c (thnk))))
    c))

Then, when you want to get the values from the threads, use something
like this:

  (define chans (map get-host hosts))
  (define hosts (map channel-get hosts))

where get-host uses thread/result instead of thread.

hth,
Robby

At Tue, 15 Jun 2004 16:15:16 -0700 (PDT), Kirk Miller wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> 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.
> Then I wait for the thread to join and return a
> list of strings. But in this case "result" is always
> '(). I think its a synchronization thing. In previous
> attempts I have pre-created a list
> of outports to hold displayed data, but I want to know
> how to get a list back instead of having to read
> ports. (the program should be more generally useful)
> see
> http://www.cs.utah.edu/plt/mailarch/plt-scheme-2000/msg00137.html
> 
>   (define result '())
>   
>   (define (get-host host) 
>     ; in= string: host
>     ; out=(cons (tid los))
>   
>       (cons
>        (thread
>         (lambda ()
>           (let (
>                 (server (xml-rpc-server host 8000
> "/servlets/dpmon-server")))
>             (set! result (cons host
>                                ((server 'sm50)))))))
>        result)
>       )
> 
>   (define (sm66 hostlist)
>     (let ((tid-los
>            (map 
>             (lambda (h)
>               (get-host h))
>             hostlist)))
>       (map
>        (lambda (t)
>          (thread-wait (car t))
>          (cdr t))
>        tid-los)))
> 
> 
> 
> 
> 
> 		
> __________________________________
> Do you Yahoo!?
> Yahoo! Mail - You care about security. So do we.
> http://promotions.yahoo.com/new_mail


Posted on the users mailing list.