Coro::PriorityQueue
use Coro::PriorityQueue; use Coro; my $queue = Coro::PriorityQueue->new(10); my $producer = async { foreach my $i (1 .. 10) { $queue->insert($i); } $queue->shutdown; }; my $consumer = async { while (1) { my $i = $queue->remove or last; printf("%d * 2 = %d\n", $i, $i * 2); } }; $producer->join, $consumer->join;
Priority queues automatically order their contents according to the inserted item's priority. Calling code must ensure that their queue items are comparable via this strategy (e.g. by overloading the <=> operator).
Underneath, this is implemented as a simple array heap, using Coro::Semaphore to control access.
Creates a new queue that can store $max items.
$max
Inserts an item into the queue. Will block the thread until a slot is available if necessary. If the queue has been shut down, croaks.
It is an error to insert undef into the queue.
Removes and returns an item from the queue. Blocks until an item becomes available if necessary. If the queue is shutdown, returns undefined immediately.
Returns the number of items currently stored.
Returns true if the queue is empty.
Returns true if the queue is full.
Shuts down the queue, after which no items may be inserted. Items already in the queue can be pulled normally until empty, after which further calls to remove will return undefined.
remove
Jeff Ober <jeffober@gmail.com>
To install Coro::PriorityQueue, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Coro::PriorityQueue
CPAN shell
perl -MCPAN -e shell install Coro::PriorityQueue
For more information on module installation, please visit the detailed CPAN module installation guide.