Question about threads::shared and blessed references

Discussion in 'Perl' started by Nick Marden, Jan 17, 2004.

  1. Nick Marden

    Nick Marden Guest

    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;
     
    Nick Marden, Jan 17, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Marden
    Replies:
    1
    Views:
    141
    Ben Morrow
    Jan 20, 2004
  2. Vetle Roeim

    Threads and shared blessed references

    Vetle Roeim, Jun 9, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    168
    Vetle Roeim
    Jun 9, 2004
  3. sbk

    pulling apart a blessed hash

    sbk, Jan 26, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    108
    Gary E. Ansok
    Jan 26, 2005
  4. Jon

    re-blessing a blessed reference

    Jon, Jul 25, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    208
  5. A. Farber
    Replies:
    9
    Views:
    130
Loading...

Share This Page