Change the line to $MAX_THREADS = 100;!? As this answer is more than trivial, what are you really asking ? Could you rephrase your question?
UPDATE: Got it. You want to know how to limit the total number of threads run in one session
First of all, if you post something here on perlmonks, use <code> and </code> to enclose your code, it is really hard to read without indentation
You might just change the thread creation loop to this:
my $LIFETIME_THREADS=100;
while (! $TERM) {
for (my $needed = $MAX_THREADS - threads->list();
$needed && ! $TERM;
$needed--) {
# New thread
threads->create('worker', $queue, $TIMEOUT) if ($LIFETIME_THREADS-
+- >0);
exit(0) if (threads->list()==0);
}
# Wait for any threads to finish
sleep(1);
}
2ND UPDATE: As AnonMonk observed, the loop runs through even after $LIFETIME_THREADS reaches 0, so $LIFETIME_THREADS gets negative. Testing for 0 isn't enough here. Corrected now. |