It is entirely possible to share a multi-level hash between threads. There are a few rules to follow though.

  1. Any value you store in a shared hash must itself be shared (or sharable). In the case of a hashref, this means that the reference must point to a shared hash. But, the share function will not accept an anonymous hash reference.
  2. The hash must be shared before you populate it. If you use the share function on a populated hash, it will be silently emptied.
The combination of these two (strange) design decisions mean that you must declare any subhash as shared (:shared), then populate it. You can then assign a reference to it, as a value in another shared hash.

The following code is not intended as good coding technique, it simply serves to demonstrate that sharing nested hashes is possible.

  • %hash is declared so that thread() closes over it.
  • A thread is spawned and it loops 10 times adding a new HoHs each time around the (sleep delayed for demo purposes) loop.
  • The main thread then goes on to dump the contents of the shared hash in a loop until the demo thread stop. You will see the hash growing in steps.
    #! perl -slw use strict; use threads; use threads::shared; use Data::Dumper; $Data::Dumper::Indent = 0; my %hash : shared; my $running : shared = 0; sub thread { $running++; for my $mainKey ( 1 .. 10 ) { my %subhash : shared = map { my %subsubhash : shared = map{; ( "foo$_" , "bar$_" ) } 1 .. 4; \%subsubhash; } 1 .. 4; { lock %hash; $hash{ $mainKey } = \%subhash; } sleep 1; } $running--; } my $thread = threads->new( \&thread ); sleep 1 until $running; while( $running ) { print Dumper \%hash; sleep 1; } $thread->join;

    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.

    In reply to Re: sharing a hash of hashes between threads by BrowserUk
    in thread sharing a hash of hashes between threads by coontie

    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.