Pots::SharedObject - Perl ObjectThreads base class for thread shared objects


Pots documentation Contained in the Pots distribution.

Index


Code Index:

NAME

Top

Pots::SharedObject - Perl ObjectThreads base class for thread shared objects

SYNOPSIS

Top

package My::Shared::Class;

use base qw(Pots::SharedObject);

sub new { my $class = shift;

    my $self = $class->SUPER::new();

    return $self;
}

DESCRIPTION

Top

Pots::SharedObject is a base class for all Pots objects that need to be shared between threads.

METHODS

Top

new ()

Standard method for creating a hash-based shared object. If you define your own "new()" method, don't forget to call "$class->SUPER::new()" before.

destroy ()

This method is called when the shared object is being destroyed, that is no other objects references it. It uses the standard Perl "DESTROY", so you should not use it in your derived classes. Redefine "destroy()" and put your cleanup code in it.

ACKNOWLEDGMENTS

Top

The DESTROY "trick" for shared objects was stolen from Mike Pomraning. I found it in the "perl.ithreads" list: http://www.nntp.perl.org/group/perl.ithreads/766

AUTHOR and COPYRIGHT

Top


Pots documentation Contained in the Pots distribution.

##########################################################################
#
# Module template
#
##########################################################################
package Pots::SharedObject;

##########################################################################
#
# Modules
#
##########################################################################
use threads;
use threads::shared;

use strict;

##########################################################################
#
# Global variables
#
##########################################################################
our $VERSION = "0.01";

##########################################################################
#
# Private methods
#
##########################################################################
sub DESTROY {
    my $self = shift;
    my $class = ref($self) || $self;

    return unless threads::shared::_refcnt($self) == 1;

    $self->destroy() if $self->can('destroy');
}

##########################################################################
#
# Public methods
#
##########################################################################

sub new {
    my $class = shift;
    $class = ref($class) || $class;

    my %hself : shared = ();
    my $self = bless (\%hself, $class);

    return $self;
}

sub destroy {
}

1; #this line is important and will help the module return a true value
__END__