Initial check-in of Thread module.
[p5sagit/p5-mst-13.2.git] / sync2.t
1 use Thread;
2
3 $global = undef;
4
5 sub single_file {
6     my $who = shift;
7     my $i;
8
9     print "Uh oh: $who entered while locked by $global\n" if $global;
10     $global = $who;
11     print "[";
12     for ($i = 0; $i < int(50 * rand); $i++) {
13         print $who;
14     }
15     print "]";
16     $global = undef;
17 }
18
19 sub start_a {
20     my ($i, $j);
21     for ($j = 0; $j < 50; $j++) {
22         single_file("A");
23         for ($i = 0; $i < int(50 * rand); $i++) {
24             print "a";
25         }
26     }
27 }
28
29 sub start_b {
30     my ($i, $j);
31     for ($j = 0; $j < 50; $j++) {
32         single_file("A");
33         for ($i = 0; $i < int(50 * rand); $i++) {
34             print "b";
35         }
36     }
37 }
38
39 sub start_c {
40     my ($i, $j);
41     for ($j = 0; $j < 50; $j++) {
42         single_file("c");
43         for ($i = 0; $i < int(50 * rand); $i++) {
44             print "C";
45         }
46     }
47 }
48
49 $| = 1;
50 srand($$^$^T);
51 Thread::sync(\&single_file);
52
53 $foo = new Thread \&start_a;
54 $bar = new Thread \&start_b;
55 $baz = new Thread \&start_c;
56 print "\nmain: joining...\n";
57 $foo->join;
58 $bar->join;
59 $baz->join;