#! /usr/bin/perl use threads; use threads::shared; $|=1; { my $l : shared; # private lexical sub f() { lock($l); # any f() in any thread is "locked" my $tid = threads->tid(); print "A}$tid}$_[0]\n"; select undef, undef, undef, rand(2); print "B}$tid}$_[0]\n"; } } # -- end scope for $l threads->create(\&f, $_) for (qw|foo bar baz|); $_->join() for threads->list(); __END__