I am passing a hash-of-hashes from one thread to another.

I found that if I iterate over the inner hashes with each %{$outer{inner}}, it loops forever on the first key. It looks like every use of $outer{inner} is resetting the hash iterator for the inner hash.

This program illustrates the problem:

#!/usr/bin/perl use strict; use threads; use threads::shared; my %outer :shared; $outer{inner} = &share({}); %{$outer{inner}} = map { $_ => $_ } 'a'..'z'; ################################################################# print "By taking a reference first...\n"; my $iter = 0; { my $ref = $outer{inner}; while ($iter++ < 10) { last unless (my ($k, $v) = each %$ref); print "$k\n"; } } print "...and so on\n\n"; ################################################################# print "By de-ref the shared hash-of-hashes each time...\n"; $iter = 0; while ($iter++ < 10) { last unless (my ($k, $v) = each %{$outer{inner}}); print "$k\n"; } print "...and so on\n";

... which produces the following output:

By taking a reference first... w r a x d j y u k h ...and so on By de-ref the shared hash-of-hashes each time... w w w w w w w w w w ...and so on

When I comment out use threads, everything works normally. I tried reading through shared.xs, etc but I can't find an obvious bug.

Any ideas?

-- kevin


In reply to threads::shared resets %hash iterators by kbrint

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.