This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Initial check-in of Thread module.
[perl5.git] / sync.t
CommitLineData
d9bb3666
MB
1use Thread;
2
3$level = 0;
4
5sub single_file {
6 my $arg = shift;
7 $level++;
8 print "Level $level for $arg\n";
9 print "(something is wrong)\n" if $level < 0 || $level > 1;
10 sleep 1;
11 $level--;
12 print "Back to level $level\n";
13}
14
15sub start_bar {
16 my $i;
17 print "start bar\n";
18 for $i (1..3) {
19 print "bar $i\n";
20 single_file("bar $i");
21 sleep 1 if rand > 0.5;
22 }
23 print "end bar\n";
24 return 1;
25}
26
27sub start_foo {
28 my $i;
29 print "start foo\n";
30 for $i (1..3) {
31 print "foo $i\n";
32 single_file("foo $i");
33 sleep 1 if rand > 0.5;
34 }
35 print "end foo\n";
36 return 1;
37}
38
39sub start_baz {
40 my $i;
41 print "start baz\n";
42 for $i (1..3) {
43 print "baz $i\n";
44 single_file("baz $i");
45 sleep 1 if rand > 0.5;
46 }
47 print "end baz\n";
48 return 1;
49}
50
51$| = 1;
52srand($$^$^T);
53Thread::sync(\&single_file);
54
55$foo = new Thread \&start_foo;
56$bar = new Thread \&start_bar;
57$baz = new Thread \&start_baz;
58$foo->join();
59$bar->join();
60$baz->join();
61print "main: threads finished, exiting\n";