This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add standard core test headers to the Class::ISA new tests
[perl5.git] / lib / Thread / Queue.t
CommitLineData
13c1b207 1use warnings;
6d1f61ba
AB
2
3BEGIN {
89661126
AB
4 chdir 't' if -d 't';
5 push @INC ,'../lib';
6 require Config; import Config;
7 unless ($Config{'useithreads'}) {
83272a45 8 print "1..0 # Skip: no ithreads\n";
6d1f61ba 9 exit 0;
89661126 10 }
6d1f61ba
AB
11}
12
13c1b207 13use strict;
6d1f61ba 14use threads;
83272a45 15use Thread::Queue;
6d1f61ba 16
83272a45 17my $q = new Thread::Queue;
cd055192 18$|++;
89661126
AB
19print "1..26\n";
20
2a4145a8 21my $test : shared = 1;
974236b4 22
a438a718
AB
23sub ok {
24 lock($test);
25 print "ok $test\n";
26 $test++;
27}
28
6d1f61ba
AB
29sub reader {
30 my $tid = threads->self->tid;
31 my $i = 0;
32 while (1) {
33 $i++;
34# print "reader (tid $tid): waiting for element $i...\n";
35 my $el = $q->dequeue;
a438a718
AB
36 ok();
37# print "ok $test\n"; $test++;
6d1f61ba 38# print "reader (tid $tid): dequeued element $i: value $el\n";
89661126 39 select(undef, undef, undef, rand(1));
6d1f61ba
AB
40 if ($el == -1) {
41 # end marker
42# print "reader (tid $tid) returning\n";
43 return;
44 }
45 }
46}
47
89661126 48my $nthreads = 5;
6d1f61ba
AB
49my @threads;
50
51for (my $i = 0; $i < $nthreads; $i++) {
52 push @threads, threads->new(\&reader, $i);
53}
54
89661126 55for (my $i = 1; $i <= 20; $i++) {
6d1f61ba 56 my $el = int(rand(100));
89661126 57 select(undef, undef, undef, rand(1));
6d1f61ba
AB
58# print "writer: enqueuing value $el\n";
59 $q->enqueue($el);
60}
61
62$q->enqueue((-1) x $nthreads); # one end marker for each thread
63
64for(@threads) {
89661126 65# print "waiting for join\n";
6d1f61ba 66 $_->join();
6d1f61ba 67}
a438a718
AB
68ok();
69#print "ok $test\n";
6d1f61ba
AB
70
71