Subject: | Lazy attributes don't work with ithreads |
The following sample code is to demonstrate the problem. It consists of
a pipeline where the first thread creates some Moose-based objects and
puts them in the queue, which are then picked up in the second thread.
The problem is when it tries to get an attribute which is lazy built.
package Foo;
use Moose;
has 'bar' => (
is => 'ro',
isa => 'ArrayRef', # the error doesn't happen with simple
scalar datatypes
lazy => 1, # this line causes the error
default => sub { [1 .. 10] }, # the error also occurs if this
was a builder
);
package main;
use threads;
use Thread::Queue;
my $threadq = Thread::Queue->new;
sub create {
$threadq->enqueue( Foo->new ) foreach 1 .. 5;
$threadq->enqueue( undef );
return;
}
sub process {
while (my $f = $threadq->dequeue) {
print @{$f->bar}, "\n";
}
return;
}
threads->create( \&create )->join;
threads->create( \&process )->join;
This code results in the following error message:
Show quoted text
> Thread 2 terminated abnormally: Invalid value for shared scalar
> at reader Foo::bar (defined at ... line 9) line 10.
The error disappears if I remove the lazy property for that attribute.
I ran this with Strawberry Perl 5.12.3.