Lines Matching full:queue
1 package Thread::Queue;
15 # Create a new queue possibly pre-populated with items
19 my @queue :shared = map { shared_clone($_) } @_;
20 my %self :shared = ( 'queue' => \@queue );
24 # Add items to the tail of a queue
32 Carp::croak("'enqueue' method called on queue that has been 'end'ed");
35 # Block if queue size exceeds any specified limit
36 my $queue = $$self{'queue'};
37 cond_wait(%$self) while ($$self{'LIMIT'} && (@$queue >= $$self{'LIMIT'}));
39 # Add items to queue, and then signal other threads
40 push(@$queue, map { shared_clone($_) } @_)
44 # Set or return the max. size for a queue
52 # Return a count of the number of items on a queue
57 return if ($$self{'ENDED'} && ! @{$$self{'queue'}});
58 return scalar(@{$$self{'queue'}});
61 # Indicate that no more data will enter the queue
72 # Return 1 or more items from the head of a queue, blocking if needed
77 my $queue = $$self{'queue'};
82 cond_wait(%$self) while ((@$queue < $count) && ! $$self{'ENDED'});
84 # If no longer blocking, try getting whatever is left on the queue
89 my $item = shift(@$queue);
96 push(@items, shift(@$queue)) for (1..$count);
101 # Return items from the head of a queue with no blocking
106 my $queue = $$self{'queue'};
112 my $item = shift(@$queue);
120 last if (! @$queue);
121 push(@items, shift(@$queue));
127 # Return items from the head of a queue, blocking if needed up to a timeout
132 my $queue = $$self{'queue'};
144 while ((@$queue < $count) && ! $$self{'ENDED'}) {
148 # Get whatever we need off the queue if available
152 # Return an item without removing it from a queue
158 return $$self{'queue'}[$index];
161 # Insert items anywhere into a queue
169 Carp::croak("'insert' method called on queue that has been 'end'ed");
172 my $queue = $$self{'queue'};
180 $index += @$queue;
188 while (@$queue > $index) {
189 unshift(@tmp, pop(@$queue))
192 # Add new items to the queue
193 push(@$queue, map { shared_clone($_) } @_);
195 # Add previous items back onto the queue
196 push(@$queue, @tmp);
201 # Remove items from anywhere in a queue
206 my $queue = $$self{'queue'};
213 $index += @$queue;
216 return if ($count <= 0); # Beyond the head of the queue
223 while (@$queue > ($index+$count)) {
224 unshift(@tmp, pop(@$queue))
229 unshift(@items, pop(@$queue)) while (@$queue > $index);
232 push(@$queue, @tmp);
284 …Carp::croak("'count' argument ($count) to '$method' method exceeds queue size limit ($$self{'LIMIT…
317 Thread::Queue - Thread-safe queues
321 This document describes Thread::Queue version 3.14
329 use Thread::Queue;
331 my $q = Thread::Queue->new(); # A new empty queue
353 # Count of items in the queue
366 # Set a size for a queue
369 # Get the second item in the queue without dequeuing anything
372 # Insert two items into the queue just behind the head
375 # Extract the last two items on the queue
403 settings) into thread-shared structures before being placed onto a queue.
405 For example, the following would cause L<Thread::Queue> to create a empty,
408 the queue:
414 are added directly to the queue, and no cloning takes place:
427 =head1 QUEUE CREATION
433 Creates a new empty queue.
437 Creates a new queue pre-populated with the provided list of items.
449 Adds a list of items onto the end of the queue.
456 queue, and returns them. If the queue contains fewer than the requested
465 queue, and returns them. If the queue contains fewer than the requested
467 items there are on the queue. If the queue is empty, then C<undef> is
475 queue, and returns them. If the queue contains fewer than the requested
478 reached, it returns whatever items there are on the queue, or C<undef> if the
479 queue is empty.
493 Returns the number of items still in the queue. Returns C<undef> if the queue
494 has been ended (see below), and there are no more items in the queue.
498 Sets the size of the queue. If set, calls to C<enqueue()> will block until
499 the number of pending items in the queue drops below the C<limit>. The
502 my $q = Thread::Queue->new(1, 2);
509 $q->limit = 0; # Queue size is now unlimited
511 Calling any of the dequeue methods with C<COUNT> greater than a queue's
516 Declares that no more items will be added to the queue.
519 remaining items in the queue and/or C<undef> being returned. Any subsequent
522 Once ended, no more items may be placed in the queue.
528 The following methods can be used to manipulate items anywhere in a queue.
530 To prevent the contents of a queue from being modified by another thread
532 VARIABLE"> the queue inside a local block:
535 lock($q); # Keep other threads from changing the queue's contents
541 # Queue is now unlocked
549 Returns an item from the queue without dequeuing anything. Defaults to the
550 head of queue (at index position 0) if no index is specified. Negative
552 is the end of the queue, -2 is next to last, and so on).
554 If no items exists at the specified index (i.e., the queue is empty, or the
555 index is beyond the number of items on the queue), then C<undef> is returned.
557 Remember, the returned item is not removed from the queue, so manipulating a
558 C<peek>ed at reference affects the item on the queue.
562 Adds the list of items to the queue at the specified index position (0
568 # Queue now contains: 1, foo, bar, 2, 3, 4
570 Specifying an index position greater than the number of items in the queue
577 # Queue now contains: 1, 2, foo, bar, 3, 4
580 queue adds the list to the head of the queue.
589 specified index position in the queue (0 is the head of the queue). When
597 # Queue now contains: 1, 2, 4
599 # Queue now contains: 1
602 queue results in C<undef> or an empty list being returned.
609 greater than the number of items in the queue may return items from the head
610 of the queue (similar to C<dequeue_nb>) if the count overlaps the head of the
611 queue from the specified position (i.e. if queue size + index + count is
617 # Queue now contains: bar, baz
625 Queues created by L<Thread::Queue> can be used in both threaded and
638 Thread::Queue on MetaCPAN:
639 L<https://metacpan.org/release/Thread-Queue>
642 L<https://github.com/Dual-Life/Thread-Queue>