in reply to how to limit maximum threads in cpan example boss.pl

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.

Replies are listed 'Best First'.
Re^2: how to limit maximum threads in cpan example boss.pl
by Anonymous Monk on Aug 11, 2008 at 12:38 UTC
    I think you want
    if $LIFETIME_THREADS-- >0;
    because -1 is true :)
    C:\>perl -e die(666)if-1 666 at -e line 1. C:\>perl -e die(666)if-0 C:\>
      It's running perfectly now. I had only to add this line "$LIFETIME_THREADS-- >0;" also to the timeout section with the signal handler to be sure that the script will finish properly every time.

      Thanks for your help ...

      Franc
Re^2: how to limit maximum threads in cpan example boss.pl
by Anonymous Monk on Aug 11, 2008 at 12:21 UTC
    i think he wants exit once 100 threads have finished (10 max at one time)