use strict ; use warnings ; use threads ; use Thread::Queue ; my @array = qw(a b c d e f g h); my @results; my $finished_queue = new Thread::Queue ; my $c = 2 ; # Number active at once my $n = 10 ; # Total number to dispatch my $m = $n ; # Number to collect while ($m) { while ($n && $c) { my $child = threads->create(\&sub_thread, @array) ; $n-- ; $c-- ; } ; my $tid = $finished_queue->dequeue() ; my $child = threads->object($tid) ; push @results, $child->join() ; $m-- ; $c++ ; } ; sub sub_thread { my (@temp) = (@_) ; my $tid = threads->tid() ; print "child $tid: @temp\n" ; sleep(rand(2)+2) ; $finished_queue->enqueue($tid) ; print "child $tid: finished\n" ; return "whatever from child $tid" ; } ;