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
<?php OpenSwoole\\Lock::__construct ( [ int $type [, string $file_lock_location ] )
The type of lock OpenSwoole\Constant::FILELOCK
, OpenSwoole\Constant::RWLOCK
, OpenSwoole\Constant::SEM
, OpenSwoole\Constant::MUTEX
, OpenSwoole\Constant::SPINLOCK
The locaiton of the lock file, for example: /tmp/file.lock
Create a lock for process sync.
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";