use strict; use warnings; use threads; use Thread::Queue; my $wq = Thread::Queue->new(); # Build a queue where you put work my $thread_limit = 4; # how many threads do you want? my @thr = map { # create threads and store the handles in @thr threads->create( sub { while (my $item = $wq->dequeue()) { # finish when undef # Do something with item } } ); } 1..$thread_limit; while (I get $data to process) { $wq->enqueue($data); } # Tell the threads we are done $wq->enqueue(undef) for @thr; # Wait for all the threads to finish $_->join() for @thr;