ComponentsThreadPool::isClosed()

ThreadPool::isClosed()

(PHP 8.6+, True Async 1.0)

php
public ThreadPool::isClosed(): bool

Returns true if the pool has been shut down via close() or cancel(). Returns false while the pool is still accepting tasks.

Return Value

booltrue if the pool is closed; false if it is still active.

Examples

Example #1 Checking state before submitting

php
<?php

use Async\ThreadPool;
use function Async\spawn;
use function Async\await;

spawn(function() {
    $pool = new ThreadPool(workers: 2);

    $future = $pool->submit(fn() => 'done');

    var_dump($pool->isClosed()); // bool(false)

    $pool->close();

    var_dump($pool->isClosed()); // bool(true)

    echo await($future), "\n"; // done
});

Example #2 Guarding submit in shared contexts

php
<?php

use Async\ThreadPool;
use function Async\spawn;
use function Async\await;

function trySubmit(ThreadPool $pool, callable $task): mixed
{
    if ($pool->isClosed()) {
        return null;
    }
    return await($pool->submit($task));
}

spawn(function() {
    $pool = new ThreadPool(workers: 2);
    echo trySubmit($pool, fn() => 'hello'), "\n"; // hello
    $pool->close();
    var_dump(trySubmit($pool, fn() => 'missed')); // NULL
});

See Also