(PECL pthreads >= 3.0.0)
Worker::collect — 收集已完成任务的引用
允许工作线程收集由可选的收集器确定为垃圾的引用。只有在极少数情况下才需要使用自定义收集器。
collector
一个Callable收集器,返回一个布尔值,表示任务是否可以被收集。只有在极少数情况下才需要使用自定义收集器。
工作线程堆栈上剩余待收集的任务数量。
示例 #1 Worker::collect() 的基本示例
<?php
$worker = new Worker();
echo "当前堆栈上有 {$worker->collect()} 个任务待收集\n";
for ($i = 0; $i < 15; ++$i) {
$worker->stack(new class extends Threaded {});
}
echo "堆栈上还有 {$worker->collect()} 个任务待收集\n";
$worker->start();
while ($worker->collect()); // 阻塞直到所有任务执行完毕
echo "现在堆栈上有 {$worker->collect()} 个任务待收集\n";
$worker->shutdown();
以上示例将输出
There are currently 0 tasks on the stack to be collected There are 15 tasks remaining on the stack to be collected There are now 0 tasks on the stack to be collected