in reply to Sharing multitiered hash amongst multiple threads?
You can only assign references to shared arrays and hashes into shared hashes. You can either declare them shared and assign a reference:
my %hash : shared; my @array : shared; my %hash2 : shared; $hash{ array } = \@array; $hash{ hash } = \%hash;
Which is simple but often inconvenient. Or you can use threads::shared::share to them before assignment.
There are two caveats with the second approach.
You can make life a little easier by bypassing the prototypes using $sharedHash{ key } = &share( {} ) and $sharedHash{ key } = &share( [] ), but don't be tempted to do $sharedHash{ key } = &share( [ 'some', 'stuff', 'here' ] );because the contents will be discarded.
An example
#! perl -slw use strict; use threads; use threads::shared; use Data::Rmap; sub thread { my( $hashref ) = @_; rmap{ print "$_"; } $hashref; } my %hash : shared; $hash{ leaf } = 'fred'; $hash{ L1 } = &share( {} ); $hash{ L1 }{ leaf } = 'wilma'; $hash{ L1 }{ L2 } = &share( {} ); $hash{ L1 }{ L2 }{ leaf } = 'bam bam'; $hash{ L1 }{ L2 }{ L3 } = &share( {} ); $hash{ L1 }{ L2 }{ L3 }{ leaf } = 'flintstone'; threads->create( \&thread, \%hash )->join; __END__ P:\test>junk2 fred wilma bam bam flintstone
|
|---|