This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add socklen_t probe; Configure maintenance.
[perl5.git] / ext / Thread / sync2.t
CommitLineData
d9bb3666
MB
1use Thread;
2
3$global = undef;
4
0655b981 5sub single_file : locked {
d9bb3666
MB
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 "[";
609f3ea9 12 for ($i = 0; $i < int(10 * rand); $i++) {
d9bb3666 13 print $who;
609f3ea9 14 select(undef, undef, undef, 0.1);
d9bb3666
MB
15 }
16 print "]";
17 $global = undef;
18}
19
20sub start_a {
21 my ($i, $j);
609f3ea9 22 for ($j = 0; $j < 10; $j++) {
d9bb3666 23 single_file("A");
609f3ea9 24 for ($i = 0; $i < int(10 * rand); $i++) {
d9bb3666 25 print "a";
609f3ea9 26 select(undef, undef, undef, 0.1);
d9bb3666
MB
27 }
28 }
29}
30
31sub start_b {
32 my ($i, $j);
609f3ea9
MB
33 for ($j = 0; $j < 10; $j++) {
34 single_file("B");
35 for ($i = 0; $i < int(10 * rand); $i++) {
d9bb3666 36 print "b";
609f3ea9 37 select(undef, undef, undef, 0.1);
d9bb3666
MB
38 }
39 }
40}
41
42sub start_c {
43 my ($i, $j);
609f3ea9
MB
44 for ($j = 0; $j < 10; $j++) {
45 single_file("C");
46 for ($i = 0; $i < int(10 * rand); $i++) {
47 print "c";
48 select(undef, undef, undef, 0.1);
d9bb3666
MB
49 }
50 }
51}
52
53$| = 1;
54srand($$^$^T);
d9bb3666 55
609f3ea9
MB
56print <<'EOT';
57Each pair of square brackets [...] should contain a repeated sequence of
58a unique upper case letter. Lower case letters may appear randomly both
59in and out of the brackets.
60EOT
d9bb3666
MB
61$foo = new Thread \&start_a;
62$bar = new Thread \&start_b;
63$baz = new Thread \&start_c;
64print "\nmain: joining...\n";
609f3ea9
MB
65#$foo->join;
66#$bar->join;
67#$baz->join;
68print "\ndone\n";