Next | 3.2 Deadlocks | Prev |
The following program will be deadlock-ed unless one of the threads acquires both locks first.
use threads; use threads::shared; my @a : shared = (1, 2); my %b : shared = ("foo" => "bar"); my $thr1 = threads->new(sub { lock(@a); print "Thread 1: got lock on 'a'\n"; threads->yield; sleep 5; lock(%b); print "Thread 1: got lock on 'b'\n"; }); my $thr2 = threads->new(sub { lock(%b); print "Thread 2: got lock on 'b'\n"; threads->yield; sleep 5; lock(@a); print "Thread 2: got lock on 'a'\n"; }); sleep 8;
Next | Perl Threads | Prev |