Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,
My problem/confusion can be illustrated with the code below:
#!/usr/bin/perl use strict; use threads; use threads::shared; my %h:shared; for (0..10) { $h{$_}=&share({}); } for (keys(%h)) { print "$h{$_}\n"; }
On my system, I get:
HASH(0x9904d20) HASH(0x9904d38) HASH(0x9904d14) HASH(0x9904d2c) HASH(0x9904d08) HASH(0x9904d20) HASH(0x9904d38) HASH(0x9904d14) HASH(0x9904d2c) HASH(0x9904d08) HASH(0x9904d20)
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:
#!/usr/bin/perl use strict; my %h; for (0..10) { $h{$_}={}; } for (keys(%h)) { print "$h{$_}\n"; }
Output:
HASH(0x9ea67c8) HASH(0x9ea6720) HASH(0x9ea67b0) HASH(0x9ec8578) HASH(0x9ea66e4) HASH(0x9ea672c) HASH(0x9ea5b44) HASH(0x9ea69a8) HASH(0x9ea6600) HASH(0x9ec8590) HASH(0x9ea6990)
What am I missing? Is there a good reason for this behavior? It has rendered XML::Dumper useless for me.
Eric

In reply to mysterious threads::shared behaviour by ericatkin

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found