php8.5
Home/ Manual/ pthreads / pool/ Pool::__construct

Pool::__construct

PHP function Edit on GitHub ✎

(PECL pthreads >= 2.0.0)

Creates a new Pool of Workers

Description

Pool::__construct(int $size, [string $class], [array $ctor])

Construct a new pool of workers. Pools lazily create their threads, which means new threads will only be spawned when they are required to execute tasks.

Parameters

size

The maximum number of workers for this pool to create

class

The class for new Workers. If no class is given, then it defaults to the Worker class.

ctor

An array of arguments to be passed to new workers' constructors

Examples

Creating Pools

php
<?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);
?>

The above example will output:

output
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)
}

Source: reference/pthreads/pool/construct.xml · from the official PHP manual (php/doc-en)