Please note, when using the collect function, it's important that you extend the pool class so you can keep checking for finished threads until they're all done.
<?php
class TestWork extends Threaded {
protected $complete;
public function __construct($pData){
$this->complete = false;
$this->testData = $pData;
}
public function run(){
usleep(2000000); $this->complete = true;
}
public function isGarbage() {
return $this->complete;
}
}
class ExamplePool extends Pool
{
public $data = array();
public function process()
{
while (count($this->work)) {
$this->collect(function (TestWork $task) {
if ($task->isGarbage()) {
$tmpObj = new stdclass();
$tmpObj->complete = $task->complete;
$this->data[] = $tmpObj;
}
return $task->isGarbage();
});
}
$this->shutdown();
return $this->data;
}
}
$pool = new ExamplePool(3);
$testData = 'asdf';
for($i=0;$i<5;$i++) {
$pool->submit(new TestWork($testData));
}
$retArr = $pool->process(); echo '<pre>';
print_r($retArr); echo '</pre>';
?>