use threads; my $max_threads = 5; for( 1..20 ) { sleep(1) until scalar(threads->list) < $max_threads; threads->create( \&do_work ); } # Now wait for the remaining threads to finish: $_->join foreach threads->list;