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.


In reply to Neat(er) idiom for closing Thread::Queues by Preceptor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.