my $running :shared = 0; ## This tracks the number of concurrent threads for my $machine ( @machines ) { async { { lock $running; ++$running; } ## incr on start open my $out, '>', "$machine.dat" or die $!; for my $cmd ( @commands ) { my $content = get "$machine/$cmd"; print $out $content; } close $out; { lock $running; --$running; } ## decr on finish }->detach; sleep 1 until $running < 10; ## sleep a bit if more than 10 are running } sleep 1 while $running; ## Make sure the main threads waits for the last few threads to finish