#! perl -slw use strict; use threads; use Thread::Queue; ## The worker sub sub worker { my $tid = threads->tid; my $Q = shift; while( my $workitem = $Q->dequeue ) { print "$tid: processing $workitem"; sleep $workitem; } } ### How many in the pool our $T //= 4; ### The queue my $Q = new Thread::Queue; ### Start the pool my @workers = map threads->create( \&worker, $Q ), 1 .. $T; ### Give'm some stuff to do $Q->enqueue( map int( rand 10 ), 1 .. 100 ); ### Do some other stuff here while they do their thing. ### Tell'm they're done $Q->enqueue( (undef) x $T ); ### Wait for'm to finish up $_->join for @workers; ### finished.