Rather than starting/stopping threads to feed input, it would be preferable to feed them work to do. Here is a snipped of the code I use (In production on Windows/perl 58/ Activestate):
use threads;
use Thread::Queue;
our $KIDS = $count > 20? 10 : int ($count / 4) || 1; # Some way to c
+ontrol max # of threads
my $work_Q = new Thread::Queue;
## Start the kids
my @kids = map{ threads->create( \&kid, $_) } 1 .. $KIDS;
$work_Q->enqueue( keys %ServerInfo ); # Feed the QUEUE with work to do
+ ....
## Tell them to stop
$work_Q->enqueue( (undef) x $KIDS ); # THis tells each thread to QUIT
## And wait for Kids to finish..
my %results = map {$_->join} @kids;
#----------------------------------------------------
sub kid {
my $kidnumber = shift;
my @results=();
my $tid = threads->tid;
my $count=0;
my $pinger = Net::Ping->new("icmp",$TimeOut);
#printf "Kid: %02d started<br>\n", $tid;
while( my $work = $work_Q->dequeue ) {
$count ++;
push @results, $work, Ping_it($pinger, $work) ;
}
push @results,"KID$kidnumber",$count;
#print "kid: $tid ending after processing $count items.<br>\n";
return @results;
}
"As you get older three things happen. The first is your memory goes, and I can't remember the other two... "
- Sir Norman Wisdom
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|