package Counter; use strict; use threads; use threads::shared; my $counter : shared = 0; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; lock $counter; $counter = shift; bless($self,$class); return $self; } sub incCounter { lock $counter; $counter++; } sub getCounter { return $counter; } 1; #### #!/usr/bin/perl -w use Counter; my $x = new Counter( 1 ); my $y = new Counter( 10 ); print $x->getCounter()."\n"; print $y->getCounter()."\n"; print "-------------\n"; $x->incCounter(); print $x->getCounter()."\n"; print $y->getCounter()."\n";