Hi peoples,

I thought my script which uses either ithreads, 5005threads or none, depending on environment, was working fine and dandy.. Until I noticed that I should have been sharing some objects (plugins) between the threads, and I wasnt, and thus the fun started.

According to the threads::shared docs:

"bless" is not supported on shared references. In the current version, "bless" will only bless the thread local reference and the blessing will not propagate to the other threads. This is expected to be implemented in a future version of Perl.
However, my tests show that this is not quite true. There are some strange things you can do, and more importantly, not do, with shared variables. It seems you can bless shared variables, for example, and the blessed object works fine in a thread.. You cant share a blessed object though, since whenever you share something, its contents get clobbered. These and other things might have been useful to put the threads::shared docs.. See test code below:
#!/usr/bin/perl -w use Data::Dumper; use Storable 'freeze', 'thaw'; use threads; use threads::shared; use Net::Ping; # Create hashref, set a value my $hashref = {}; $hashref->{'mine'} = 12; #share hashref - original value gone! share($hashref); print "Shared hashref: ", Dumper($hashref), "\n"; # Create object my $ping = Net::Ping->new(); print "Object: ", Dumper($ping), "\n"; # Share object - values gone share($ping); print "Shared object (empty): ", Dumper($ping), "\n"; # Recreate (bless shared thing, works fine..) $ping = Net::Ping->new(); print "Shared object (renewed): ", Dumper($ping), "\n"; # Create, share, then bless.. works.. my $newping = {}; share($newping); $newping = Net::Ping->new(); print "Shared then bless: ", Dumper($newping), "\n"; # Use in another thread, just to check my $thr = threads->create(\&ping_it); # Wait for ping to complete $thr->join(); sub ping_it { print "ping_it ", Dumper($ping); print "Ping localhost: ", ($ping->ping('localhost') ? "yes" : "no" +), "\n"; # Works! } # Put shared thingy in other shared thingy.. (I want(ed) a hash of obj +ects..) my $hashy = {}; share($hashy); # $hashy->{'ping'} = $ping; # print "Hash of obj: ", Dumper($hashy); # Damn, doesnt work - Invalid value for shared scalar at ../threadtest +.pl line 53. my %hashy = (); share(%hashy); # $hashy{'ping'} = $ping; # print "Hash of obj: ", Dumper(%hashy); # Damn, doesnt work - Invalid value for shared scalar at ../threadtest +.pl line 53. # my $testping = Net::Ping->new(); # $hashy->{'ping'} = $testping; # Can't do that either.. # $hashy->{'ping'} = freeze($ping); # print "Hash of obj: ", Dumper(%hashy); # Nope.. $hashy->{'ping'} = freeze(pingy->new()); print "Hash of obj: ", Dumper($hashy); # hmm, works.. (but only if no globs or code-refs) $thr = threads->create(\&test_me); $thr->join(); sub test_me { print Dumper(thaw($hashy->{'ping'})); } package pingy; use Data::Dumper; sub new { # Create new pingy # Parameter: Class-Name/Reference, Properties my $class = shift; $class = ref($class) || $class; my $self = {'just' => 'testing'}; bless($self, $class); print "Created: ", Dumper($self), "\n"; return $self; }
.. So, if I keep my modules free of code refs and globs, it seems I can cheat a little, use freeze/thaw, and actually share objects. Of course, no guarantee that this behaviour will work in the next version..

(All tests here done with 5.8.2, on linux)

Anyone know how, what, why and WTF?

C.


In reply to ithreads, shared variables.. by castaway

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.