I have a big code that somewhere in the middle of it, there's a foreach loop filling up some dictionaries. Each iteration is independant and fills those dictionaries with disjoint keys.
I'm trying to turn the "foreach" loop in the middle to multithreaded in order to decrease time.
In the following example, $a1, $b1 are the pointers to dictionaries.
I tried "thread::shared" this way:
my $a1 = {};
my $b1 = {};
my $c1 = {};
my $d1 = {};
# a lot of code using $a1 and $b1
share($a1);
share($b1);
share($c1);
share($d1);
my @threads;
foreach my $Inst ( sort keys %{ $a1->{ports} }) {
push( @threads, threads->create('some_func', $Inst, $a1, $b1, $c1, $d1, $e ...));
}
for my $thr (@threads) {
thr->join();
}
# all the other code
But I get an error of:
Invalid value for shared scalar at ...
Any ideas how to get the data-structures filled, but not that it would interfere with the code before and after the for-each loop?
a1,b1,c1,d1as shared and then you say that they filled on each iteration without specifying any code how it happening. If they filled on each iteration then it sounds like they should not be shared. And your sentence -- but not that it would interfere with the "#some code" section and not with the "#some other code" section" -- creates even more confusion.$a1references a dictionary%h1, then you need to share%h1too..shared_clonefromthreads::shared. I suggested this in comments to your two previous questions, one now deleted (exact same as this one, I believe). There was a link to a post with an example as well. Did you not look at any of that at all?shared %$a1and$a1references a hash%h1, the data in%h1is lost. As zdim mentioned, I think you need to useshared_clone