I am trying to figure out how to implement threads to ping about 10-20 hosts at a time. Win32(XP) Perl v5.8.0 built for MSWin32-x86-multi-thread
use threads; use threads::shared; use strict; my $wait = 500; my @up: shared; my $th; my @down: shared; my @list = qw ( www.apple.com www.perl.org www.ibm.com www.coke.com ww +w.oracle.com www.hp.com ); foreach(@list){ $th = threads->create("ping","$_"); # $th->join; # new thread is NOT created until join # not what I want! } # This works... unless one of the threads finishes too # soon... then the app dies... $th->join; print @up; print @down; sub ping { my $ip = shift; my @rv = `ping -n 1 -w $wait $ip`; foreach(@rv){ push @up, "$ip up\n" if /\(0% loss\)/; push @down, "$ip down\n" if /\(100% loss\)/; } }
Ok, the problem is that if I join in the block where the threads get created... it waits for each process to finish. If I join after the create threads... it works... BUT ONLY IF all the threads finish in order... And, if I don't join at all... I don't get anything I have tried the async method, and got the same results. Are all the threads dependant on each other? If you have to wait on each thread to finish... what is the use in threads?

In reply to Using threads with Ping by JamesNC

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.