my $q = Thread::Queue->new(); # A new empty queue # Worker threads my @thr = map { threads->create(sub { while (my $item = $q->dequeue()) { # assuming undef isn't a valid value and so can be a marker. return unless defined $item; do_stuff($item, @_); }, $param1, $param2 )->detach(); } 1..$thread_limit; # Send work to the threads $q->enqueue($_) for @array_of_files; # send markers. $q->enqueue(undef) for 1..$thread_limit; # terminate. $_->join() for @thr;