geoffroy-aubry / mutex
Mutex & Semaphore PHP implementations both based only on flock() function.
Requires
- php: >=5.3.3
- geoffroy-aubry/helpers: ~1.7
- psr/log: 1.0.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-10-30 10:59:41 UTC
README
Mutex & Semaphore PHP implementations both based only on flock() function.
Demo
Mutex
File: demo-mutex.php.
Example:
$oLogger = new MinimalLogger(); $oMutex = new Mutex($oLogger, 100, '/tmp/demo-lock'); $oMutex->acquire(); echo "Do anything for 3 seconds…\n"; sleep(3); $oMutex->release();
⇒ RESULT for 2 processes P1 and P2:
P1 $ php examples/demo-mutex.php Do anything for 3 seconds… P2 $ php examples/demo-mutex.php Waiting to acquire Mutex lock on /tmp/demo-lock… Mutex lock acquired after 2.57s Do anything for 3 seconds…
Semaphore
File: demo-semaphore.php.
Example, semaphore with 2 units of a resource /tmp/demo-sem
:
$oLogger = new MinimalLogger(); $oSem = new Semaphore($oLogger, 2, 100, '/tmp/demo-sem'); $oSem->acquire(); echo "Do anything for 3 seconds…\n"; sleep(3); $oSem->release();
⇒ RESULT for 3 processes P1, P2 and P3:
P1 $ php examples/demo-semaphore.php Do anything for 3 seconds… P2 $ php examples/demo-semaphore.php Do anything for 3 seconds… P3 $ php examples/demo-semaphore.php Waiting to acquire lock on /tmp/demo-sem… Lock acquired after 2.30s Do anything for 3 seconds…
Usage
Mutex is available via Packagist.
- Class autoloading and dependencies are managed by Composer so install it following the instructions on Composer: Installation - *nix or just run the following command:
$ curl -sS https://getcomposer.org/installer | php
- Add dependency to
GAubry\Mutex
into require section of yourcomposer.json
:
{ "require": { "geoffroy-aubry/mutex": "1.*" } }
and run php composer.phar install
from the terminal into the root folder of your project.
- Include Composer's autoloader and use the
GAubry\Mutex
classes.
Copyrights & licensing
Licensed under the GNU Lesser General Public License v3 (LGPL version 3). See LICENSE file for details.
Change log
See CHANGELOG file for details.
Git branching model
The git branching model used for development is the one described and assisted by twgit
tool: https://github.com/Twenga/twgit.