MCE::Shared provides a single point of entry for commands sent to the shared manager process no matter the number of workers. This allows zero locking at the application level. The multiple data channels behind MCE::Shared is for reducing IPC latency. With that in mind, accessing the shared data is better done via the OO methods. The TIE interface may require locking and possible with MCE::Mutex.

OO interface.

use strict; use warnings; use MCE::Shared; my $data = MCE::Shared->array(); if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child MCE::Shared->init(); my $item; sleep 1; for ( my $i = 1; $i <= 10; $i++ ) { $item = $data->shift(); $item = '' if ( ! defined $item ); print "Child: $item\n"; } } else { # Parent for ( my $i = 1; $i <= 10; $i++ ) { $data->push($i); print "Parent: ", join(" ", $data->vals()), "\n"; } waitpid( $pid, 0 ); }

OO interface with locking.

use strict; use warnings; use MCE::Mutex; use MCE::Shared; my $mutex = MCE::Mutex->new(); my $data = MCE::Shared->array(); if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child MCE::Shared->init(); my $item; sleep 1; for ( my $i = 1; $i <= 10; $i++ ) { $mutex->lock(); $item = $data->shift(); $mutex->unlock(); $item = '' if ( ! defined $item ); print "Child: $item\n"; } } else { # Parent for ( my $i = 1; $i <= 10; $i++ ) { $mutex->lock(); $data->push($i); $mutex->unlock(); print "Parent: ", join(" ", $data->vals()), "\n"; } waitpid( $pid, 0 ); }

OO interface (de-referencing) with locking.

use strict; use warnings; use MCE::Shared; my $data = MCE::Shared->array(); if ( ! defined ( my $pid = fork() ) ) { die "Cannot fork!: $!"; } elsif ( $pid == 0 ) { # Child MCE::Shared->init(); my $item; sleep 1; for ( my $i = 1; $i <= 10; $i++ ) { $item = shift @$data; $item = '' if ( ! defined $item ); print "Child: $item\n"; } } else { # Parent for ( my $i = 1; $i <= 10; $i++ ) { push @$data, $i; print "Parent: @$data\n"; } waitpid( $pid, 0 ); }

Summary.

Locking may be omitted at the application level for the OO interface including de-referencing unless wanting to call multiple OO methods. Furthermore, MCE::Shared::{ Array, Hash, Ordhash, and Scalar } provide sugar methods resembling the Redis API without having to call set and get explicitly.

# Locking is necessary via the TIE interface. # Below, set and get are called separately behind the scene ( 2 IPC st +atements ). my $m1 = MCE::Mutex->new(); tie my @a1, 'MCE::Shared'; $m1->lock; my $v0 = $a1[0] = "item0"; $m1->unlock; # Locking is optional at the application level when using the OO inter +face. # This is made possible by the single point of entry. my $a2 = MCE::Shared->array(); my $v2 = $a2->set(0, "item2"); my $a3 = MCE::Shared->array(); my $v3 = $a3->[0] = "item3";

Regards, Mario.


In reply to Re^4: IPC:Shareable: Not an array reference by marioroy
in thread IPC:Shareable: Not an array reference by Bloehdian

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.