Do it the easy way:

#! perl -slw use strict; use threads; use Thread::Queue; $| = 1; our $KIDS ||= 10; our $WORK ||= 1000; our $SLEEP||= 10; sub kid { my( $Q ) = shift; my $tid = threads->tid; printf "Kid: %d started\n", $tid; ## Pick a work item off the queue and process it while( my $work = $Q->dequeue ) { printf "Kid: %d processing work item '%s'\n", $tid, $work; ## Replace the sleep with the code to process the work items sleep rand( $SLEEP ); } printf "kid: %d ending\n", $tid; } ## A queue for communications my $Q = new Thread::Queue; ## Start the kids my @kids = map{ threads->create( \&kid, $Q ) } 1 .. $KIDS; ## Wait till they're all up and running sleep 1 until @{[ threads->list ]} == $KIDS; ## Feed the queue with work ## The limit just ensures we don't fill lots of memory for my $workitem ( 1 .. $WORK ) { sleep 1 while $Q->pending > $KIDS *2; print "Queueing work item $workitem"; $Q->enqueue( $workitem ); } ## Tell them to stop $Q->enqueue( (undef) x $KIDS ); ## And wait for them to do so. $_->join for @kids;

Throw out the comments & demo print statements and it is all of 20 lines of code. It could easily be reduced to 15.

A test run

C:\test>536937 -SLEEP=5 -WORK=20 Kid: 1 started Kid: 2 started Kid: 3 started Kid: 4 started Kid: 5 started Kid: 6 started Kid: 7 started Kid: 8 started Kid: 9 started Kid: 10 started Queueing work item 1 Kid: 1 processing work item '1' Queueing work item 2 Kid: 2 processing work item '2' Queueing work item 3 Kid: 3 processing work item '3' Queueing work item 4 Kid: 4 processing work item '4' Queueing work item 5 Kid: 5 processing work item '5' Queueing work item 6 Kid: 6 processing work item '6' Queueing work item 7 Kid: 7 processing work item '7' Queueing work item 8 Kid: 8 processing work item '8' Queueing work item 9 Kid: 9 processing work item '9' Queueing work item 10 Kid: 10 processing work item '10' Queueing work item 11 Kid: 2 processing work item '11' Queueing work item 12 Kid: 3 processing work item '12' Queueing work item 13 Kid: 6 processing work item '13' Queueing work item 14 Kid: 7 processing work item '14' Queueing work item 15 Kid: 8 processing work item '15' Queueing work item 16 Kid: 2 processing work item '16' Queueing work item 17 Kid: 8 processing work item '17' Queueing work item 18 Queueing work item 19 Queueing work item 20 Kid: 7 processing work item '18' Kid: 8 processing work item '19' Kid: 1 processing work item '20' kid: 1 ending kid: 5 ending kid: 9 ending kid: 2 ending kid: 7 ending kid: 4 ending kid: 6 ending kid: 10 ending kid: 3 ending kid: 8 ending

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.

In reply to Re: Tasking children from a single pipe by BrowserUk
in thread Tasking children from a single pipe by vancetech

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.