kbrint has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: threads::shared resets %hash iterators
by BrowserUk (Patriarch) on Nov 01, 2008 at 07:11 UTC

    I can confirm I see the same behaviour with the latest CPAN versions:

    c:\test>junk6 threads version: 1.71 threads::shared version: 1.26 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

    And I can't see any obvious cause either. If you haven't already done so, you should raise a bug report.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.