my $i=0; while (@dirs_to_ftp) { my $dir= shift @dirs_to_ftp; startthread($dir); $i++; if ($i==2) { $i= $i- wait_for_thread_finish(); $i--; } } while($i) { $i= $i- wait_for_thread_finish(); $i--; }