Swoole\Coroutine\Barrier

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

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 August 31, 2022