class Pool (View source)

(PECL pthreads >= 2.0.0)
A Pool is a container for, and controller of, an adjustable number of Workers.
Pooling provides a higher level abstraction of the Worker functionality, including the management of references in the way required by pthreads.

Properties

protected int $size

Maximum number of Workers this Pool can use

protected string $class

The class of the Worker

protected array $ctor

The arguments for constructor of new Workers

protected array $workers

References to Workers

protected int $last

Offset in workers of the last Worker used

Methods

__construct(int $size, string $class = 'Worker', array $ctor = [])

(PECL pthreads >= 2.0.0)
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.

int
collect(callable|null $collector = null)

(PECL pthreads >= 2.0.0)
Allows the pool to collect references determined to be garbage by the optionally given collector

void
resize(int $size)

(PECL pthreads >= 2.0.0)
Resize the Pool

void
shutdown()

(PECL pthreads >= 2.0.0)
Shuts down all of the workers in the pool. This will block until all submitted tasks have been executed.

int
submit(Threaded $task)

(PECL pthreads >= 2.0.0)
Submit the task to the next Worker in the Pool

int
submitTo(int $worker, Threaded $task)

(PECL pthreads >= 2.0.0)
Submit a task to the specified worker in the pool. The workers are indexed from 0, and will only exist if the pool has needed to create them (since threads are lazily spawned).

Details

__construct(int $size, string $class = 'Worker', array $ctor = [])

(PECL pthreads >= 2.0.0)
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

int $size

The maximum number of workers for this pool to create

string $class

[optional]

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

array $ctor

[optional]

An array of arguments to be passed to new Workers

int collect(callable|null $collector = null)

(PECL pthreads >= 2.0.0)
Allows the pool to collect references determined to be garbage by the optionally given collector

Parameters

callable|null $collector

[optional]

A Callable collector that returns a boolean on whether the task can be collected or not. Only in rare cases should a custom collector need to be used.

Return Value

int

The number of remaining tasks in the pool to be collected

void resize(int $size)

(PECL pthreads >= 2.0.0)
Resize the Pool

Parameters

int $size

The maximum number of Workers this Pool can create

Return Value

void

void shutdown()

(PECL pthreads >= 2.0.0)
Shuts down all of the workers in the pool. This will block until all submitted tasks have been executed.

Return Value

void

int submit(Threaded $task)

(PECL pthreads >= 2.0.0)
Submit the task to the next Worker in the Pool

Parameters

Threaded $task

The task for execution

Return Value

int

the identifier of the Worker executing the object

int submitTo(int $worker, Threaded $task)

(PECL pthreads >= 2.0.0)
Submit a task to the specified worker in the pool. The workers are indexed from 0, and will only exist if the pool has needed to create them (since threads are lazily spawned).

Parameters

int $worker

The worker to stack the task onto, indexed from 0

Threaded $task

The task for execution

Return Value

int

The identifier of the worker that accepted the task