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.
In reply to Re: failing with Thread::Queue
by BrowserUk
in thread failing with Thread::Queue
by rastoboy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |