ericatkin has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
My problem/confusion can be illustrated with the code below:
Eric
My problem/confusion can be illustrated with the code below:
On my system, I get:#!/usr/bin/perl use strict; use threads; use threads::shared; my %h:shared; for (0..10) { $h{$_}=&share({}); } for (keys(%h)) { print "$h{$_}\n"; }
Now, obviously, the addresses change with each invocation, but it is always 5 unique addresses that repeat. It seems to be creating 11 distinct hashes where I can file away my scalars, but it doesn't make sense to me that there would only be 5 unique memory addresses. Now, mysteries like this always bother me, but beyond the mystery, this behavior breaks XML::Dumper as it uses the memory address to ensure that a data structure that is referenced multiple times is only dumped once. Without the threads::shared module, I get 11 distinct memory locations as I would expect as illustrated below:HASH(0x9904d20) HASH(0x9904d38) HASH(0x9904d14) HASH(0x9904d2c) HASH(0x9904d08) HASH(0x9904d20) HASH(0x9904d38) HASH(0x9904d14) HASH(0x9904d2c) HASH(0x9904d08) HASH(0x9904d20)
Output:#!/usr/bin/perl use strict; my %h; for (0..10) { $h{$_}={}; } for (keys(%h)) { print "$h{$_}\n"; }
What am I missing? Is there a good reason for this behavior? It has rendered XML::Dumper useless for me.HASH(0x9ea67c8) HASH(0x9ea6720) HASH(0x9ea67b0) HASH(0x9ec8578) HASH(0x9ea66e4) HASH(0x9ea672c) HASH(0x9ea5b44) HASH(0x9ea69a8) HASH(0x9ea6600) HASH(0x9ec8590) HASH(0x9ea6990)
Eric
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: mysterious threads::shared behaviour
by dave_the_m (Monsignor) on Jan 18, 2009 at 13:09 UTC | |
by ericatkin (Novice) on Jan 21, 2009 at 20:22 UTC | |
by BrowserUk (Patriarch) on Jan 21, 2009 at 20:54 UTC | |
Re: mysterious threads::shared behaviour
by zentara (Archbishop) on Jan 18, 2009 at 11:18 UTC | |
Re: mysterious threads::shared behaviour
by gone2015 (Deacon) on Jan 18, 2009 at 11:45 UTC |
Back to
Seekers of Perl Wisdom