2 package threads::shared::queue;
11 return bless $q, $class;
36 push(@$q, @_) and cond_broadcast @$q;
49 threads::shared::queue - thread-safe queues
53 use threads::shared::queue;
54 my $q = new threads::shared::queue;
55 $q->enqueue("foo", "bar");
56 my $foo = $q->dequeue; # The "bar" is still in the queue.
57 my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was
59 my $left = $q->pending; # returns the number of items still in the queue
63 A queue, as implemented by C<threads::shared::queue> is a thread-safe
64 data structure much like a list. Any number of threads can safely
65 add elements to the end of the list, or remove elements from the head
66 of the list. (Queues don't permit adding or removing elements from
67 the middle of the list).
69 =head1 FUNCTIONS AND METHODS
75 The C<new> function creates a new empty queue.
79 The C<enqueue> method adds a list of scalars on to the end of the queue.
80 The queue will grow as needed to accommodate the list.
84 The C<dequeue> method removes a scalar from the head of the queue and
85 returns it. If the queue is currently empty, C<dequeue> will block the
86 thread until another thread C<enqueue>s a scalar.
90 The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from
91 the head of the queue and returns it. Unlike C<dequeue>, though,
92 C<dequeue_nb> won't block if the queue is empty, instead returning
97 The C<pending> method returns the number of items still in the queue.
103 L<threads>, L<threads::shared>