GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
MCE::Mutex(3) User Contributed Perl Documentation MCE::Mutex(3)

MCE::Mutex - Locking for Many-Core Engine

This document describes MCE::Mutex version 1.878

 use MCE::Mutex;

 my $mutex = MCE::Mutex->new;

 {
     use MCE::Flow max_workers => 4;

     mce_flow sub {
         $mutex->lock;

         # access shared resource
         my $wid = MCE->wid; MCE->say($wid); sleep 1;

         $mutex->unlock;
     };
 }

 {
     use MCE::Hobo;

     MCE::Hobo->create('work', $_) for 1..4;
     MCE::Hobo->waitall;
 }

 {
     use threads;

     threads->create('work', $_)   for 5..8;
     $_->join for ( threads->list );
 }

 sub work {
     my ($id) = @_;
     $mutex->lock;

     # access shared resource
     print $id, "\n";
     sleep 1;

     $mutex->unlock;
 }

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 (the default), unless "path" is given, is through a pipe or socket depending on the platform. The advantage of channel locking is not having to re-establish handles inside new processes and threads.

For Fcntl-based locking, it is the responsibility of the caller to remove the "tempfile", associated with the mutex, when path is given. Otherwise, it establishes a "tempfile" internally including removal on scope exit.

Returns the implementation used for the mutex.

 $m1 = MCE::Mutex->new( );
 $m1->impl();   # Channel

 $m2 = MCE::Mutex->new( path => /tmp/my.lock );
 $m2->impl();   # Flock

 $m3 = MCE::Mutex->new( impl => "Channel" );
 $m3->impl();   # Channel

 $m4 = MCE::Mutex->new( impl => "Flock" );
 $m4->impl();   # Flock

Current API available since 1.822.

Attempts to grab an exclusive 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.

The method "lock_exclusive" is an alias for "lock", available since 1.822.

 ( my $mutex = MCE::Mutex->new( path => $0 ) )->lock_exclusive;

Like "lock_exclusive", but attempts to grab a shared lock instead. The "lock_shared" method is an alias to "lock" otherwise for non-Fcntl implementations.

Current API available since 1.822.

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.

 my $val = $mutex->synchronize( sub {
     # access shared resource
     return 'scalar';
 });

 my @ret = $mutex->enter( sub {
     # access shared resource
     return @list;
 });

The method "enter" is an alias for "synchronize", available since 1.822.

Blocks until obtaining an exclusive lock. A false value is returned if the timeout is reached, and a true value otherwise. The default is 1 second when omitting timeout.

 my $mutex = MCE::Mutex->new( path => $0 );

 # terminate script if a previous instance is still running

 exit unless $mutex->timedwait( 2 );

 ...

Current API available since 1.822.

MCE, MCE::Core

Mario E. Roy, <marioeroy AT gmail DOT com>
2022-02-20 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.