Hello everyone,

I have constructed this piece of code to illustrate three problems and I would like to know: How would you write this code so that it works?

Problem 1: The join is located at a problematic position due to the nature of how hashes work. Sometimes the code takes 6 seconds to execute and sometimes 2 + 6 seconds.

Problem 2: L2_counter1 => 3 is not incremented

Problem 3: (Is actually a question) Why do I need to use a share? The data is incremented independently, so I would think there should be no problems regards synchronicity. However if I don't use the share, nothing gets incremented at all...

Here is the code:

use strict ; use warnings ; use MCE::Hobo ; use MCE::Shared ; use Data::Dumper ; sub task1 { print "Starting task 1 for $_[0]\n" ; sleep(2) ; print "Finished task 1 for $_[0]\n" ; } sub task2 { print "Starting task 2 for $_[0]\n" ; sleep(4) ; print "Finished task 2 for $_[0]\n" ; } sub task3 { print "Starting task 3 for $_[0]\n" ; sleep(6) ; print "Finished task 3 for $_[0]\n" ; } MCE::Hobo->init( max_workers => 2, # hobo_timeout => 10, # posix_exit => 1, ) ; my $mutex = MCE::Mutex->new; my $_test = { L1_counter1 => 1, # L1_counter2 => 2, # L1_counter3 => 3, nested1 => { L2_counter1 => 3, # L2_counter2 => 2, # L2_counter3 => 1, }, } ; my $test ; tie %{$test}, 'MCE::Shared', { module => 'MCE::Shared::Hash' }, %{$_te +st} ; print Dumper( $test ) ; sub executeTasks { my $in = $_[0] ; my $hobo ; foreach( keys %{$in} ) { if ( ref $in->{ $_ } eq 'HASH' ) { executeTasks( $in->{ $_ } ) ; } else { if ( $in->{ $_ } == 1 ) { $hobo = mce_async { task1( $_ ) ; ++$in->{ $_ } ; } ; } elsif ( $in->{ $_ } == 2 ) { $hobo = mce_async { task2( $_ ) ; ++$in->{ $_ } ; } ; } elsif ( $in->{ $_ } == 3 ) { $hobo = mce_async { task3( $_ ) ; ++$in->{ $_ } ; } ; } ; } ; } ; $hobo->join() ; } ; executeTasks( $test ) ; print "\n" ; print Dumper( $test ) ;

In reply to Hobo with a bit of recursion by Veltro

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.