develooper Front page | perl.perl6.language.flow | Postings from August 2000

Re: RFC 178 (v1) Lightweight Threads

Thread Previous | Thread Next
From:
Steven W McDougall
Date:
August 31, 2000 12:25
Subject:
Re: RFC 178 (v1) Lightweight Threads
Message ID:
200008311922.PAA29549@world.std.com
> The more interesting case is this:
> 
>     #!/my/path/to/perl
>     sub foo_generator { my $a = shift; sub { print $a++ } }
>     my $foo = foo_generator(1);
>     $foo->();
>     Thread->new($foo);

> Is $a shared between threads or not? 

$a is shared between threads.
The anonymous subroutine is a closure. 
Closures work across threads.


> IMHO the rule is not as simple as this RFC states. (Partly because of
> confusion about "executing" my.)

It is almost as simple. I should add an example (like this one)
showing the behavior of closures. 


> Perhaps Thread->new should deep copy the code ref before executing
> it? Deep copy lexicals but not globals? Deep copy anything that doesn't
> already have a mutex lock?

no no no...perlref.pod, Making References, item 4 says

    A reference to an anonymous subroutine can be created by using sub
    without a subname:

         $coderef = sub { print "Boink!\n" };

     ...no matter how many times you execute that particular line
    (unless you're in an eval("...")), $coderef will still have a
    reference to the same anonymous subroutine.)


We can, and should, retain this behavior.


- SWM

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About