in reply to Why won't this Deadlock?

Hi Jambo Hamon,

Regarding Mutex (standalone module) and MCE::Mutex, multiple calls to mutex lock or enter by the same process or thread are safe from within dynamically nested scopes. The behavior is similar to locking using threads and threads::shared.

The hash may be shared. Below is the diff output.

$ diff orig.pl script.pl 33a34 > use MCE::Shared; 51c52 < my %t; --- > tie my %t, 'MCE::Shared'; 217c218 < print Dumper( \%t ); --- > print Dumper( tied(%t)->export( { unbless => 1 } ) );

The shared hash resides under the shared-manager process. Deep-sharing a non-blessed structure recursively is handled automatically via the TIE-shared interface. To see the key-value pairs, call the export method. The unbless option unblesses any shared array, hash, and scalar object to a non-blessed array, hash, and scalar respectively.

Regards, Mario

Replies are listed 'Best First'.
Re^2: Why won't this Deadlock?
by Jambo Hamon (Novice) on Jul 11, 2017 at 15:51 UTC
    Hi Mario,

    I'm going on vacation soon, so I'll print on the manuals to MCE::* and try to comment on how this research compares.

    DESCRIPTION A Hobo is a migratory worker inside the machine that carries the asynchronous gene. Hobos are equipped with "threads"-like capability for running code asynchronously. Unlike threads, each hobo is a unique process to the underlying OS. The IPC is managed by "MCE::Shared", which runs on all the major platforms including Cygwin.
    It seems you are way ahead of me in thinking about this. That's a cool venture (MCE::*); I see tonnes of applications for it.

    I appreciate the diff. Thank-you.

    Do you have terminology for the scope of data shared between processes?

    So PIE in the sky question is...

    How would I have a data shared for the lifetime of a block, and I want that block to run first, amongst all the processes that share it?

    { use MCE::Shared; my $cnt; #tie my $cnt, 'MCE::Shared', { module => 'MCE::Shared::Scalar' }, 0; tie my $va1, 'MCE::Shared', { module => 'MCE::Shared::Scalar' }, sub { + return $cnt++; }; #just for fun let's make a closure # How can I be sure that ($val->() == 0)? say ($val->()) ? 0 : 1; # says 1 if first caller }
    My Brain is saying that setting a mutex to protect the call to $val would be a recursive solution with no end? Eg. No atomic-ity, eg deadlock. Yeah, if it was synchronous it would work, but it's not.

    I said deadlock is on the horizon because the block could be wrapped in another block with a different mutex controlling the lower one. Got to run to an appointment. I'm going to be late. All the best, Jambo

      Hi Jambo Hamon,

      Enjoy your vacation. When you come back, I'd likely have MCE 1.830 (bug fixes) and MCE::Shared 1.827 released on CPAN. The OO interface for shared objects makes it possible to not worry about mutex at the application level.

      use MCE::Shared; my $val = MCE::Shared->scalar(0); $val->incr(); $val->incrby(20); $val->set(40); my @pairs = ('aa'..'zz'); my $oh = MCE::Shared->ordhash(); my $ha = MCE::Shared->hash(); $oh->assign( @pairs ); $ha->assign( @pairs ); $ha->set( counter => 0 ); $ha->incr('counter'); $ha->incrby('counter', 2); my $val = $ha->get('counter'); # Scoping works similarly to lexical scoping in Perl. # The shared object is destroyed upon leaving the scope. { my $ar = MCE::Shared->array(1..9); $ar->assign('aa'..'zz'); }

      Passing a code reference via IPC isn't supported at this time. Perhaps, I can add filter support for STORE (set), FETCH (get) at a later time. I'm currently at a code freeze with MCE 1.830 and MCE::Shared 1.827.

      use MCE::Shared; my $obj = MCE::Shared->scalar(0); $obj->filter_store_value( \&code ); $obj->filter_fetch_value( \&code );

      Regards, Mario