Preceptor has asked for the wisdom of the Perl Monks concerning the following question:

I've been doing more and more threading recently - simple, but implicitly parallel tasks, like bulk statistics collection and graphing on servers.

Whilst I initially did a 'one thread per server' type of approach, I'm finding that doesn't scale as well as I'd like - a few hundred is not a problem, but a few thousand and it just doesn't work.

Thankfully, Thread::Queue and a set of 'worker' threads works quite nicely. Queue your 'server list', spawn N threads, and have them process the queue. Thus I tend to use something like:

sub worker_thread { while ( my $item = $processing_q -> dequeue() ) { # do stuff to $item } } for ( my $count = 0; $count < $nthreads; $count++ ) { my $thr = threads -> create ( \&worker_thread ); } $processing_q -> enqueue ( @server_list ); $processing_q -> end; foreach my $thread ( threads -> list() ) { $thread -> join(); }

This works quite nicely, but recently I've found I've needed to do two different 'sets' of worker threads - one to 'go and collect', and another set to process (and maybe some 'helper' threads, such as one to serialise data/logging output). I've been using queues again, and having the 'workers' feed the queue. However this is where I'm starting to have a bit more difficulty.

Does anyone have a neat idiom for 'handling' sub threads?

Currently, I've tried a variety of approaches:

I was wondering though - does anyone have a more elegant solution? I like how neat the 'while' loop is for queue processing.