in reply to failing with Thread::Queue
What am I doing wrong?
Which is not its purpose.
This method, along with insert() and extract() should never have been added to a queue module.
They create far more problems than they solve.
my $q = Thread::Queue->new; ## create a new **EMPTY** queue my $done : shared; $done = 0; my $wget_thread = threads->create( \&wget_sub ); my $cpu_monitor_thread = threads->create( \&monitor_cpu ); ## Start y +our threads with the queue empty/ my $mem_monitor_thread = threads->create( \&monitor_mem )
When those threads run:
sub monitor_cpu { my $cap; while ( my $val = $q->peek(0) ) { #this does not get evaluated to + true, ever $cap .= `bash cpu.sh; sleep 2`; } return $cap; }
The queue is empty so they go straight passed the loop and out the other end.
If all you want is a way to tell your threads to stop, use a single shared variable:
my $done :shared = 0; ... sub monitor_cpu { my $cap; until( $done ) { $cap .= `bash cpu.sh; sleep 2`; } return $cap; } ... my $mon = threads->new( \&monitor_cpu ); ... do other stuff ## To stop the queue(s) $done = 1; $mon->join;
Though quite what the purpose of your cpu/mem monitoring is, I do not understand, because you are only going to get the last report returned, which is utterly meaningless as a measure of anything.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: failing with Thread::Queue
by rastoboy (Monk) on Mar 22, 2012 at 21:08 UTC | |
by BrowserUk (Patriarch) on Mar 22, 2012 at 21:36 UTC | |
by rastoboy (Monk) on Mar 22, 2012 at 22:10 UTC | |
by BrowserUk (Patriarch) on Mar 22, 2012 at 21:22 UTC |