Movatterモバイル変換


[0]ホーム

URL:


 / 
MCE-1.901
River stage three • 22 direct dependents • 287 total dependents
/MCE::Mutex

NAME

MCE::Mutex - Locking for Many-Core Engine

VERSION

This document describes MCE::Mutex version 1.901

SYNOPSIS

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

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 ( )

MCE::Mutex->new ( impl => "Channel" )

MCE::Mutex->new ( impl => "Flock", [ path => "/tmp/file.lock" ] )

MCE::Mutex->new ( path => "/tmp/file.lock" )

Creates a new mutex.

Channel locking (the default), unlesspath 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 thetempfile, associated with the mutex, when path is given. Otherwise, it establishes atempfile internally including removal on scope exit.

$mutex->impl ( void )

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.

$mutex->lock ( void )

$mutex->lock_exclusive ( void )

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 methodlock_exclusive is an alias forlock, available since 1.822.

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

$mutex->lock_shared ( void )

Likelock_exclusive, but attempts to grab a shared lock instead. Thelock_shared method is an alias tolock otherwise for non-Fcntl implementations.

Current API available since 1.822.

$guard = $mutex->guard_lock ( void )

This method callslock and returns a guard object. When the guard object is destroyed, it automatically callsunlock.

Current API available since 1.889.

$mutex->unlock ( void )

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

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

$mutex->enter ( sub { ... }, @_ )

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

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

The methodenter is an alias forsynchronize, available since 1.822.

$mutex->timedwait ( floating_seconds )

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.

my $mutex = MCE::Mutex->new( path => $0 );# terminate script if a previous instance is still runningexit unless $mutex->timedwait( 2 );...

Current API available since 1.822.

INDEX

MCE,MCE::Core

AUTHOR

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

Module Install Instructions

To install MCE, copy and paste the appropriate command in to your terminal.

cpanm

cpanm MCE

CPAN shell

perl -MCPAN -e shellinstall MCE

For more information on module installation, please visitthe detailed CPAN module installation guide.

Keyboard Shortcuts

Global
sFocus search bar
?Bring up this help dialog
GitHub
gpGo to pull requests
gigo to github issues (only if github is preferred repository)
POD
gaGo to author
gcGo to changes
giGo to issues
gdGo to dist
grGo to repository/SCM
gsGo to source
gbGo to file browse
Search terms
module: (e.g.module:Plugin)
distribution: (e.g.distribution:Dancer auth)
author: (e.g.author:SONGMU Redis)
version: (e.g.version:1.00)

[8]ページ先頭

©2009-2025 Movatter.jp