opendir(hand,$ARGV[0]); #replace ARGV[0] with your DIR @files = readdir(hand); closedir(hand); foreach(@files){ if(/\.log$/i) { #if the filename has .log at the end push(@logfiles,$_); } } #### while (scalar @Threads < scalar @logfiles) { @running = threads->list(threads::running); if (scalar @running < $n_process) { #spawn threads if no of running threads less that MAX processes defined $filename = $logfiles[$fileindex]; my $thread = threads->new( sub { sorterwriter($filename) }); #spawning worker threads passing filename to subroutine push (@Threads, $thread); #pushing no of threads spawned for checking in outer loop my $tid = $thread->tid; print " - starting thread $tid\n"; $fileindex++; } } #### $nooffiles = @logfiles; $fileindex=0; while ($fileindex <$nooffiles ) { open hanr ,">", "$logfiles[$fileindex]" or die "could not open logfile .. $!"; $fileindex++; }