in reply to Tasking children from a single pipe

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.