in reply to how to share array of hashes among several threads?

From the title of this node, a quick look at your code, and my experience with shared vars on linux:

When you share hashes, only the top level keys get shared, unless you declare each hash element as shared. I have doubts whether a hash, in an shared AoH will actually get shared, and will just fail without error.

As an experiment, try making a minimal example with shared AoH, and see if you can access them from all threads. You probably can't. Then try to (laboriously) individually predeclare all hash elements as shared, and it will probably work. For instance:

my %shash; #share(%shash); #will work only for first level keys my %hash; my %workers; my $numworkers = 3; foreach my $dthread(1..$numworkers){ share ($shash{$dthread}{'go'}); share ($shash{$dthread}{'progress'}); share ($shash{$dthread}{'timekey'}); #actual instance of the thread share ($shash{$dthread}{'frame_open'}); #open or close the frame share ($shash{$dthread}{'handle'}); share ($shash{$dthread}{'data'}); share ($shash{$dthread}{'pid'}); share ($shash{$dthread}{'die'}); $shash{$dthread}{'go'} = 0; $shash{$dthread}{'progress'} = 0; $shash{$dthread}{'timekey'} = 0; $shash{$dthread}{'frame_open'} = 0; $shash{$dthread}{'handle'} = 0; $shash{$dthread}{'data'} = $data; $shash{$dthread}{'pid'} = -1; $shash{$dthread}{'die'} = 0; $hash{$dthread}{'thread'} = threads->new(\&work,$dthread); }

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: how to share array of hashes among several threads?
by BrowserUk (Patriarch) on May 25, 2005 at 06:25 UTC

    You don't need to individually shared the hash elements. You only need to ensure that the hashes are shared before you assign anything to them.

    Here's a quick demo:

    #! perl -slw use strict; use threads; use threads::shared; sub test { my $href = shift; for( 1 .. 10000 ) { for my $ref ( values %$href ) { for my $value ( values %{ $ref } ) { $value++; } } } } my %hash : shared = map{ my %hash2 : shared = map{ $_ => 0 } 'a' .. 'd'; $_ => \%hash2; } 'A' .. 'D'; threads->create( \&test, \%hash )->detach;; for( 1 .. 5 ) { lock %hash; print "$_ => [ @{[ %{ $hash{ $_ } } ]} ]" for keys %hash; sleep 1; } __END__ P:\test>thr-hash.pl A => [ c 0 a 0 b 0 d 0 ] B => [ c 0 a 0 b 0 d 0 ] C => [ c 0 a 0 b 0 d 0 ] D => [ c 0 a 0 b 0 d 0 ] A => [ c 3447 a 3447 b 3447 d 3447 ] B => [ c 3447 a 3447 b 3447 d 3447 ] C => [ c 3446 a 3446 b 3446 d 3446 ] D => [ c 3446 a 3446 b 3446 d 3446 ] A => [ c 6903 a 6903 b 6903 d 6903 ] B => [ c 6902 a 6902 b 6902 d 6902 ] C => [ c 6902 a 6902 b 6902 d 6902 ] D => [ c 6902 a 6902 b 6902 d 6902 ] A => [ c 10000 a 10000 b 10000 d 10000 ] B => [ c 10000 a 10000 b 10000 d 10000 ] C => [ c 10000 a 10000 b 10000 d 10000 ] D => [ c 10000 a 10000 b 10000 d 10000 ] A => [ c 10000 a 10000 b 10000 d 10000 ] B => [ c 10000 a 10000 b 10000 d 10000 ] C => [ c 10000 a 10000 b 10000 d 10000 ] D => [ c 10000 a 10000 b 10000 d 10000 ]

    Far from ideal I agree, but there are other parts that are much more broken than this.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.