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] / sync2.t
CommitLineData
d9bb3666
MB
1use Thread;
2
3$global = undef;
4
5sub 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
19sub 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
29sub 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
39sub 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;
50srand($$^$^T);
51Thread::sync(\&single_file);
52
53$foo = new Thread \&start_a;
54$bar = new Thread \&start_b;
55$baz = new Thread \&start_c;
56print "\nmain: joining...\n";
57$foo->join;
58$bar->join;
59$baz->join;