my @waiting = @chromosomes; my %threads; my $max_threads = 10; while ( scalar(@waiting) || scalar( keys(%threads) ) ) { if ( scalar( keys(%threads) ) < $max_threads ) { my $chr = pop(@waiting); $threads{$chr} = threads->new(\&threading_function, $chr); } else { foreach my $chr ( keys(%threads) ) { if ( $threads{$chr}->is_joinable() ) { $threads{$chr}->join(); delete $threads{$chr}; } } } } #### my $limit = 10; my $processes = 0; my $processed = scalar(@chromosomes); my @threads; while ($processed) { if ( scalar(@threads) < $limit ) { push @threads, threads->new( \&thread, $_ ); ++$processes; } else { foreach my $thread (@threads) { if ( $thread->is_joinable() ) { $thread->join(); --$processes; } } } }