Swoole\Lock::__construct

Latest version: pecl install openswoole-4.11.1

Declaration

<?php Swoole\\Lock::__construct ( [ int $type [, string $file_lock_location ] )

Parameters

type

The type of lock SWOOLE_FILELOCK, SWOOLE_RWLOCK, SWOOLE_SEM, SWOOLE_MUTEX, SWOOLE_SPINLOCK

file_lock_location

The locaiton of the lock file, for example: /tmp/file.lock

Return

Description

Create a lock for process sync.

Lock types:

SWOOLE_FILELOCKfile lock
SWOOLE_RWLOCKread write lock
SWOOLE_SEMLinux semaphore
SWOOLE_MUTEXMutex
SWOOLE_SPINLOCKspin lock

Example

<?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";
Last updated on March 31, 2022