in reply to Re^3: Threading with a twist
in thread Threading with a twist

Having extra thread only for print is kiss?

Replies are listed 'Best First'.
Re^5: Threading with a twist
by Random_Walk (Prior) on Oct 04, 2017 at 07:23 UTC

    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!