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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
BEGIN {
chdir 't' if -d 't';
push @INC, '../lib','.';
require Config; import Config;
unless ($Config{'useithreads'}) {
print "1..0 # Skip: no useithreads\n";
exit 0;
}
require "test.pl";
}
use ExtUtils::testlib;
use strict;
BEGIN { $| = 1; print "1..25\n" };
use threads;
use threads::shared;
print "ok 1\n";
sub content {
print shift;
return shift;
}
{
my $t = threads->new(\&content, "ok 2\n", "ok 3\n", 1..1000);
print $t->join();
}
{
my $lock : shared;
my $t;
{
lock($lock);
$t = threads->new(sub { lock($lock); print "ok 5\n"});
print "ok 4\n";
}
$t->join();
}
sub dorecurse {
my $val = shift;
my $ret;
print $val;
if(@_) {
$ret = threads->new(\&dorecurse, @_);
$ret->join;
}
}
{
my $t = threads->new(\&dorecurse, map { "ok $_\n" } 6..10);
$t->join();
}
{
# test that sleep lets other thread run
my $t = threads->new(\&dorecurse, "ok 11\n");
threads->yield; # help out non-preemptive thread implementations
sleep 1;
print "ok 12\n";
$t->join();
}
{
my $lock : shared;
sub islocked {
lock($lock);
my $val = shift;
my $ret;
print $val;
if (@_) {
$ret = threads->new(\&islocked, shift);
}
return $ret;
}
my $t = threads->new(\&islocked, "ok 13\n", "ok 14\n");
$t->join->join;
}
sub testsprintf {
my $testno = shift;
my $same = sprintf( "%0.f", $testno);
return $testno eq $same;
}
sub threaded {
my ($string, $string_end) = @_;
# Do the match, saving the output in appropriate variables
$string =~ /(.*)(is)(.*)/;
# Yield control, allowing the other thread to fill in the match variables
threads->yield();
# Examine the match variable contents; on broken perls this fails
return $3 eq $string_end;
}
{
curr_test(15);
my $thr1 = threads->new(\&testsprintf, 15);
my $thr2 = threads->new(\&testsprintf, 16);
my $short = "This is a long string that goes on and on.";
my $shorte = " a long string that goes on and on.";
my $long = "This is short.";
my $longe = " short.";
my $foo = "This is bar bar bar.";
my $fooe = " bar bar bar.";
my $thr3 = new threads \&threaded, $short, $shorte;
my $thr4 = new threads \&threaded, $long, $longe;
my $thr5 = new threads \&testsprintf, 19;
my $thr6 = new threads \&testsprintf, 20;
my $thr7 = new threads \&threaded, $foo, $fooe;
ok($thr1->join());
ok($thr2->join());
ok($thr3->join());
ok($thr4->join());
ok($thr5->join());
ok($thr6->join());
ok($thr7->join());
}
# test that 'yield' is importable
package Test1;
use threads 'yield';
yield;
main::ok(1);
package main;
# test async
{
my $th = async {return 1 };
ok($th);
ok($th->join());
}
{
# there is a little chance this test case will falsly fail
# since it tests rand
my %rand : shared;
rand(10);
threads->new( sub { $rand{int(rand(10000000000))}++ } ) foreach 1..25;
$_->join foreach threads->list;
# use Data::Dumper qw(Dumper);
# print Dumper(\%rand);
#$val = rand();
ok((keys %rand == 25), "Check that rand works after a new thread");
}
|