2 use Thread qw(cond_wait cond_broadcast);
6 Thread::Queue - thread-safe queues
11 my $q = new Thread::Queue;
12 $q->enqueue("foo", "bar");
13 my $foo = $q->dequeue; # The "bar" is still in the queue.
14 my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was
16 my $left = $q->pending; # returns the number of items still in the queue
20 A queue, as implemented by C<Thread::Queue> is a thread-safe data structure
21 much like a list. Any number of threads can safely add elements to the end
22 of the list, or remove elements from the head of the list. (Queues don't
23 permit adding or removing elements from the middle of the list)
25 =head1 FUNCTIONS AND METHODS
31 The C<new> function creates a new empty queue.
35 The C<enqueue> method adds a list of scalars on to the end of the queue.
36 The queue will grow as needed to accomodate the list.
40 The C<dequeue> method removes a scalar from the head of the queue and
41 returns it. If the queue is currently empty, C<dequeue> will block the
42 thread until another thread C<enqueue>s a scalar.
46 The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from
47 the head of the queue and returns it. Unlike C<dequeue>, though,
48 C<dequeue_nb> won't block if the queue is empty, instead returning
53 The C<pending> method returns the number of items still in the queue. (If
54 there can be multiple readers on the queue it's best to lock the queue
55 before checking to make sure that it stays in a consistent state)
67 return bless [@_], $class;
71 use attrs qw(locked method);
73 cond_wait $q until @$q;
78 use attrs qw(locked method);
88 use attrs qw(locked method);
90 push(@$q, @_) and cond_broadcast $q;
94 use attrs qw(locked method);