Swoole\Coroutine\Barrier

Latest version: pecl install openswoole-4.11.1

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 Swoole\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 March 31, 2022