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.
|