Greetings, friends!

So just when I think I've got this threading thing figured out...;-)

I'm trying to use Thread::Queue to coordinate some simple activities which need to occur simultaneously. Basically I need to run them in a loop until X happens. Basically I'm running a wget, while monitoring cpu and memory usage.

The examples I've seen for how to use this module are all feeding information into a thread, where the thread then acts on them in real time. I just want it to continue doing something until I say stop. The technique I'm using--queueing a single bit of data and using Thread::Queue->peek() in the thread to see if it's true, seems to work fine when I have a single thread. But if I have more than one thread, only the last thread created seems to be able to use peek():

#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; use Thread::Queue; my $q = Thread::Queue->new; my $done : shared; $done = 0; my $wget_thread = threads->create( \&wget_sub ); my $cpu_monitor_thread = threads->create( \&monitor_cpu ); my $mem_monitor_thread = threads->create( \&monitor_mem ); $q->enqueue(1); $wget_thread->join(); $q->dequeue; my $cpu = $cpu_monitor_thread->join(); my $mem = $mem_monitor_thread->join(); print "cpu usage: $cpu\n"; print "mem usage: $mem\n"; sub wget_sub { my $wget = `wget http://10.3.13.4/testfile.dat`; $done = 1; lock($done); print "wget:\n$wget\n"; return 1; } 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; } sub monitor_mem { my $cap; while ( my $val = $q->peek(0) ) { $cap .= `perl mem.pl; sleep 2`; } return $cap; }
In this case, only the monitor_mem loop gets executed. If I comment out the memory monitoring thread creation and joining, the cpu stuff works fine.

What am I doing wrong? Any input would be greatly appreciated!

peek(0) ) { $cap .= `bash cpu.sh; sleep 2`; } return $cap; } sub monitor_mem { my $cap; while ( my $val = $q-


In reply to failing with Thread::Queue by rastoboy

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.