Join 4,000+ others and never miss out on new tips, tutorials, and more.
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
<?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);
});