Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\\Lock::__construct ( [ int $type [, string $file_lock_location ] )
The type of lock SWOOLE_FILELOCK
, SWOOLE_RWLOCK
, SWOOLE_SEM
, SWOOLE_MUTEX
, SWOOLE_SPINLOCK
The locaiton of the lock file, for example: /tmp/file.lock
Create a lock for process sync.
SWOOLE_FILELOCK | file lock |
SWOOLE_RWLOCK | read write lock |
SWOOLE_SEM | Linux semaphore |
SWOOLE_MUTEX | Mutex |
SWOOLE_SPINLOCK | spin lock |
<?php
$lock = new Swoole\Lock(SWOOLE_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";