I realize that this is an old thread but I ran across this trying to solve a similar problem: run a specific number of threads to work on a set of issues. I thought it might be useful for others to know the correct implementation of Tanktalus' suggestion.
Basically, there were two issues that took me a while to sort out: firstly, the newly created threads are detached, which makes them impossible to join later; secondly (much less important), adding undef as a queue item isn't necessary when using dequeue_nb instead of dequeue.
The code that actually works for me looks like this:
#! /usr/bin/perl
use threads;
use Thread::Queue;
my $q = Thread::Queue->new(); # A new empty queue
# Send work to the threads
$q->enqueue($_) for @ARGV;
# Worker threads
my $thread_limit = 8;
my @thr = map {
threads->create(sub {
while (defined (my $item = $q->dequeue_nb())) {
doStuff($item);
}
});
} 1..$thread_limit;
# terminate.
$_->join() for @thr;