php-ipc/examples/locks.php

44 lines
851 B
PHP
Raw Normal View History

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