Why do you assign a nicely shared anonymous hash my $href = &share({});, and then immmediately overwrite it with a non-shared one: $href = {};?

What you are doing is assigning a reference to a shared anonymous hash, to a non-shared scalar. You then immediately overwrite it with a reference to a non-shared anonynous hash.

And you do a similar thing again here:

$href->{a} = &share({}); $href->{a} = {};

It's kind of very surprising to me that anything gets shared this way(*), but if simply remove that bewildering practice, then you'll get something approaching the effect you are looking for:

#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Data::Dumper; my $href = &share({}); #$href = {}; sub sub1 { sleep 1; print threads->tid(); print Dumper $href; } $href->{a} = &share({}); #$href->{a} = {}; my $th1 = threads->new('sub1'); $href->{b} = &share({}); my $th2 = threads->new('sub1'); $th1->join(); $th2->join(); __OUTPUTS_ C:\test>junk1 1$VAR1 = { 'a' => {}, 'b' => {} }; 2$VAR1 = { 'a' => {}, 'b' => {} };

That said, I don't understand why you are doing things the way you are doing them. But I realise this is just a snippet, so it might make more sense in your real code. There seems to be a tendancy to use hash refs where a simple hash would do. This is equivalent to the above, far simpler, and more reliable because perl won't let you accidently overwrite the shared hash with a non-shared one as you've been doing with the unshared reference to a shared hash.

#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Data::Dumper; my %hash :shared; sub sub1 { sleep 1; print threads->tid(); print Dumper \%hash; } $hash{a} = &share({}); my $th1 = threads->new('sub1'); $hash{b} = &share({}); my $th2 = threads->new('sub1'); $th1->join(); $th2->join(); __OUTPUTS__ C:\test>junk1 1$VAR1 = { 'a' => {}, 'b' => {} }; 2$VAR1 = { 'a' => {}, 'b' => {} };

(*)It would be interesting to see what the p5p guys make of that, cos it confuses the begebbers outta me!


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^11: does threads (still) memleak? by BrowserUk
in thread does threads (still) memleak? by faxm0dem

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.