Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Question about threads::shared and blessed references (http://www.velocityreviews.com/forums/t884699-question-about-threads-shared-and-blessed-references.html)

Nick Marden 01-20-2004 04:35 AM

Question about threads::shared and blessed references
 
(This is a repost of something I posted last week on c.l.p. Since
comp.lang.perl.misc seems more active, perhaps it is a better place
for my question.)

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;

Ben Morrow 01-20-2004 04:59 AM

Re: Question about threads::shared and blessed references
 

nick@vert.net (Nick Marden) wrote:
> (This is a repost of something I posted last week on c.l.p. Since
> comp.lang.perl.misc seems more active, perhaps it is a better place
> for my question.)


c.l.p was rmgrouped quite some time ago. c.l.p.misc is its
replacement.

> 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.

<snip>
> What am I missing?


perldoc threads::shared
| BUGS
| "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.

Ben

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit. | ben@morrow.me.uk
Musica vel ipsas arbores et horridas movet feras. |


All times are GMT. The time now is 11:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.