2 use Thread qw(cond_wait cond_broadcast);
9 Thread::Queue - thread-safe queues (5.005-threads)
13 This Perl installation is using the old unsupported "5.005 threads".
14 Use of the old threads model is discouraged.
16 For the whole story about the development of threads in Perl, and why
17 you should B<not> be using "old threads" unless you know what you're
18 doing, see the CAVEAT of the C<Thread> module.
23 my $q = new Thread::Queue;
24 $q->enqueue("foo", "bar");
25 my $foo = $q->dequeue; # The "bar" is still in the queue.
26 my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was
28 my $left = $q->pending; # returns the number of items still in the queue
32 A queue, as implemented by C<Thread::Queue> is a thread-safe data structure
33 much like a list. Any number of threads can safely add elements to the end
34 of the list, or remove elements from the head of the list. (Queues don't
35 permit adding or removing elements from the middle of the list)
37 =head1 FUNCTIONS AND METHODS
43 The C<new> function creates a new empty queue.
47 The C<enqueue> method adds a list of scalars on to the end of the queue.
48 The queue will grow as needed to accomodate the list.
52 The C<dequeue> method removes a scalar from the head of the queue and
53 returns it. If the queue is currently empty, C<dequeue> will block the
54 thread until another thread C<enqueue>s a scalar.
58 The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from
59 the head of the queue and returns it. Unlike C<dequeue>, though,
60 C<dequeue_nb> won't block if the queue is empty, instead returning
65 The C<pending> method returns the number of items still in the queue. (If
66 there can be multiple readers on the queue it's best to lock the queue
67 before checking to make sure that it stays in a consistent state)
79 return bless [@_], $class;
82 sub dequeue : locked : method {
84 cond_wait $q until @$q;
88 sub dequeue_nb : locked : method {
97 sub enqueue : locked : method {
99 push(@$q, @_) and cond_broadcast $q;
102 sub pending : locked : method {