I have a program (Perl) that launches a huge number of threads (each of which is responsible for creating graphics based on data processing). Every thread that I start using:
my @threads //list to store threads that have been launched
push @threads,threads->create(\mySubName,params...);
Streams work correctly, but some time after I opened several of them, Perl-interpreter errors (I suppose, is this related to memory?). So my solution is to limit the number of threads that I open at a time, I chose 15. And I want to add a sub in front of each line to check whether it is normal to start the next thread or to sleep, while I wait for to finish. This is how I tried to do this.
sub checkThreads{
my $addThread = 0;
until($addThread){
my $totalThreads = 0;
foreach my $task (@threads){
if($task->is_running()){$totalThreads++;}
}
if($totalThreads <= 15 ){
print "Ok to add new thread, carry on!\n";
$addthread = 1;
}else{
print "Waiting for $totalThreads threads to fire next one...\n";
sleep 2;
}
}
}
So every time I want to create a new thread, I just call
&checkThreads;
, , .
, sub, , , :
$task->is_running()
- . , , .
?
, , :
scalar(threads->list());
, , , :
threads=SCALAR(0x80fea8c)