MCE::Mutex(3) Simple semaphore for Many-Core Engine


This document describes MCE::Mutex version 1.608


use MCE::Flow max_workers => 4;
use MCE::Mutex;
print "## running a\n";
my $a = MCE::Mutex->new;
mce_flow sub {
## access shared resource
my $wid = MCE->wid; MCE->say($wid); sleep 1;
print "## running b\n";
my $b = MCE::Mutex->new;
mce_flow sub {
$b->synchronize( sub {
## access shared resource
my ($wid) = @_; MCE->say($wid); sleep 1;
}, MCE->wid );


This module implements locking methods that can be used to coordinate access to shared data from multiple workers spawned as processes or threads.

The inspiration for this module came from reading Mutex for Ruby.


MCE::Mutex->new ( void )

Creates a new mutex.

$m->lock ( void )

Attempts to grab the lock and waits if not available. Multiple calls to mutex->lock by the same process or thread is safe. The mutex will remain locked until mutex->unlock is called.

$m->unlock ( void )

Releases the lock. A held lock by an exiting process or thread is released automatically.

$m->synchronize ( sub { ... }, @_ )

Obtains a lock, runs the code block, and releases the lock after the block completes. Optionally, the method is wantarray aware.

   my $value = $m->synchronize( sub {
      ## access shared resource




Mario E. Roy, <marioeroy AT gmail DOT com>