summaryrefslogtreecommitdiffstats
path: root/contrib/perl5/ext/Thread/sync2.t
blob: 0901da46a0abe01a6da3b117c057ba0d265b0572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use Thread;

$global = undef;

sub single_file {
    use attrs 'locked';
    my $who = shift;
    my $i;

    print "Uh oh: $who entered while locked by $global\n" if $global;
    $global = $who;
    print "[";
    for ($i = 0; $i < int(10 * rand); $i++) {
	print $who;
	select(undef, undef, undef, 0.1);
    }
    print "]";
    $global = undef;
}

sub start_a {
    my ($i, $j);
    for ($j = 0; $j < 10; $j++) {
	single_file("A");
	for ($i = 0; $i < int(10 * rand); $i++) {
	    print "a";
	    select(undef, undef, undef, 0.1);
	}
    }
}

sub start_b {
    my ($i, $j);
    for ($j = 0; $j < 10; $j++) {
	single_file("B");
	for ($i = 0; $i < int(10 * rand); $i++) {
	    print "b";
	    select(undef, undef, undef, 0.1);
	}
    }
}

sub start_c {
    my ($i, $j);
    for ($j = 0; $j < 10; $j++) {
	single_file("C");
	for ($i = 0; $i < int(10 * rand); $i++) {
	    print "c";
	    select(undef, undef, undef, 0.1);
	}
    }
}

$| = 1;
srand($$^$^T);

print <<'EOT';
Each pair of square brackets [...] should contain a repeated sequence of
a unique upper case letter. Lower case letters may appear randomly both
in and out of the brackets.
EOT
$foo = new Thread \&start_a;
$bar = new Thread \&start_b;
$baz = new Thread \&start_c;
print "\nmain: joining...\n";
#$foo->join;
#$bar->join;
#$baz->join;
print "\ndone\n";
OpenPOWER on IntegriCloud