Pool::__construct

(PECL pthreads >= 2.0.0)

Pool::__construct創(chuàng)建新的 Worker 對象池

說明

public Pool::__construct(int $size, string $class = ?, array $ctor = ?): Pool

創(chuàng)建新的 Worker 對象池,但是所對應的線程是延遲創(chuàng)建的的,也就是說, 直到需要執(zhí)行任務(wù)的時候 才會創(chuàng)建對應的線程。

參數(shù)

size

此 Pool 對象可創(chuàng)建的 Worker 對象的最大數(shù)量

class

新創(chuàng)建的 Worker 對象的類。 如果不指定類,那么會使用默認的 Worker 類。

ctor

創(chuàng)建 Worker 對象時所用到的參數(shù),以數(shù)組方式傳入

返回值

新創(chuàng)建的 Pool 對象

范例

示例 #1 創(chuàng)建 Pool 對象

<?php
class MyWorker extends Worker {
    
    public function 
__construct(Something $something) {
        
$this->something $something;
    }
    
    public function 
run() {
        
/** ... **/
    
}
}

$pool = new Pool(8, \MyWorker::class, [new Something()]);

var_dump($pool);
?>

以上例程會輸出:

object(Pool)#1 (6) {
  ["size":protected]=>
  int(8)
  ["class":protected]=>
  string(8) "MyWorker"
  ["workers":protected]=>
  NULL
  ["work":protected]=>
  NULL
  ["ctor":protected]=>
  array(1) {
    [0]=>
    object(Something)#2 (0) {
    }
  }
  ["last":protected]=>
  int(0)
}