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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#!./perl
BEGIN {
chdir 't' if -d 't';
unshift @INC, '../lib';
require Config; import Config;
if (! $Config{'usethreads'}) {
print "1..0 # Skip: this perl is not threaded\n";
exit 0;
}
# XXX known trouble with global destruction
$ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
}
$| = 1;
print "1..18\n";
use Thread;
print "ok 1\n";
sub content
{
print shift;
return shift;
}
# create a thread passing args and immedaietly wait for it.
my $t = new Thread \&content,("ok 2\n","ok 3\n", 1..1000);
print $t->join;
# check that lock works ...
{lock $foo;
$t = new Thread sub { lock $foo; print "ok 5\n" };
print "ok 4\n";
}
$t->join;
sub dorecurse
{
my $val = shift;
my $ret;
print $val;
if (@_)
{
$ret = Thread->new(\&dorecurse, @_);
$ret->join;
}
}
$t = new Thread \&dorecurse, map { "ok $_\n" } 6..10;
$t->join;
# test that sleep lets other thread run
$t = new Thread \&dorecurse,"ok 11\n";
sleep 6;
print "ok 12\n";
$t->join;
sub islocked : locked {
my $val = shift;
my $ret;
print $val;
if (@_)
{
$ret = Thread->new(\&islocked, shift);
}
$ret;
}
$t = Thread->new(\&islocked, "ok 13\n", "ok 14\n");
$t->join->join;
{
package Loch::Ness;
sub new { bless [], shift }
sub monster : locked, method {
my($s, $m) = @_;
print "ok $m\n";
}
sub gollum { &monster }
}
Loch::Ness->monster(15);
Loch::Ness->new->monster(16);
Loch::Ness->gollum(17);
Loch::Ness->new->gollum(18);
|