Thread::Queue::Any - thread-safe queues for any data-structure
use Thread::Queue::Any; my $q = Thread::Queue::Any->new; $q->enqueue("foo", ["bar"], {"zoo"}); my ($foo,$bar,$zoo) = $q->dequeue; my ($foo,$bar,$zoo) = $q->dequeue_nb; my $left = $q->pending;
A queue, as implemented by Thread::Queue::Any is a thread-safe data structure that inherits from Thread::Queue. But unlike the standard Thread::Queue, you can pass (a reference to) any data structure to the queue.
Thread::Queue::Any
Thread::Queue
Apart from the fact that the parameters to enqueue are considered to be a set that needs to be enqueued together and that dequeue returns all of the parameters that were enqueued together, this module is a drop-in replacement for Thread::Queue in every other aspect.
enqueue
dequeue
Any number of threads can safely add elements to the end of the list, or remove elements from the head of the list. (Queues don't permit adding or removing elements from the middle of the list).
The new function creates a new empty queue.
new
The enqueue method adds a reference to all the specified parameters on to the end of the queue. The queue will grow as needed.
The dequeue method removes a reference from the head of the queue, dereferences it and returns the resulting values. If the queue is currently empty, dequeue will block the thread until another thread enqueues.
The dequeue_nb method, like the dequeue method, removes a scalar from the head of the queue and returns it. Unlike dequeue, though, dequeue_nb won't block if the queue is empty, instead returning undef.
dequeue_nb
undef
The pending method returns the number of items still in the queue.
pending
Passing unshared values between threads is accomplished by serializing the specified values using Storable when enqueuing and de-serializing the queued value on dequeuing. This allows for great flexibility at the expense of more CPU usage. It also limits what can be passed, as e.g. code references can not be serialized and therefor not be passed.
Storable
Elizabeth Mattijsen, <liz@dijkmat.nl>.
Please report bugs to <perlbugs@dijkmat.nl>.
Copyright (c) 2002 Elizabeth Mattijsen <liz@dijkmat.nl>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
threads, threads::shared, Thread::Queue.
To install Thread::Queue::Any, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Thread::Queue::Any
CPAN shell
perl -MCPAN -e shell install Thread::Queue::Any
For more information on module installation, please visit the detailed CPAN module installation guide.