[plt-scheme] Problem updating variables from threads

From: Robby Findler (robby at cs.uchicago.edu)
Date: Mon Oct 22 09:00:39 EDT 2007

This does that:

(define (parallel-execute . thunks)
  (for-each sync (map thread thunks)))

Robby

On 10/21/07, Carl Eastlund <cce at ccs.neu.edu> wrote:
> The thread function spawns a thread.  It does not guarantee when the
> thread will take a step, however.  Your "display" is taking effect
> before either of your new threads have actually performed any
> computation.  You probably want to add a line between the call to
> parallel-execute and the call to display that waits for both threads
> to complete.  You'll have to change parallel-execute to keep track of
> its threads to do that, of course.
>
> --Carl
>
> On 10/22/07, eliben <eliben at gmail.com> wrote:
> > Hello,
> >
> > Consider this code:
> >
> > (define (parallel-execute . thunks)
> >   (for-each thread thunks))
> >
> > (define x 10)
> >
> > (parallel-execute (lambda () (set! x (* x x)))
> >                   (lambda () (set! x (+ x 1))))
> >
> > (display x)
> >
> > ---
> >
> > Always prints "10", which is surprising, since I would expect either
> > 101 or 121 (given that set! is indeed atomic. otherwise it could be
> > any of more values, depending on thread switch granularity).
> >
> > Eli
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.