Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Question about threads::shared and blessed references

Reply
Thread Tools

Question about threads::shared and blessed references

 
 
Nick Marden
Guest
Posts: n/a
 
      01-17-2004
I am trying to use threads::shared to make a thread-safe object.

It appears to be the case that copying a blessed-then-shared reference
to another shared variable keeps the underlying structure intact
(e.g., hashref & its contents) but forgets the blessedness. This makes
my program pretty useless because I need to be able to copy or pass my
lockable object(s) at various times in my program, without losing
their identity as blessed objects.

The attached program demonstrates what I am trying to say. It's output
is:

ref $f = LockableObject
ref $copy = HASH

Note that there are no threads involved here; this is all executing in
a single thread.

Also note that if you remove the share() call from
LockableObject::new(), make $copy a non-shared variable, and comment
out the *LOCK_BLOCK blocks - in other words, if you remove just the
threadedness of the program - then the program works "right". In this
case the output is what I had originally expected, to wit:

ref $f = LockableObject
ref $copy = LockableObject

What am I missing?

(P.S. perl -V says "Summary of my perl5 (revision 5.0 version 8
subversion 0)...usethreads=define use5005threads=undef
useithreads=define usemultiplicity=define" and a bunch of other things
that are available upon request if they are relevant.)
---------------------------------------
#!/usr/bin/perl -w

use strict;

package LockableObject;

use threads;
use threads::shared;

sub new {
my $obj = bless { }, shift;
# Imagine that $obj has some internals that I want to protect,
# so I need to be able to lock the object to serialize access
share($obj);
}

package main;

use threads;
use threads::shared;

my $f = new LockableObject;

print "ref \$f = ", ref $f, "\n";

LOCK_BLOCK:
{
# The object is lockable. Look, no errors!
lock ($f);
}

my $copy : shared = $f;

ANOTHER_LOCK_BLOCK:
{
# The copy of the object is still lockable!
lock ($copy);
}

# But the copy is no longer a LockableObject reference
print "ref \$copy = ", ref $copy, "\n";

1;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally I feel blessed M_Marked Wireless Networking 0 12-28-2007 04:31 PM
re-blessing a blessed reference Jon Perl Misc 2 07-25-2005 05:59 AM
pulling apart a blessed hash sbk Perl Misc 3 01-26-2005 09:28 PM
Threads and shared blessed references Vetle Roeim Perl Misc 0 06-09-2004 01:55 PM
Question about threads::shared and blessed references Nick Marden Perl Misc 1 01-20-2004 04:59 AM



Advertisments