This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Indent the test a bit.
[perl5.git] / ext / threads / t / basic.t
CommitLineData
47ba8780 1
a54396a0
AB
2
3#
4# The reason this does not use a Test module is that
5# they mess up test numbers between threads
6#
7# And even when that will be fixed, this is a basic
8# test and should not rely on shared variables
9#
10#
47ba8780
AB
11#########################
12
a54396a0 13
83ebf7e2
RGS
14BEGIN {
15 chdir 't' if -d 't';
16 @INC = '../lib';
17 require Config; import Config;
18 unless ($Config{'useithreads'}) {
19 print "1..0 # Skip: no useithreads\n";
20 exit 0;
21 }
22}
23
47ba8780 24use ExtUtils::testlib;
47ba8780 25use strict;
a54396a0 26BEGIN { print "1..12\n" };
47ba8780
AB
27use threads;
28
29
a54396a0
AB
30
31print "ok 1\n";
32
47ba8780
AB
33
34#########################
35
36# Insert your test code below, the Test module is use()ed here so read
37# its man page ( perldoc Test ) for help writing this test script.
38#my $bar;
a54396a0
AB
39sub ok {
40 my ($id, $ok, $name) = @_;
41
42 # You have to do it this way or VMS will get confused.
43 print $ok ? "ok $id - $name\n" : "not ok $id - $name\n";
44
45 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
46
47 return $ok;
48}
49
50
47ba8780 51
47ba8780
AB
52
53#test passing of simple argument
a54396a0 54my $thread = threads->create(sub { ok(2, 'bar' eq $_[0]),"" },"bar");
47ba8780 55$thread->join();
a54396a0 56
47ba8780
AB
57
58#test passing of complex argument
59
a54396a0 60$thread = threads->create(sub { ok(3, 'bar' eq $_[0]->[0]->{foo})},[{foo => 'bar'}]);
47ba8780
AB
61
62$thread->join();
a54396a0 63
47ba8780
AB
64
65#test execuion of normal sub
a54396a0 66sub bar { ok(4,shift() == 1,"") }
47ba8780 67threads->create(\&bar,1)->join();
a54396a0 68
47ba8780
AB
69
70#check Config
a54396a0 71ok(5, 1 == $Config::threads,"");
47ba8780
AB
72
73#test trying to detach thread
74
a54396a0 75my $thread1 = threads->create(sub {ok(6,1,"")});
47ba8780
AB
76
77$thread1->detach();
47ba8780 78sleep 1;
a54396a0 79ok(7,1,"");
47ba8780
AB
80#create nested threads
81unless($^O eq 'MSWin32') {
82 my $thread3 = threads->create(sub { threads->create(sub {})})->join();
47ba8780 83}
a54396a0 84
47ba8780 85
47ba8780 86unless($^O eq 'MSWin32') {
ea63ed0e
JH
87 my @threads;
88 my $i;
89 for(1..25) {
90 push @threads,
91 threads->create(
92 sub {
93 for(1..100000) { my $i }
94 threads->create(
95 sub {sleep 2})
96 ->join()
97 }
98 );
99 }
100 foreach my $thread (@threads) {
47ba8780 101 $thread->join();
ea63ed0e 102 }
47ba8780 103}
a54396a0 104ok(8,1,"");
47ba8780
AB
105threads->create(sub {
106 my $self = threads->self();
a54396a0 107 ok(9,$self->tid() == 57,"");
47ba8780 108})->join();
47ba8780
AB
109threads->create(sub {
110 my $self = threads->self();
a54396a0 111 ok(10,$self->tid() == 58,"");
47ba8780 112})->join();
47ba8780
AB
113
114#check support for threads->self() in main thread
a54396a0
AB
115ok(11, 0 == threads->self->tid(),"");
116ok(12, 0 == threads->tid(),"Check so that tid for threads work for current tid");
47ba8780
AB
117
118
119
120
121
122
123
124
125
126