in reply to Controlling Thread Numbers
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Controlling Thread Numbers
by BBQ (Curate) on Oct 19, 2007 at 02:01 UTC | |
by NetWallah (Canon) on Oct 21, 2007 at 20:44 UTC |
In Section
Seekers of Perl Wisdom