Class
WaitGroup
Waits until a counted amount of work has been done, like Go's sync.WaitGroup.
Introduction#
Call add() before starting each piece of work, done() when it ends, and await() in the coroutine that waits. await() returns when the counter is zero, and at once if it already is. The counter may be raised again after it reached zero, so one group can be reused.
Class synopsis#
final class WaitGroup implements SelectableInterface {
/* Methods */
public WaitGroup::isReady(): bool
public WaitGroup::add(int $delta = 1): void
public WaitGroup::done(): void
public WaitGroup::await(float $timeout = PHP_FLOAT_MAX): void
public WaitGroup::wait(): void
}Methods#
| Method | Description |
|---|---|
isReady | Returns true when the counter is zero: nothing is left to wait for. |
add | Adds $delta to the counter of unfinished work. |
done | Marks one piece of work as done. |
await | Waits until the counter is zero. |
wait | Waits until the counter is zero. |
Examples#
phasync::run(function () {
$group = new phasync\Util\WaitGroup();
foreach ([0.02, 0.01] as $delay) {
$group->add();
phasync::go(function () use ($group, $delay) {
try {
phasync::sleep($delay);
} finally {
$group->done();
}
});
}
$group->await();
echo "both done\n";
});See also#
phasync::goStarts$fnas a coroutine in the current frame and scope, and returns it, without waiting for it.SelectableInterfaceAn object that can be asked whether using it would block, and waited for until it would not.