MCE::Mutex(3)

NAME
MCE::Mutex - Locking for Many-Core Engine

VERSION
This document describes MCE::Mutex version 1.706

SYNOPSIS
   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 );
   };

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

API DOCUMENTATION

  MCE::Mutex->new ( void )
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.

  $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

      'value';
   });

INDEX
MCE, MCE::Core

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