469,890 Members | 1,642 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Question about threads::shared and blessed references

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;
Jul 19 '05 #1
0 2061

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dennis Gavrilov | last post: by
reply views Thread by Al Tobey | last post: by
5 posts views Thread by Richard P | last post: by
4 posts views Thread by Joe Fallon | last post: by
1 post views Thread by Joel Denton | last post: by
3 posts views Thread by Sako | last post: by
8 posts views Thread by zefciu | last post: by
18 posts views Thread by Ulrich Hobelmann | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.