blob: 6445b5579c58fccb505e4c28a287fd075878aa1f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
use Thread;
$level = 0;
sub single_file : locked {
my $arg = shift;
$level++;
print "Level $level for $arg\n";
print "(something is wrong)\n" if $level < 0 || $level > 1;
sleep 1;
$level--;
print "Back to level $level\n";
}
sub start_bar {
my $i;
print "start bar\n";
for $i (1..3) {
print "bar $i\n";
single_file("bar $i");
sleep 1 if rand > 0.5;
}
print "end bar\n";
return 1;
}
sub start_foo {
my $i;
print "start foo\n";
for $i (1..3) {
print "foo $i\n";
single_file("foo $i");
sleep 1 if rand > 0.5;
}
print "end foo\n";
return 1;
}
sub start_baz {
my $i;
print "start baz\n";
for $i (1..3) {
print "baz $i\n";
single_file("baz $i");
sleep 1 if rand > 0.5;
}
print "end baz\n";
return 1;
}
$| = 1;
srand($$^$^T);
$foo = new Thread \&start_foo;
$bar = new Thread \&start_bar;
$baz = new Thread \&start_baz;
$foo->join();
$bar->join();
$baz->join();
print "main: threads finished, exiting\n";
|