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.)


In reply to Re: threads :: shared hash of hashes (array of arrays) by gone2015
in thread threads :: shared hash of hashes (array of arrays) by konstant1n

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.