The Swoole\Coroutine\Lock class

(No version information available, might only be in Git)

简介

Swoole 6.0.1 introduced a coroutine lock that supports inter-process and inter-thread sharing. This lock is designed with non-blocking behavior and enables efficient coroutine synchronization in multi-process and multi-thread environments.

When compiled with the --enable-iouring option and the Linux kernel supports the io_uring futex feature, Swoole's coroutine lock implements synchronization using io_uring futex. In this case, coroutines wait for lock wakeups using an efficient queuing mechanism, significantly improving performance.

Without io_uring futex, the coroutine lock falls back to an exponential backoff sleep mechanism, where the wait time increases by 2^n milliseconds (n being the number of failures) after each failed attempt to acquire the lock. While this approach avoids busy waiting, it introduces additional CPU scheduling overhead and latency.

The coroutine lock is reentrant, allowing the currently holding coroutine to safely perform multiple lock operations.

警告

Do not create locks in callback functions like onReceive, as this will cause continuous memory growth and lead to memory leaks.

警告

Locking and unlocking must be performed in the same coroutine, otherwise it will break static conditions.

类摘要

class Swoole\Coroutine\Lock {
/* 方法 */
public __construct(): void
public lock(): bool
public trylock(): bool
public unlock(): bool
}

示例

示例 #1 Basic usage

<?php
use Swoole\Coroutine\Lock;
use
Swoole\Coroutine\WaitGroup;
use function
Swoole\Coroutine\go;
use function
Swoole\Coroutine\run;

$lock = new Lock();
$waitGroup = new WaitGroup();

run(function() use ($lock, $waitGroup) {
go(function() use ($lock, $waitGroup) {
$waitGroup->add();
$lock->lock();
sleep(1);
$lock->unlock();
$waitGroup->done();
});

go(function() use ($lock, $waitGroup) {
$waitGroup->add();
$lock->lock(); // Wait for the holding coroutine to unlock
sleep(1);
$lock->unlock();
$waitGroup->done();
});

echo
'Lock does not block the process';
$waitGroup->wait();
});

目录

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top