I am trying to use Thread::Queue::Any 0.07 by liz but need some help.

The following code works fine:

#!/opt/perl-5.8.2/bin/perl -w use strict; use Thread::Queue::Any; my $value = 1234; my $queue = Thread::Queue::Any->new; $queue->enqueue( $value ); my ($returned_value) = $queue->dequeue; printf "We have returned %i\n", $returned_value;

and prints We have returned 1234 as expected.

The power of a queue, however, comes into its own when used with threads as it allows data to be passed around. It is here that I am having problems.

The Thread::Queue::Any documentation describes the usage of each object method but doesn't give an example using threads.

The following code was my first attempt to get Thread::Queue::Any working with threads:

#!/opt/perl-5.8.2/bin/perl -w use strict; use threads; use Thread::Queue::Any; my $queue = Thread::Queue::Any->new; my $prod_thread = new threads(\&producer); my $cons_thread = new threads(\&consumer); $prod_thread->join; $cons_thread->join; sub producer { my $value = 1234; $queue->enqueue( $value ); $queue->enqueue( undef ); } sub consumer { while( my($returned_value) = $queue->dequeue ) { printf "I have been returned: %i\n", $returned_value; } }

but running it produces:

I have been returned: 1234 Use of uninitialized value in printf at ./2.pl line 23. I have been returned: 0

and then it just sits there doing nothing.

The Perl 5.8.0 thread tutorial uses the idiom of enqueuing an undef to a queue so that the while loop handling dequeuing exits when the undef is dequeued. This does not seem to work with Thread::Queue::Any. Should it?

In an example given in the above tutorial, the queue is passed to the thread as a parameter for the sub. I thought I might need something like that with Thread::Queue::Any so I tried the next piece of code:

#!/opt/perl-5.8.2/bin/perl -w use strict; use threads; use Thread::Queue::Any; my $queue = Thread::Queue::Any->new; my $prod_thread = new threads(\&producer, $queue); my $cons_thread = new threads(\&consumer, $queue); $prod_thread->join; $cons_thread->join; sub producer { my $queue = $_[0]; my $value = 1234; $queue->enqueue( $value ); $queue->enqueue( undef ); } sub consumer { my $queue = $_[0]; while( my($returned_value) = $queue->dequeue ) { printf "I have been returned: %i\n", $returned_value; } }

but this too produced:

I have been returned: 1234 Use of uninitialized value in printf at ./3.pl line 25. I have been returned: 0

and then just sat there too.

And so we reach my question - how should I really be using Thread::Queue::Any across threads?

== fx, Infinity Is Colourless


In reply to What is the correct way to use Thread::Queue::Any ? by fx

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.