package Counter; use strict; use threads; use threads::shared; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; my $counter :shared = shift; ## Local shared variable $self->{ counter } = \$counter; ## Store a reference to it in the object bless($self,$class); return $self; } sub incCounter { my $self = shift; lock ${ $self->{ counter } }; ++${ $self->{ counter } }; } sub getCounter { my $self = shift; lock ${ $self->{ counter } }; ${ $self->{ counter } }; } 1