in reply to What is the correct way to use Thread::Queue::Any ?

Remove the brackets from the my statement in the while condition.

while( my $returned_value = $queue->dequeue ) { # ^ ^

The code will then behave as you expect.

Update: Sorry! Wrong answer. I hadn't reinstalled Thread::Queue::Any since ungrading and tried it with Thread::Queue instead.

P:\test>type 319494.pl8 use strict; use threads; use Thread::Queue; my $queue = Thread::Queue->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; } } P:\test>319494 I have been returned: 1234 P:\test>

It worked! ...but not with Thread::Queued::Any :(


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!

Replies are listed 'Best First'.
Re: Re: What is the correct way to use Thread::Queue::Any ?
by fx (Pilgrim) on Jan 07, 2004 at 17:42 UTC

    The reason for Thread::Queue::Any over Thread::Queue (if it matters) is that I need to be queuing references to data structures.

    From a previous post of mine it turns out that I cannot do what I need with Thread::Queue otherwise I'd be using it :)

    Hey ho, 'tis a shame :)

    Many thanks.