Perl6 RFC Librarian wrote:
> =head2 Each thread gets its own copy of block-scoped lexicals upon
> execution of C<my>
>
> Example 8
>
> #!/my/path/to/perl
> foo();
> Thread->new(\&foo);
>
> sub foo
> {
> my $a = 1;
> print $a++;
> }
[prints "11"]
This must be true otherwise $a is not a lexical variable. Threading
must not change the definition of a lexical.
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? If $a is not shared, we've broken
lexicals.
IMHO the rule is not as simple as this RFC states. (Partly because of
confusion about "executing" my.)
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?
I like the idea of having an RFC nail down the behavior of threads
though. It might be getting a bit into internals, but if the language
group doesn't decide the behavior, they'll have to live with whatever
internals cooks up.
- Ken