#! perl -slw use strict; use threads; use threads::shared; package Dummy; sub new { my $sem : shared; my $this : shared = 12345; my $that : shared = 'fred'; my $theOther : shared = 3.141592653; return bless { sem => \$sem, this => \$this, that => \$that, theOther => \$theOther, }, $_[ 0 ]; } sub this : lvalue { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ this } }; } sub that : lvalue { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ that } }; } sub theOther : lvalue { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ theOther } }; } sub combo { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ that } } = "[ ${ $self->{ this } } : ${ $self->{ theOther } } ]"; } package main; sub thread { Win32::Sleep rand( 1000 ); my( $obj ) = @_; bless $obj, 'Dummy'; for ( 1 .. 100 ) { Win32::Sleep rand( 1000 ); print threads->tid, ': ', join ' : ', $obj->this, $obj->that, $obj->theOther; $obj->this = threads->tid; $obj->that = '' . threads->tid; $obj->theOther = rand 1000; $obj->combo; print threads->tid, ': ', join ' : ', $obj->this, $obj->that, $obj->theOther; } } my @objs = map{ new Dummy } 1 .. 2; my @threads = map{ threads->create( \&thread, $objs[ rand 2 ] ) } 1 .. 10; $_->join for @threads; bless $_, 'Dummy' for @objs; print 'Back in main: ', join ' :', $_->this, $_->that, $_->theOther for @objs;