This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Argument sanity checking.
[perl5.git] / ext / Thread / lock.tx
1 BEGIN {
2     eval { require Config; import Config };
3     if ($@) {
4         print "1..0 # Skip: no Config\n";
5         exit(0);
6     }
7 }
8
9 use Thread;
10
11 $level = 0;
12
13 sub worker
14 {
15     my $num = shift;
16     my $i;
17     print "thread $num starting\n";
18     for ($i = 1; $i <= 20; $i++) {
19         print "thread $num iteration $i\n";
20         select(undef, undef, undef, rand(10)/100);
21         {
22             lock($lock);
23             warn "thread $num saw non-zero level = $level\n" if $level;
24             $level++;
25             print "thread $num has lock\n";
26             select(undef, undef, undef, rand(10)/100);
27             $level--;
28         }
29         print "thread $num released lock\n";
30     }
31 }
32
33 for ($t = 1; $t <= 5; $t++) {
34     new Thread \&worker, $t;
35 }