MCE::Mutex - Locking for Many-Core Engine
This document describes MCE::Mutex version 1.806
use MCE::Flow max_workers => 4; use MCE::Mutex; print "## running a\n"; my $a = MCE::Mutex->new; mce_flow sub { $a->lock; ## access shared resource my $wid = MCE->wid; MCE->say($wid); sleep 1; $a->unlock; }; 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.
Creates a new mutex.
Channel locking is through a pipe or socket depending on platform. The advantage of channel locking is not having to re-establish handles inside new processes or threads.
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.
Releases the lock. A held lock by an exiting process or thread is released automatically.
Obtains a lock, runs the code block, and releases the lock after the block completes. Optionally, the method is wantarray aware.
wantarray
my $value = $m->synchronize( sub { ## access shared resource 'value'; });
MCE, MCE::Core
Mario E. Roy, <marioeroy AT gmail DOT com>
To install MCE, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MCE
CPAN shell
perl -MCPAN -e shell install MCE
For more information on module installation, please visit the detailed CPAN module installation guide.