[plt-scheme] Problem updating variables from threads

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Mon Oct 22 00:27:37 EDT 2007

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.


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

Posted on the users mailing list.