This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Rewrite thread destruction system using linked list of threads.
[perl5.git] / sync2.t
CommitLineData
d9bb3666
MB
1use Thread;
2
3$global = undef;
4
5sub single_file {
8f4f90ac 6 use attrs 'locked';
d9bb3666
MB
7 my $who = shift;
8 my $i;
9
10 print "Uh oh: $who entered while locked by $global\n" if $global;
11 $global = $who;
12 print "[";
13 for ($i = 0; $i < int(50 * rand); $i++) {
14 print $who;
15 }
16 print "]";
17 $global = undef;
18}
19
20sub start_a {
21 my ($i, $j);
22 for ($j = 0; $j < 50; $j++) {
23 single_file("A");
24 for ($i = 0; $i < int(50 * rand); $i++) {
25 print "a";
26 }
27 }
28}
29
30sub start_b {
31 my ($i, $j);
32 for ($j = 0; $j < 50; $j++) {
33 single_file("A");
34 for ($i = 0; $i < int(50 * rand); $i++) {
35 print "b";
36 }
37 }
38}
39
40sub start_c {
41 my ($i, $j);
42 for ($j = 0; $j < 50; $j++) {
43 single_file("c");
44 for ($i = 0; $i < int(50 * rand); $i++) {
45 print "C";
46 }
47 }
48}
49
50$| = 1;
51srand($$^$^T);
d9bb3666
MB
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;