[plt-scheme] fluid-let and threads

From: Robby Findler (robby at cs.uchicago.edu)
Date: Thu Apr 17 17:37:24 EDT 2008

This just seems like the behavior one would expect if one thinks of
fluid let as a pair of set!s, one at the beginning and one at the end
of the let.

Specifically, in your last expression, without the channel-get:

(define x 1)
(define ch (make-channel))
(fluid-let ((x 2))
 (thread (lambda()
           (printf "~a~n" x)
           (channel-put ch 'ok))))

you can get either 1 or 2, depending on if the original thread
completes before the new thread runs or not.

If you're actually interested in fluid bindings and their combination
of threads (as opposed to fluid-let specifically), check out
parameters. Also, check out this paper:

Martin Gasbichler, Eric Knauel, Michael Sperber and Richard A. Kelsey.
"How to Add Threads to a Sequential Language Without Getting Tangled
Up". Scheme Workshop 2003. November 2003.

  http://repository.readscheme.org/ftp/papers/sw2003/Threads.pdf

Robby

On Thu, Apr 17, 2008 at 3:57 PM, Eric Tanter <etanter at dcc.uchile.cl> wrote:
> Hi,
>
>  I am trying to understand the semantics of fluid-let in the presence of
> threads.
>
>  First, consider this program:
>
>  (define x 1)
>  (fluid-let ((x 2))
>   (thread (lambda()
>             (printf "~a~n" x)
>             )))
>
>  prints a 1, which suggests that a thread created within a fluid-let does
> not see the fluid binding. Ok, that's a possibility.
>
>  But, it turns to be different if I add a bit of synchronization between the
> two threads. First, a variant that gives the same result (1):
>
>  (define x 1)
>  (define ch (make-channel))
>  (fluid-let ((x 2))
>   (thread (lambda()
>             (printf "~a~n" x)
>             (channel-put ch 'ok))))
>  (channel-get ch)
>
>  Now, consider I move the channel-get within the fluid-let:
>
>  (define x 1)
>  (define ch (make-channel))
>  (fluid-let ((x 2))
>   (thread (lambda()
>             (printf "~a~n" x)
>             (channel-put ch 'ok)))
>   (channel-get ch)
>  )
>
>  I get 2!! meaning the value bound to x in the printf of the created thread
> is now 2. Just commenting out the "(channel-get ch)" expression, and I am
> back to getting a 1.
>
>  Any clarification is more than welcome!
>
>  Thank you,
>
>  -- Éric, completely puzzled
>
>  PS: using DrScheme 372 language
> MzScheme_________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>

Posted on the users mailing list.