in reply to threads :: shared hash of hashes (array of arrays)

When I tried some threaded code I found that the &share() function was next to useless for hashes and arrays. The documentation (threads::shared) appears to pretty much agree:

BUGS AND LIMITATIONS

When share is used on arrays, hashes, array refs or hash refs, any data they contain will be lost.

I found that the trick was to declare shared arrays/hashes. Then you can put references to those shared items into other shared data structures.

So, where you have:

sub thread_worker { my $n = shift; # thread N my @args = shift; my %temp = (); .... while(1) { .... $totals{$n}->{STEPS} = $s; # record step $temp{$s}=$random; # add value to per +-thread hash $totals{$n}->{HASH}= &share( \%temp ); # copy results to +shared structure .... } }
I would try:
sub thread_worker { my $n = shift; # thread N my @args = shift; my %temp : shared = (); # Make shared $totals{$n}->{STEPS} = 0 ; # Nothing yet $totals{$n}->{HASH} = \%temp ; # Set ref to shared result +s .... while(1) { .... $totals{$n}->{STEPS} = $s; # record step $temp{$s}=$random; # add value to per-thread +hash # REMOVED: $totals{$n}->{HASH}= &share( \%temp ); .... } }
and see if it works any better. (It did for me.)

Replies are listed 'Best First'.
Re^2: threads :: shared hash of hashes (array of arrays)
by Anonymous Monk on Oct 18, 2008 at 01:45 UTC
    wow, what was pretty clever! thank you very much. It works as expected now. -konstantin