in reply to Building a thread pool

This isn't what you want, but it is what you have asked for. And it is rather less cumbersome than Thread::Pool:

#! perl -slw use strict; package pool; use threads; use Thread::Queue; sub new{ my $class = shift; my $nThreads = shift; my $Qin = new Thread::Queue; my $Qout = new Thread::Queue; my @threads = map{ threads->new( sub { my( $Qin, $Qout ) = @_; while( my $code = $Qin->dequeue() ) { $Qout->enqueue( eval $code ); } }, $Qin, $Qout ) } 1 .. $nThreads; return bless [ $Qin, $Qout, \@threads ], $class } sub job { my( $Qin, $Qout, $threads ) = @{ +shift }; $Qin->enqueue( shift ); return; } sub DESTROY { my( $Qin, $Qout, $threads ) = @{ +shift }; $Qin->enqueue( undef ) for @$threads; sleep 0; $_->join for @$threads; } *stop = *stop = *DESTROY; package main; my $pool = pool->new( 5); for ( 1 .. 1000 ) { $pool->job( qq[ sleep 5; printf "In thread(%d). arg:%d\n", threads->tid, $_ ] ); } sleep 10; $pool->stop; __END__ c:\test>543977 In thread(1). arg:1 In thread(2). arg:2 In thread(3). arg:3 In thread(4). arg:4 In thread(5). arg:5 In thread(1). arg:6 In thread(2). arg:7 In thread(3). arg:8 In thread(4). arg:9 In thread(5). arg:10 In thread(1). arg:11 In thread(2). arg:12 In thread(3). arg:13 In thread(4). arg:14 In thread(5). arg:15 In thread(4). arg:16 In thread(3). arg:17

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.