6 if (! $Config{'useithreads'}) {
7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
16 require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
23 ### Basic usage with multiple threads ###
27 my $q = Thread::Queue->new(1..$nthreads);
29 is($q->pending(), $nthreads, 'Pre-populated queue count');
32 my $id = threads->tid();
33 while ((my $el = $q->dequeue()) != -1) {
34 ok($el >= 1, "Thread $id got $el");
35 select(undef, undef, undef, rand(1));
37 ok(1, "Thread $id done");
41 push(@threads, threads->create('reader')) for (1..$nthreads);
44 select(undef, undef, undef, rand(1));
48 $q->enqueue((-1) x $nthreads); # One end marker for each thread
50 $_->join() foreach @threads;
53 is($q->pending(), 0, 'Empty queue');
56 ### ->dequeue_nb() test ###
58 $q = Thread::Queue->new();
60 is($q->pending(), 0, 'Empty queue');
62 my @items = qw/foo bar baz/;
66 is($q->pending(), scalar(@items), 'Queue count in thread');
67 while (my $el = $q->dequeue_nb()) {
68 is($el, shift(@items), "Thread got $el");
70 is($q->pending(), 0, 'Empty queue');
74 is($q->pending(), 1, 'Queue count after thread');
75 is($q->dequeue(), 'done', 'Thread reported done');
76 is($q->pending(), 0, 'Empty queue');
79 ### ->dequeue(COUNT) test ###
84 my $id = threads->tid();
86 my @el = $q->dequeue($count);
87 is(scalar(@el), $count, "Thread $id got @el");
88 select(undef, undef, undef, rand(1));
89 return if ($el[0] == 0);
93 push(@threads, threads->create('reader2')) for (1..$nthreads);
95 $q->enqueue(1..4*$count*$nthreads);
96 $q->enqueue((0) x ($count*$nthreads));
98 $_->join() foreach @threads;
101 is($q->pending(), 0, 'Empty queue');
104 ### ->dequeue_nb(COUNT) test ###
106 @items = qw/foo bar baz qux exit/;
108 is($q->pending(), scalar(@items), 'Queue count');
110 threads->create(sub {
111 is($q->pending(), scalar(@items), 'Queue count in thread');
112 while (my @el = $q->dequeue_nb(2)) {
113 is($el[0], shift(@items), "Thread got $el[0]");
114 if ($el[0] eq 'exit') {
115 is(scalar(@el), 1, 'Thread to exit');
117 is($el[1], shift(@items), "Thread got $el[1]");
120 is($q->pending(), 0, 'Empty queue');
124 is($q->pending(), 1, 'Queue count after thread');
125 is($q->dequeue(), 'done', 'Thread reported done');
126 is($q->pending(), 0, 'Empty queue');