We could reduce your question to: Why does using subroutines make code simple?

We are looking at two very different functions. It is logical to split them and keep two simple easy to debug blocks, rather than interleave them in a complex routine. Depending on the simplicity of the records going to <STDOUT> it may even be better to have the workers print directly. If the records are long and will not print as an atomic operation, or if there is post processing, then an publishing thread that gathers them and prints them keeps this work in one place.

Something like this looks very simple to me.

# some initialisation, queue building etc... my $shouty = threads->create('publish'); # This is the boss loop, dispatches to workers while (<>) { chomp; # I guess $work->enqueue $_; ) sub publish { while (my $out = $result->dequeue) { print "I got: $out\n"; }

Do you have an example where you read <STDIN> and the $result queue in the same code block, that is simpler?

Now if we need to add pre or post processing then it sits nicely in the dispatcher or publisher routine without being all mixed up.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re^5: Threading with a twist by Random_Walk
in thread Threading with a twist by scorpion7

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.