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