I'm having a hard time storing objects in a Thread::Queue. According to the Thread::Queue documentation (BUGS section), '"bless" is not supported on shared references', so let's not go there for now and just talk about sharing ordinary references in a Thread::Queue.

As I understand it, reference sharing is _supposed_ to work fine as long as all the referants are shared in a recursive manner. (Also, threads::shared says references must be shared using &share({}) syntax. Fine..whatever.) When my references are dequeued in another thread, however, the referants appear to be empty. It appears empty regardless of how I go about sharing the entire structure. The attached code illustrates this behavior.

Also , to work around the no-shared-blessed-references problem, I can just re-bless dequeued references as long as I can assume they were all of a certain type whey they were enqueued, right?

Perl 5.8.0 (ithreads) on Red Hat 9. Help!!!! Many thanks in advance,

Preston

#!/usr/bin/perl -wall use strict; #=-=-=-=- require Exporter; package Foo; our @ISA = qw(Exporter); use threads; use threads::shared; use Thread::Queue; sub new { my $class = shift; my $self = {key1 => 'value1', key2 => 'value2'}; share($self); bless($self, $class); return $self; } #=-=-=-=- package Bar; use threads; use threads::shared; use Thread::Queue; my $q = new Thread::Queue; my $thr = threads->new(\&monitorQueue); # Add a reference to an empty hash. my $hash_ref_0 = {}; print "\$hash_ref_0 is a " . ref($hash_ref_0); &share($hash_ref_0); $q->enqueue($hash_ref_0); # Add a reference to a hash with something in it. my $hash_ref_1 = {keyM => "valueM", keyN => "valueN"}; print "\$hash_ref_1 is a " . ref($hash_ref_1); &share($hash_ref_1); # Try sharing the reference. share(%$hash_ref_1); # Try sharing the referant. share($hash_ref_1->{$_}) foreach(keys %$hash_ref_1); # Try sharing all + elements of the referant. $q->enqueue($hash_ref_1); # Add a blessed reference to a hash with something in it. my $hash_ref_2 = new Foo(); print "\$hash_ref_2 is a " . ref($hash_ref_2); &share($hash_ref_2); $q->enqueue($hash_ref_2); $thr->join; sub monitorQueue { while (my $elem = $q->dequeue) { print "Removed $elem from queue"; printHashRef($elem); } } sub printHashRef { my $rhash = shift; print "Key: '$_'\t\tValue: '$rhash->{$_}'" foreach(sort keys %$rhash +); }

Edited by BazB: added readmore tag.


In reply to Sharing (blessed)? references with Thread::Queue. by preston

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.