perlfunc
root
<P>
Thread::Queue - thread-safe queues
<P>
<HR>
<P>
<PRE> use Thread::Queue;
my $q = new Thread::Queue;
$q->enqueue("foo", "bar");
my $foo = $q->dequeue; # The "bar" is still in the queue.
my $foo = $q->dequeue_nb; # returns "bar",
<P>
<FONT SIZE=-1>A</FONT> queue, as implemented by <CODE>Thread::Queue</CODE> is a thread-safe data structure much like a list. Any number of threads can
safely add elements to the end of the list, or remove elements from the
head of the list. (Queues don't permit adding or removing elements from the
middle of the list)
<P>
<HR>
<H1><A NAME="FUNCTIONS_AND_METHODS">FUNCTIONS AND METHODS</A></H1>
<DL>
<DT><STRONG><A NAME="item_new">new</A></STRONG><P>
<DD>
The <CODE>new</CODE> function creates a new empty queue.
<P><DT><STRONG><A NAME="item_enqueue">enqueue LIST</A></STRONG><P>
<DD>
The <CODE>enqueue</CODE> method adds a list of scalars on to the end of the queue. The queue will
grow as needed to accomodate the list.
<P><DT><STRONG><A NAME="item_dequeue">dequeue</A></STRONG><P>
<DD>
The <CODE>dequeue</CODE> method removes a scalar from the head of the queue and returns it. If the
queue is currently empty, <CODE>dequeue</CODE> will block the thread until another thread <CODE>enqueue</CODE>s a scalar.
<P><DT><STRONG><A NAME="item_dequeue_nb">dequeue_nb</A></STRONG><P>
<DD>
The <CODE>dequeue_nb</CODE> method, like the <CODE>dequeue</CODE> method, removes a scalar from the head of the queue and returns it. Unlike <CODE>dequeue</CODE>, though,
<CODE>dequeue_nb</CODE> won't block if the queue is empty, instead returning
[perlfunc:undef|undef].
<P><DT><STRONG><A NAME="item_pending">pending</A></STRONG><P>
<DD>
The <CODE>pending</CODE> method returns the number of items still in the queue. (If there can be
multiple readers on the queue it's best to lock the queue before checking
to make sure that it stays in a consistent state)
</DL>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
[perlman:lib:Thread|Thread]
=cut
<P>
sub new { my <CODE>$class</CODE> = shift; return bless [@_], $class; }
<P>
sub dequeue { use attrs
<CODE>qw(locked</CODE> method); my <A
HREF="../../pod/perlop.html#item__q">$q</A> = shift;
cond_wait [perlman:perlop|perlop]
until @$q; return shift @$q; }
<P>
sub dequeue_nb { use attrs
<CODE>qw(locked</CODE> method); my <A
HREF="../../pod/perlop.html#item__q">$q</A> = shift; if
(@$q) { return shift @$q; } else { return undef; } }
<P>
sub enqueue { use attrs
<CODE>qw(locked</CODE> method); my [perlman:perlop|perlop] = shift;
<CODE>push(@$q,</CODE> @_) and cond_broadcast $q; }
<P>
sub pending { use attrs
<CODE>qw(locked</CODE> method); my [perlman:perlop|perlop] = shift; return
<CODE>scalar(@$q);</CODE> }
<P>
1;
<HR>