use strict;
use warnings;
use IPC::Shareable;
my $options = {
create => 1,
exclusive => 0,
mode => 0644,
destroy => 1,
};
tie my $cntr, 'IPC::Shareable', $options;
my $val;
$cntr = 0;
if ( ! defined ( my $pid = fork() ) ) {
die "Cannot fork!: $!";
}
elsif ( $pid == 0 ) {
# Child
for ( 1 .. 10000 ) {
( tied $cntr )->shlock;
$val = $cntr++;
( tied $cntr )->shunlock;
}
}
else {
# Parent
for ( 1 .. 10000 ) {
( tied $cntr )->shlock;
$val = $cntr++;
( tied $cntr )->shunlock;
}
waitpid $pid, 0;
print "counter: ", $cntr, "\n";
}
####
use strict;
use warnings;
use MCE::Mutex;
use MCE::Shared;
my $mutex = MCE::Mutex->new();
tie my $cntr, 'MCE::Shared', 0;
my $val;
if ( ! defined ( my $pid = fork() ) ) {
die "Cannot fork!: $!";
}
elsif ( $pid == 0 ) {
# Child
for ( 1 .. 10000 ) {
$mutex->lock;
$val = $cntr++;
$mutex->unlock;
}
}
else {
# Parent
for ( 1 .. 10000 ) {
$mutex->lock;
$val = $cntr++;
$mutex->unlock;
}
waitpid( $pid, 0 );
print "counter: ", $cntr, "\n";
}
####
use strict;
use warnings;
use MCE::Shared;
my $cntr = MCE::Shared->scalar( 0 );
my $val;
if ( ! defined ( my $pid = fork() ) ) {
die "Cannot fork!: $!";
}
elsif ( $pid == 0 ) {
# Child
for ( 1 .. 10000 ) {
$val = $cntr->incr;
}
}
else {
# Parent
for ( 1 .. 10000 ) {
$val = $cntr->incr;
}
waitpid( $pid, 0 );
print "counter: ", $cntr->get, "\n";
}
####
time in seconds : FreeBSD Linux Mac OSX
ipc_shareable.pl : 1.80 1.87 1.79
mce_shared.pl : 0.68 0.97 0.58
mce_shared_oo.pl : 0.38 0.73 0.41
counter: 20000
####
time in seconds : FreeBSD Linux Mac OSX
ipc_shareable.pl : 1.77 1.86 1.61
mce_shared.pl : 0.68 0.95 0.58
mce_shared_oo.pl : 0.19 0.17 0.16
counter: 20000