#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; { package Foo; use threads::shared; use constant INDENT => "\t\t\t\t\t\t\t\t"; sub new { my $class = shift; my %this; $this{_socket} = shift; $this{_sendBuffer} = []; # TBD: Why does this not work? # my %this = { # _socket => shift, # _sendBuffer => [], # }; share ($this{_sendBuffer}); # make this a shared variable to allow main thread to lock & manipulate this return bless \%this, $class; } sub serve { my $this = shift; { # scope for lock on $Foo->{_sendBuffer} print INDENT . "object thread: 1st lock attempt\n"; lock ($this->{_sendBuffer}); print INDENT . "object thread: 1st lock acquired..."; push $this->{_sendBuffer}, "datagram " . scalar (@{$this->{_sendBuffer}}); sleep (1); print " and released!\n"; # pretend to release lock before going out of scope } # release lock sleep (1); { # scope for lock on $Foo->{_sendBuffer} print INDENT . "object thread: 2nd lock attempt\n"; lock ($this->{_sendBuffer}); print INDENT . "object thread: 2nd lock acquired..."; push $this->{_sendBuffer}, "datagram " . scalar (@{$this->{_sendBuffer}}); sleep (1); print " and released!\n"; # pretend to release lock before going out of scope } # release lock } } my $foo = new Foo(); my $thr1 = threads->create(sub { $foo->serve() }); { # scope for lock on $foo print "main thread: 1st lock attempt\n"; lock ($foo->{_sendBuffer}); print "main thread: 1st lock acquired...\n"; print " foo send buffer size: " . scalar (@{$foo->{_sendBuffer}}) . "\n"; sleep (1); print "main thread: 1st lock released!\n"; } # release lock sleep (1); { # scope for lock on $foo print "main thread: 2nd lock attempt\n"; lock ($foo->{_sendBuffer}); print "main thread: 2nd lock acquired...\n"; print " foo send buffer size: " . scalar (@{$foo->{_sendBuffer}}) . "\n"; sleep (1); print "main thread: 2nd lock released!\n"; } # release lock $thr1->join(); # for some reason, this calls the destructor of foo (if present): so the thread obtained a copy of foo to work with? print "main thread: object thread joined\n\n"; my $index = 0; foreach my $item (@{$foo->{_sendBuffer}}) { # loop through contents of sendBuffer print "foo->{_sendBuffer[$index]}: $item\n"; # output sendBuffer contents $index++; } undef ($foo); # destroy object