following this question, I face the following problem:
When I want to loop over some hashes in a couple of threads and update them, I get the following error:
Thread .. terminated abnormally: Invalid value for shared scalar at ...
This is the code:
use feature qw(say);
use strict;
use warnings;
use threads ;
use threads::shared ;
use Data::Dumper qw(Dumper);
my %h1 = (a => 1, b => 2);
my %h2 = (c => 3, d => 4);
my $a1 = \%h1;
my $b1 = \%h2;
my $a1c = shared_clone($a1);
my $b1c = shared_clone($b1);
my $lockvar:shared;
my $nthreads = 3;
for ( 1..$nthreads ) {
threads->create('job_to_parallelize', $a1c, $b1c, \$lockvar ) ;
}
$_->join() for threads->list();
sub job_to_parallelize {
my ($a1, $b1, $lockvar) = @_;
{
lock $lockvar;
$a1->{a}++;
$b1->{d}++;
$a1->{scalar}{10} = 1;
}
}
print Dumper($a1c);
print Dumper($b1c);
From what I understand, it's because "scalar" didn't exist before I cloned. Any idea how to solve this? (i.e. Allow me to declare inside threads on cloned structures).