This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
Re: static modules and local vars
- From: "62945" <62945 at bol dot com dot br>
- To: "kawa" <kawa at sources dot redhat dot com>
- Date: Sat, 7 Aug 2004 21:23:27 -0300
- Subject: Re: static modules and local vars
Per wrote:
> 62945 wrote:
>
> > I imagine this implies that closures coming from a
> > static module are safe as well
>
> Yes and no. Closures and objects are conceptually
> equivalent. (They're implemented similarly, and you can
> use one to simulate the other.) If you create a closure
> in one thread, and pass that closure to another thread,
> you might get thread safety problems - though only
> if either thread modifies the captured variable (field)
> while sharing it.
If I understand the jvm model, only heap objects need to
be synchronized - method (stack) variables are private
to each thread. I think I get what you mean when you say
there might be problems in sharing a closure, but this
would apply to any heap object. If there is only one
(object) reference in _use_ at a given time no problem.
If you copy (pass) the reference be cautious.
So the scenario:
static module f:
(define (f x)
(let ((c (abs x)))
(lambda ()
(if (positive? c) (set! c (- c 1)))
(zero? c))))
non-static module a:
(define a (f 5))
(if (a) ... ) ; many times
non-static module b:
(define b (f 10))
(if (b) ... ) ; many times
should work properly since the counter "c" is local to
each thread (module "a" and "b" run concurrently) and I
see no sharing of values except if "(f ...)" returns the
same value/closure/object. I would like to think that
the returned lambda behaved as a value, different for
each "f" call.
__________________________________________________________________________
Acabe com aquelas janelinhas que pulam na sua tela.
AntiPop-up UOL - É grátis!
http://antipopup.uol.com.br/