OpenSwoole\Coroutine::isCanceled()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Coroutine::isCanceled(int $cid): bool

Parameters

None

Return

Returns true if the coroutine was successfully cancelled and false if there was a problem, use OpenSwoole\Util::getLastErrorCode() to see what went wrong.

Experimental feature, not recommended for production use

Description

Check if the current coroutine has been cancelled or not.

Since v4.7.0


Example

<?php
use OpenSwoole\Coroutine;
use OpenSwoole\Coroutine\System;

co::run(function()
{
    $chan = new Coroutine\Channel(1);

    $cid = Coroutine::getCid();
    go(function() use ($cid)
    {
        System::usleep(2000);
        assert(Coroutine::cancel($cid) === true);
    });

    assert($chan->push("Hello World [1]", 100) === true);
    assert(Coroutine::isCanceled() === false);
    assert($chan->errCode === OpenSwoole\Coroutine\Channel::CHANNEL_OK);

    assert($chan->push("Hello World [2]", 100) === false);
    assert(Coroutine::isCanceled() === true);
    assert($chan->errCode === OpenSwoole\Coroutine\Channel::CHANNEL_CANCELED);

    echo "Done\n";
});
Last updated on September 19, 2022