5 if ($ENV{'PERL_CORE'}){
7 unshift(@INC, '../lib');
10 if (! $Config{'useithreads'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
20 require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
27 ### Basic usage with multiple threads ###
31 my $q = Thread::Queue->new(1..$nthreads);
33 is($q->pending(), $nthreads, 'Pre-populated queue count');
36 my $id = threads->tid();
37 while ((my $el = $q->dequeue()) != -1) {
38 ok($el >= 1, "Thread $id got $el");
39 select(undef, undef, undef, rand(1));
41 ok(1, "Thread $id done");
45 push(@threads, threads->create('reader')) for (1..$nthreads);
48 select(undef, undef, undef, rand(1));
52 $q->enqueue((-1) x $nthreads); # One end marker for each thread
54 $_->join() foreach @threads;
57 is($q->pending(), 0, 'Empty queue');
60 ### ->dequeue_nb() test ###
62 $q = Thread::Queue->new();
64 is($q->pending(), 0, 'Empty queue');
66 my @items = qw/foo bar baz/;
70 is($q->pending(), scalar(@items), 'Queue count in thread');
71 while (my $el = $q->dequeue_nb()) {
72 is($el, shift(@items), "Thread got $el");
74 is($q->pending(), 0, 'Empty queue');
78 is($q->pending(), 1, 'Queue count after thread');
79 is($q->dequeue(), 'done', 'Thread reported done');
80 is($q->pending(), 0, 'Empty queue');
83 ### ->dequeue(COUNT) test ###
88 my $id = threads->tid();
90 my @el = $q->dequeue($count);
91 is(scalar(@el), $count, "Thread $id got @el");
92 select(undef, undef, undef, rand(1));
93 return if ($el[0] == 0);
97 push(@threads, threads->create('reader2')) for (1..$nthreads);
99 $q->enqueue(1..4*$count*$nthreads);
100 $q->enqueue((0) x ($count*$nthreads));
102 $_->join() foreach @threads;
105 is($q->pending(), 0, 'Empty queue');
108 ### ->dequeue_nb(COUNT) test ###
110 @items = qw/foo bar baz qux exit/;
112 is($q->pending(), scalar(@items), 'Queue count');
114 threads->create(sub {
115 is($q->pending(), scalar(@items), 'Queue count in thread');
116 while (my @el = $q->dequeue_nb(2)) {
117 is($el[0], shift(@items), "Thread got $el[0]");
118 if ($el[0] eq 'exit') {
119 is(scalar(@el), 1, 'Thread to exit');
121 is($el[1], shift(@items), "Thread got $el[1]");
124 is($q->pending(), 0, 'Empty queue');
128 is($q->pending(), 1, 'Queue count after thread');
129 is($q->dequeue(), 'done', 'Thread reported done');
130 is($q->pending(), 0, 'Empty queue');