blackadder has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Multi Threading the ping process
by Anonymous Monk on Sep 23, 2004 at 20:19 UTC