$mutex->enter( $shared->set( $_ => $fetch->($_) ) );
1. the worker enters a mutex
meaning one worker runs solo while inside the mutex
2. then does a fetch on given URL
3. then stores the result into a shared hash
4. finally, leaves the mutex
####
mce_loop {
MCE->yield;
# run parallel
my $url = $_; my $result = $fetch->($url);
# run solo to store the result
$mutex->enter( $shared->set( $url => $result ) );
# am back to running parallel
# ...
}
##
##
mce_loop {
MCE->yield;
# run parallel, without a mutex
$shared->set( $_ => $fetch->($_) );
}
##
##
tie my %hash, 'MCE::Shared';
my $shared = MCE::Shared->hash();
my $mutex = MCE::Mutex->new();
$hash{number} = 0; # 1 trip, store
$shared->set( number => 0 ); # 1 trip
# 2 trips fetch and store, needs a mutex
$mutex->enter( $hash{number} += 2 );
# 1 trip via the OO interface
$shared->incrby( number => 2 );