Dear All,

How reliable is multithreading?

The reason why I ask is this. I found this bit of code here which suppose to ping a list of host name concurrently.
use strict; use threads; use Thread::Queue; use Net::Ping; my $ThreadCount = 6; my @Threads; my $ServerQueue = Thread::Queue->new; my @down; chomp (my @Servers = <DATA>); foreach my $Item (@Servers) { $ServerQueue->enqueue($Item); } for (1..$ThreadCount) { my $Thread = threads->new(\&Process, $ServerQueue); push (@Threads, $Thread); } my $Start_time = time; foreach my $Thread (@Threads) { $Thread->join(); } my $End_time = time; print "Start : $Start_time\n"; print "End : $End_time\n"; my $lapsed = $End_time - $Start_time; print "\n\nLapsed Time (in Seconds) : $lapsed\n"; print "\n\n_________________\n\n"; print "$_\n" for @down; sub Process { my $ServerQueue = shift; my $pingobj = Net::Ping->new("icmp",5,64); while (1) { my $Server = $ServerQueue->dequeue_nb(); last if (! $Server); if ($pingobj->ping($Server)) { print "$Server : Up\n"; } else { push (@down, $Server . " : Down"); } } } __DATA__ SN01BDW02a SN01BDW03a SN01BDW04a SN01BDW05a SN01BDW06a SN01BDW07a SN01BDW08a SN01BDW09a SN01BDW10a SN01BDW11a SN01BDW12a SN01BDW13a SN01BDW14a
Which it does but not accurately. i.e. the script will report a machine is up even when its down!

How do I know this? Because I wrote this poxy script - see below - to do the same but sequentially and its 100% accurate.

Here is the code
use strict; my @down; chomp (my @Servers = <DATA>); my $Start_time = time; foreach (@Servers) { my $res = `ping -n 1 $_`; if ($res =~ /reply/i) { print "$_ : Up\n"; } else { push (@down, $_." : Down"); } } my $End_time = time; print "Start : $Start_time\n"; print "End : $End_time\n"; my $lapsed = $End_time - $Start_time; print "\n\nLapsed Time (in Seconds) : $lapsed\n"; print "\n\n\n___________________________\n\n\n"; print "$_\n" for @down; __DATA__ SN01BDW02a SN01BDW03a SN01BDW04a SN01BDW05a SN01BDW06a SN01BDW07a SN01BDW08a SN01BDW09a SN01BDW10a SN01BDW11a SN01BDW12a SN01BDW13a SN01BDW14a
Can an angle please advise why this state of the art module fails when a script that was written without any thoughts does much better job?

Thanks

BTW: in the multithreaded script what does this mean/do? my $ThreadCount = 6;
Blackadder

In reply to Multi Threading the ping process by blackadder

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.