php-ipc/examples/locks.php
Christopher Vagnetoft 176f9aa5ec More examples and tweaks
* Signal dispatch() method now uses own pid as default value
* SharedData now updates checksum cache on set()
2018-04-15 17:25:55 +02:00

44 lines
851 B
PHP

<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Ipc\Lock\FileLock;
// Creating the lock will not acquire it
$lock1 = new FileLock(__FILE__);
$lock2 = new FileLock(__FILE__);
// Acquire the lock like this
if ($lock1->acquire()) {
echo "lock1 acquired\n";
} else {
echo "lock1 not acquired\n";
}
// Test the locks like this
if ($lock1->isLocked()) {
echo "lock1 is locked\n";
} else {
echo "lock1 is not locked\n";
}
if ($lock2->isLocked()) {
echo "lock2 is locked\n";
} else {
echo "lock2 is not locked\n";
}
// This will timeout after a second
if ($lock2->acquire(1)) {
echo "lock2 acquired\n";
} else {
echo "lock2 not acquired\n";
}
// After releasing, you can acquire it
$lock1->release();
if ($lock2->acquire(1)) {
echo "lock2 acquired\n";
} else {
echo "lock2 not acquired\n";
}