Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
Lock
can be used for data synchronization between multiple processes
.
Locks should not be used within coroutine context.
OpenSwoole\Lock->__construct
OpenSwoole\Lock->lock
OpenSwoole\Lock->lockwait
OpenSwoole\Lock->trylock
OpenSwoole\Lock->lock_read
OpenSwoole\Lock->trylock_read
OpenSwoole\Lock->unlock
Constant | Description |
---|---|
OpenSwoole\Constant::FILELOCK | file lock |
OpenSwoole\Constant::RWLOCK | read write lock |
OpenSwoole\Constant::SEM | Linux semaphore |
OpenSwoole\Constant::MUTEX | Mutex |
OpenSwoole\Constant::SPINLOCK | spin lock |
<?php
$lock = new OpenSwoole\Lock(OpenSwoole\Constant::MUTEX);
echo "[Master] Create lock\n";
$lock->lock();
if (pcntl_fork() > 0)
{
sleep(1);
$lock->unlock();
}
else
{
echo "[Child] Wait Lock\n";
$lock->lock();
echo "[Child] Get Lock\n";
$lock->unlock();
exit("[Child] exit\n");
}
echo "[Master]release lock\n";
unset($lock);
sleep(1);
echo "[Master]exit\n";