If you want to push a ref onto a shared array, you have to share that ref first:

#!/usr/bin/perl use strict; use threads; use threads::shared; use tmp; my $rf = tmp->new(); share(my @data); my @threads; for ( my $count = 1; $count <= 10; $count++) { my $t = threads->new(\&sub1, $count); push(@threads,$t); } foreach (@threads) { my $num = $_->join; print "done with $num -- @data\n"; } print "End of main program @data\n"; sub sub1 { my $num = shift; print "started thread $num\n"; print "done with thread $num\n"; push (@data,share( $rf )); #### NOTE CHANGE HERE! return $num; }

With that change, your code runs:

c:\test>807599.pl started thread 1 done with thread 1 started thread 2 done with thread 2 started thread 3 done with thread 3 started thread 4 done with thread 4 started thread 5 done with thread 5 started thread 6 done with thread 6 started thread 7 done with thread 7 started thread 8 done with thread 8 started thread 9 done with thread 9 started thread 10 done with 1 -- tmp=HASH(0xf4a08) tmp=HASH(0xf4a50) t done with thread 10 done with 2 -- tmp=HASH(0xf4a98) tmp=HASH(0xf4978) t done with 3 -- tmp=HASH(0xf4990) tmp=HASH(0xf4b40) t done with 4 -- tmp=HASH(0xf4b70) tmp=HASH(0xf4ba0) t done with 5 -- tmp=HASH(0xf4bb8) tmp=HASH(0xf4a68) t done with 6 -- tmp=HASH(0xf49d8) tmp=HASH(0xf4ae0) t done with 7 -- tmp=HASH(0xf4c18) tmp=HASH(0xf4b58) t done with 8 -- tmp=HASH(0xf49f0) tmp=HASH(0xf4a50) t done with 9 -- tmp=HASH(0xf4c48) tmp=HASH(0xf4978) t done with 10 -- tmp=HASH(0xf4c60) tmp=HASH(0xf4b40) End of main program tmp=HASH(0xf4c78) tmp=HASH(0xf4

Note: The ability to share blessed references is a relatively new feature of threads::shared. I have done very little with and I am dubious of its benefits, but it does appear to work!


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.
RIP PCW It is as I've been saying!(Audio until 20090817)

In reply to Re: [threads] Sharing object through threads by BrowserUk
in thread [threads] Sharing object through threads by baxy77bax

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.