8 use Thread qw(cond_wait cond_broadcast);
12 $ithreads = $Config{useithreads};
13 $othreads = $Config{use5005threads};
15 require 'threads/shared/queue.pm';
16 for my $m (qw(new enqueue dequeue dequeue_nb pending)) {
18 *{"Thread::Queue::$m"} = \&{"threads::shared::queue::${m}"};
21 for my $m (qw(new enqueue dequeue dequeue_nb pending)) {
23 *{"Thread::Queue::$m"} = \&{"Thread::Queue::${m}_othread"};
31 Thread::Queue - thread-safe queues
36 my $q = new Thread::Queue;
37 $q->enqueue("foo", "bar");
38 my $foo = $q->dequeue; # The "bar" is still in the queue.
39 my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was
41 my $left = $q->pending; # returns the number of items still in the queue
45 A queue, as implemented by C<Thread::Queue> is a thread-safe data structure
46 much like a list. Any number of threads can safely add elements to the end
47 of the list, or remove elements from the head of the list. (Queues don't
48 permit adding or removing elements from the middle of the list)
50 =head1 FUNCTIONS AND METHODS
56 The C<new> function creates a new empty queue.
60 The C<enqueue> method adds a list of scalars on to the end of the queue.
61 The queue will grow as needed to accomodate the list.
65 The C<dequeue> method removes a scalar from the head of the queue and
66 returns it. If the queue is currently empty, C<dequeue> will block the
67 thread until another thread C<enqueue>s a scalar.
71 The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from
72 the head of the queue and returns it. Unlike C<dequeue>, though,
73 C<dequeue_nb> won't block if the queue is empty, instead returning
78 The C<pending> method returns the number of items still in the queue. (If
79 there can be multiple readers on the queue it's best to lock the queue
80 before checking to make sure that it stays in a consistent state)
92 return bless [@_], $class;
95 sub dequeue_othread : locked : method {
97 cond_wait $q until @$q;
101 sub dequeue_nb_othread : locked : method {
110 sub enqueue_othread : locked : method {
112 push(@$q, @_) and cond_broadcast $q;
115 sub pending_othread : locked : method {