use strict;
use warnings;
use threads;
use Thread::Queue;
my $q = Thread::Queue->new(); # A new empty queue
# Worker thread
my $thr = threads->create(sub {
while (my $item = $q->dequeue()) {
# Do work on $item
}
})->detach();
# Send work to the thread
$q->enqueue($item1, ...);
# Count of items in the queue
my $left = $q->pending();
# Non-blocking dequeue
if (defined(my $item = $q->dequeue_nb())) {
# Work on $item
}
# Get the second item in the queue without dequeuing anything
my $item = $q->peek(1);
# Insert two items into the queue just behind the head
$q->insert(1, $item1, $item2);
# Extract the last two items on the queue
my ($item1, $item2) = $q->extract(-2, 2);
Any data types supported by threads::shared can be passed via queues:
Ordinary scalars are added to queues as they are.
If not already thread-shared, the other complex data types will be cloned (recursively, if needed, and including any "bless"ings and read-only settings) into thread-shared structures before being placed onto a queue.
For example, the following would cause Thread::Queue to create a empty, shared array reference via "&shared([])", copy the elements 'foo', 'bar' and 'baz' from @ary into it, and then place that shared reference onto the queue:
my @ary = qw/foo bar baz/;
$q->enqueue(\@ary);
However, for the following, the items are already shared, so their references are added directly to the queue, and no cloning takes place:
my @ary :shared = qw/foo bar baz/;
$q->enqueue(\@ary);
my $obj = &shared({});
$$obj{'foo'} = 'bar';
$$obj{'qux'} = 99;
bless($obj, 'My::Class');
$q->enqueue($obj);
See ``LIMITATIONS'' for caveats related to passing objects via queues.
To prevent the contents of a queue from being modified by another thread while it is being examined and/or changed, lock the queue inside a local block:
{
lock($q); # Keep other threads from changing the queue's contents
my $item = $q->peek();
if ($item ...) {
...
}
}
# Queue is now unlocked
If no items exists at the specified index (i.e., the queue is empty, or the index is beyond the number of items on the queue), then "undef" is returned.
Remember, the returned item is not removed from the queue, so manipulating a "peek"ed at reference affects the item on the queue.
$q->enqueue(1, 2, 3, 4);
$q->insert(1, qw/foo bar/);
# Queue now contains: 1, foo, bar, 2, 3, 4
Specifying an index position greater than the number of items in the queue just adds the list to the end.
Negative index positions are supported:
$q->enqueue(1, 2, 3, 4);
$q->insert(-2, qw/foo bar/);
# Queue now contains: 1, 2, foo, bar, 3, 4
Specifying a negative index position greater than the number of items in the queue adds the list to the head of the queue.
This method is non-blocking, and will return only as many items as are available to fulfill the request:
$q->enqueue(1, 2, 3, 4);
my $item = $q->extract(2) # Returns 3
# Queue now contains: 1, 2, 4
my @items = $q->extract(1, 3) # Returns (2, 4)
# Queue now contains: 1
Specifying an index position greater than the number of items in the queue results in "undef" or an empty list being returned.
$q->enqueue('foo');
my $nada = $q->extract(3) # Returns undef
my @nada = $q->extract(1, 3) # Returns ()
Negative index positions are supported. Specifying a negative index position greater than the number of items in the queue may return items from the head of the queue (similar to "dequeue_nb") if the count overlaps the head of the queue from the specified position (i.e. if queue size + index + count is greater than zero):
$q->enqueue(qw/foo bar baz/);
my @nada = $q->extract(-6, 2); # Returns () - (3+(-6)+2) <= 0
my @some = $q->extract(-6, 4); # Returns (foo) - (3+(-6)+4) > 0
# Queue now contains: bar, baz
my @rest = $q->extract(-3, 4); # Returns (bar, baz) - (2+(-3)+4) > 0
Passing array/hash refs that contain objects may not work for Perl prior to 5.10.0.
Annotated POD for Thread::Queue: <http://annocpan.org/~JDHEDDEN/Thread-Queue-2.11/lib/Thread/Queue.pm>
Source repository: <http://code.google.com/p/thread-queue/>