OpenSwoole\Atomic->wait

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Atomic->wait ( float $timeout = 1.0 )

Parameters

timeout

Wait on the counter for $timeout if no wakeup from other processes.

Return

Description

The Atomic can be used as a Lock for syncing between mutliple processes.

Put the process into wait mode if the value of OpenSwoole Atomic is 0. The other process can wakeup the processes with wait status.

Wait on the counter for $timeout ms if no wakeup from other processes.

Example

<?php
$lock = new OpenSwoole\Atomic;
if (pcntl_fork() > 0) {
    echo "master start\n";
    $lock->wait(1.5);
    echo "master end\n";
} else {
    echo "child start\n";
    sleep(1);
    $lock->wakeup();
    echo "child end\n";
}
Last updated on September 1, 2022