merge changes#1423,1465 from maintbranch; checkin two missed files
[p5sagit/p5-mst-13.2.git] / t / lib / thread.t
CommitLineData
39e571d4 1#!./perl
bf3d9ec5 2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require Config; import Config;
dfe9444c 7 if (! $Config{'usethreads'}) {
bf3d9ec5 8 print "1..0\n";
9 exit 0;
10 }
eec2d3df 11 $ENV{PERL_DESTRUCT_LEVEL} = 0; # XXX known trouble with global destruction
bf3d9ec5 12}
13$| = 1;
0f5feb8d 14print "1..12\n";
bf3d9ec5 15use Thread;
16print "ok 1\n";
17
18sub content
19{
20 print shift;
21 return shift;
22}
23
24# create a thread passing args and immedaietly wait for it.
25my $t = new Thread \&content,("ok 2\n","ok 3\n");
26print $t->join;
27
28# check that lock works ...
29{lock $foo;
30 $t = new Thread sub { lock $foo; print "ok 5\n" };
31 print "ok 4\n";
32}
33$t->join;
34
35sub islocked
36{
37 use attrs 'locked';
38 my $val = shift;
39 my $ret;
0f5feb8d 40 print $val;
bf3d9ec5 41 if (@_)
42 {
0f5feb8d 43 $ret = Thread->new(\&islocked, @_);
44 join $ret;
bf3d9ec5 45 }
bf3d9ec5 46}
47
0f5feb8d 48$t = new Thread \&islocked, map { "ok $_\n" } 6..10;
49sleep 2;
bf3d9ec5 50join $t;
51
52# test that sleep lets other thread run
0f5feb8d 53$t = new Thread \&islocked,"ok 11\n";
61bb5906 54sleep 6;
0f5feb8d 55print "ok 12\n";
bf3d9ec5 56join $t;