PHP code example of alesinicio / mutex

1. Go to this page and download the library: Download alesinicio/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/ */

    

alesinicio / mutex example snippets




//PROCESS #1
$cache = new MyCache();
$mutex = new Mutex($cache);
$event = 'MyEvent';

$mutex->lock(event: $event);
sleep(5);
$mutex->unlock(event: $event);

------------

//PROCESS #2
$cache = new MyCache();
$mutex = new Mutex($cache);
$event = 'MyEvent';

while ($mutex->isLocked(event: $event)) {
	echo "Mutex in place... Waiting\n";
	sleep(1);
}
echo "Mutex not in place... Proceeding...\n";



//PROCESS #1
$cache = new MyCache();
$mutex = new Mutex($cache);
$event = 'MyEvent';

$lockID = $mutex->lock(event: $event, timeout: 5);
sleep(10);
$mutex->unlock($event, $lockID);

------------

//PROCESS #2
$cache = new MyCache();
$mutex = new Mutex($cache);
$event = 'MyEvent';

while ($mutex->isLocked($event)) {
	echo "Mutex in place... Waiting\n";
	sleep(1);
}
echo "Mutex not in place... Proceeding...\n";



$cache = new MyCache();
$mutex = new Mutex($cache);
$event = 'MyEvent';

try {
	$mutex->waitUntilUnlocked(event: $event, maxWait: 5, preDelay: 1, checkPeriod: 2_000_000);
} catch (MutexTimeoutException) {
	echo "Mutex did not unlock...\n";
}