©
                    本文档使用
                    php中文网手册 发布
                
(PECL pthreads >= 2.0.0)
Pool::collect — 回收已完成任务的引用
$collector 
   )对于视为垃圾的引用,使用给定的垃圾收集器进行收集
collector 可调用的垃圾收集器
void
Example #1 创建 Pool 对象
  <?php
 class  MyWork  extends  Stackable  {
    public function  __construct () {
         $this -> complete  =  false ;
    }
    public function  run () {
         printf (
             "Hello from %s in Thread #%lu\n" , 
             __CLASS__ ,  $this -> worker -> getThreadId ());
         $this -> complete  =  true ;
    }
    public function  isComplete () { 
        return  $this -> complete ; 
    }
    protected  $complete ;
}
class  MyWorker  extends  Worker  {
    
    public function  __construct ( Something $something ) {
         $this -> something  =  $something ;
    }
    
    public function  run () {
         
     }
}
 $pool  = new  Pool ( 8 , \ MyWorker ::class, [new  Something ()]);
 $pool -> submit (new  MyWork ());
 usleep ( 1000 );
 $pool -> collect (function( $work ){
    return  $work -> isComplete ();
});
 var_dump ( $pool );
 ?>   以上例程会输出:
Hello from MyWork in Thread #140222468777728
object(Pool)#1 (6) {
  ["size":protected]=>
  int(8)
  ["class":protected]=>
  string(8) "MyWorker"
  ["workers":protected]=>
  array(1) {
    [0]=>
    object(MyWorker)#4 (1) {
      ["something"]=>
      object(Something)#5 (0) {
      }
    }
  }
  ["work":protected]=>
  array(0) {
  }
  ["ctor":protected]=>
  array(1) {
    [0]=>
    object(Something)#2 (0) {
    }
  }
  ["last":protected]=>
  int(1)
}
 [#1] your dot brother dot t at hotmail dot com [2014-12-29 09:33:16]
The example code crashes and made me waste 2 working days
First of all, `Stackable` has no attribute named $worker or it's access method made it inaccessible.
Secondly, `Stackable` also doesn't have `getThreadId()` . It's best practice to use `Thread` class for realization of a thread since it has more control functions. It's better to use `Stackable` for object storage and use it's `run()` as its initialization.
The working example is
<?php
    class MyWork extends Thread {
        protected $complete;
        public function __construct() {
            $this->complete = false;
        }
        public function run() {
            printf(
                "Hello from %s in Thread #%lu\n",
                __CLASS__, $this->getThreadId());
            $this->complete = true;
        }
        public function isComplete() {
            return $this->complete;
        }
    }
    class Something {}
    class MyWorker extends Worker {
        public function __construct(Something $something) {
            $this->something = $something;
        }
        public function run() {
            
        }
    }
    $pool = new Pool(8, \MyWorker::class, [new Something()]);
    $pool->submit(new MyWork());
    usleep(1000);
    $pool->collect(function($work){
        return $work->isComplete();
    });
    var_dump($pool);
?>