PHP code example of geoffroy-aubry / mutex

1. Go to this page and download the library: Download geoffroy-aubry/mutex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

geoffroy-aubry / mutex example snippets


$oLogger = new MinimalLogger();
$oMutex = new Mutex($oLogger, 100, '/tmp/demo-lock');

$oMutex->acquire();
echo "Do anything for 3 seconds…\n";
sleep(3);
$oMutex->release();

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…

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

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…
bash
$ curl -sS https://getcomposer.org/installer | php