Swoole\Coroutine\Barrier

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

Methods

  • Swoole\Coroutine\Barrier::make
  • Swoole\Coroutine\Barrier::wait

    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::sleep(0.5);
                $count++;
            });
        }
        Barrier::wait($barrier);
    });