OpenSwoole\Coroutine\Barrier

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

We can use Barrier to sync multiple coroutines, wait for multiple coroutines to be finished.

You have to pass barrier into the coroutines.

Since v4.5.3 Removed since v4.9.1

Methods

Example

<?php
declare(strict_types=1);

use OpenSwoole\Coroutine\Barrier;

co::run(function() {
    $barrier = Barrier::make();
    $count = 0;
    $N = 4;
    foreach (range(1, $N) as $i) {
        \Swoole\Coroutine::create(function () use ($barrier, &$count) {
            System::usleep(500000);
            $count++;
        });
    }
    Barrier::wait($barrier);
});
Last updated on September 1, 2022