fx has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What is the correct way to use Thread::Queue::Any ?
by liz (Monsignor) on Jan 07, 2004 at 16:35 UTC | |
by fx (Pilgrim) on Jan 07, 2004 at 17:45 UTC | |
by liz (Monsignor) on Jan 07, 2004 at 17:56 UTC | |
by ysth (Canon) on Jan 07, 2004 at 20:03 UTC | |
|
Re: What is the correct way to use Thread::Queue::Any ?
by Molt (Chaplain) on Jan 07, 2004 at 16:15 UTC | |
by liz (Monsignor) on Jan 07, 2004 at 17:26 UTC | |
|
Re: What is the correct way to use Thread::Queue::Any ?
by BrowserUk (Patriarch) on Jan 07, 2004 at 16:37 UTC | |
by fx (Pilgrim) on Jan 07, 2004 at 17:42 UTC | |
|
Re: What is the correct way to use Thread::Queue::Any ?
by fx (Pilgrim) on Jan 07, 2004 at 18:13 UTC |