This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlio:
[perl5.git] / ext / Thread / unsync.t
1 BEGIN {
2     eval { require Config; import Config };
3     if ($@) {
4         print "1..0 # Skip: no Config\n";
5         exit(0);
6     }
7     if ($Config{extensions} !~ /\bThread\b/) {
8         print "1..0 # Skip: no use5005threads\n";
9         exit(0);
10     }
11 }
12
13 use Thread;
14
15 $| = 1;
16
17 if (@ARGV) {
18     srand($ARGV[0]);
19 } else {
20     my $seed = $$ ^ $^T;
21     print "Randomising to $seed\n";
22     srand($seed);
23 }
24
25 sub whoami {
26     my ($depth, $a, $b, $c) = @_;
27     my $i;
28     print "whoami ($depth): $a $b $c\n";
29     sleep 1;
30     whoami($depth - 1, $a, $b, $c) if $depth > 0;
31 }
32
33 sub start_foo {
34     my $r = 3 + int(10 * rand);
35     print "start_foo: r is $r\n";
36     whoami($r, "start_foo", "foo1", "foo2");
37     print "start_foo: finished\n";
38 }
39
40 sub start_bar {
41     my $r = 3 + int(10 * rand);
42     print "start_bar: r is $r\n";
43     whoami($r, "start_bar", "bar1", "bar2");
44     print "start_bar: finished\n";
45 }
46
47 $foo = new Thread \&start_foo;
48 $bar = new Thread \&start_bar;
49 print "main: exiting\n";