Swoole locks enable PHP developers use locks for data synchronization between multiple theads or processes.
Lock types:
- SWOOLE_FILELOCK: file lock
- SWOOLE_RWLOCK: read write lock
- SWOOLE_SEM: Linux semaphore
- SWOOLE_MUTEX: Mutex
- SWOOLE_SPINLOCK: spin lock
Construct a lock.
Destory a lock.
Try to acquire the lock. It will block if the lock is not available.
Try to acquire the lock and waiting the lock for a duration before giving up.
Try to acquire the lock and return straight away even the lock is not available.
Lock a read-write lock for reading.
Try to lock a read-write lock for reading and return straight away even the lock is not available.
Release the 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";