[gambit-list] dynamic scope

lowly coder lowlycoder at huoyanjinjing.com
Sun Feb 1 00:18:55 EST 2009


Re problems in multithreaded programs:

     The dynamic environment is composed of two parts: the "local dynamic
environment" and the "global dynamic environment".  There is a single global
dynamic environment, and it is used to lookup parameter objects that can't
be found in the local dynamic environment.

is that the main reason? that with the parameter method, i'm changing the
'local env' (which each thread has it's own) whereas with the dynamic-wind
method, I'm setting a var in the global env (which all the threads share)

On Sat, Jan 31, 2009 at 9:05 PM, Marc Feeley <feeley at iro.umontreal.ca>wrote:

>
> On 31-Jan-09, at 11:44 PM, lowly coder wrote:
>
>  Great, the following works:
>>
>> cat test.scm
>> (define (foo x) (* x 2))
>>
>> (define (g x) (foo x))
>>
>>
>> (pp (g 2))
>> (pp
>>  (let ((old-func '()))
>>   (dynamic-wind
>>     (lambda ()
>>       (set! old-func foo)
>>       (set! foo (lambda (x) (* x 3))))
>>     (lambda () (g 2))
>>     (lambda () (set! foo old-func)))))
>>
>> (pp (g 2))
>>
>> 4
>> 6
>> 4
>>
>> two questions:
>> 1) can anything go wrong with variable capture / aliasing? [intuitively, I
>> believe no, so long as I don't use 'old-func' in my thunk
>> 2) is there a more elegant/idiomatic way to do this?
>>
>
> In terms of elegance I prefer this (which eliminates all variable capture
> problems):
>
>  (let ((thunk (lambda () (g 2)))
>       (new-foo (lambda (x) (* x 3)))
>       (old-foo foo))
>   (dynamic-wind
>     (lambda () (set! foo new-foo))
>     thunk
>     (lambda () (set! foo old-foo))))
>
> But once again this will not work right if multiple threads are dynamically
> scoping foo simultaneously, but the parameterize based approach will work
> fine.
>
> Marc
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20090131/98d5c9f5/attachment.htm>


More information about the Gambit-list mailing list