in reply to Re^5: adding a hash to a shared object
in thread adding a hash to a shared object

Thank you Corion. I am trying to understand thies pieces of code but it seems the program does not return (it does all the work but does not terminate).

Replies are listed 'Best First'.
Re^7: adding a hash to a shared object
by Corion (Patriarch) on Aug 11, 2010 at 13:03 UTC

    Ouch! Indeed, it should be that the first thread does terminate, but it never tells its next thread to terminate. So you need to add a $q2->enqueue(undef); to the end of the first thread, and do the same for the third part as well.

      Thank you. I can see how this allows for the correct scheduling of the tasks, but what I'm still missing is how do I deal with data that each subroutine generates and I want to store somewhere outside, not just pass to the next function.

      For example, some subroutines create small hashes which I want to add up at the end of the program and write as a JSON file.

      Also, I should note my use of your scheme will be probably very degenerate since any subroutine is only called once (i.e. a single payload...).

        If your subroutines are only ever called once, what gains do you hope to get from parallelizing them?

        My approach would be to not store any data but to forward it to whatever subroutine. If you want to do some processing after a subroutine has consumed all data passed to it, do it just there:

        sub accumulate { async { my %totals; while (defined (my $payload = $q2->dequeue())) { $totals{ $payload }++; }; # Processing has finished print_to_json(\%totals); }; };