Swoole 中使用 Lock 实现进程间锁
注意:不要在 lock
和 unlock
操作中间使用可能引起协程切换的 API
。
$lock = new SwooleLock(SWOOLE_MUTEX);
echo "[Master]create lock
";
$lock->lock();
if (pcntl_fork() > 0)
{
sleep(1);
$lock->unlock();
} else {
echo "[Child] Wait Lock
";
$lock->lock();
echo "[Child] Get Lock
";
$lock->unlock();
exit("[Child] exit
");
}
echo "[Master]release lock
";
unset($lock);
sleep(1);
echo "[Master]exit
";