class Worker extends Thread implements Traversable, Countable, ArrayAccess (View source)

(PECL pthreads >= 2.0.0)
Worker Threads have a persistent context, as such should be used over Threads in most cases.
When a Worker is started, the run method will be executed, but the Thread will not leave until one of the following conditions are met:

  • the Worker goes out of scope (no more references remain)
  • the programmer calls shutdown
  • the script dies
This means the programmer can reuse the context throughout execution; placing objects on the stack of the Worker will cause the Worker to execute the stacked objects run method.

Properties

protected Worker $worker

Worker object in which this Threaded is being executed

from  Threaded

Methods

void
addRef()

(PECL pthreads >= 3.0.0)
Increments the internal number of references to a Threaded object

from  Threaded
array
chunk(int $size, bool $preserve = false)

(PECL pthreads >= 2.0.0)
Fetches a chunk of the objects property table of the given size, optionally preserving keys

from  Threaded
int
count()

(PECL pthreads >= 2.0.0)
Returns the number of properties for this object

from  Threaded
void
delRef()

(PECL pthreads >= 3.0.0)
Decrements the internal number of references to a Threaded object

from  Threaded
static bool
extend(string $class)

(PECL pthreads >= 2.0.8)
Makes thread safe standard class at runtime

from  Threaded
int
getRefCount()

(PECL pthreads >= 3.0.0)
Retrieves the internal number of references to a Threaded object

from  Threaded
bool
isRunning()

(PECL pthreads >= 2.0.0)
Tell if the referenced object is executing

from  Threaded
bool
isGarbage()

(PECL pthreads >= 3.1.0)

from  Threaded
bool
isTerminated()

(PECL pthreads >= 2.0.0)
Tell if the referenced object was terminated during execution; suffered fatal errors, or threw uncaught exceptions

from  Threaded
bool
merge($from, $overwrite = true)

(PECL pthreads >= 2.0.0)
Merges data into the current object

from  Threaded
bool
notify()

(PECL pthreads >= 2.0.0)
Send notification to the referenced object

from  Threaded
bool
notifyOne()

(PECL pthreads >= 3.0.0)
Send notification to the referenced object. This unblocks at least one of the blocked threads (as opposed to unblocking all of them, as seen with Threaded::notify()).

from  Threaded
mixed
pop()

(PECL pthreads >= 2.0.0)
Pops an item from the objects property table

from  Threaded
void
run()

(PECL pthreads >= 2.0.0)
The programmer should always implement the run method for objects that are intended for execution.

from  Threaded
mixed
shift()

(PECL pthreads >= 2.0.0)
Shifts an item from the objects property table

from  Threaded
mixed
synchronized(Closure $block, mixed ...$_)

(PECL pthreads >= 2.0.0)
Executes the block while retaining the referenced objects synchronization lock for the calling context

from  Threaded
bool
wait(int $timeout = 0)

(PECL pthreads >= 2.0.0)
Will cause the calling context to wait for notification from the referenced object

from  Threaded
bool
offsetExists(TKey $offset)

No description

from  Threaded
mixed
offsetGet(TKey $offset)

No description

from  Threaded
void
offsetSet(TKey $offset, TValue $value)

No description

from  Threaded
void
offsetUnset(TKey $offset)

No description

from  Threaded
int
getCreatorId()

(PECL pthreads >= 2.0.0)
Will return the identity of the Thread that created the referenced Thread

from  Thread
static Thread
getCurrentThread()

(PECL pthreads >= 2.0.0)
Return a reference to the currently executing Thread

from  Thread
static int
getCurrentThreadId()

(PECL pthreads >= 2.0.0)
Will return the identity of the currently executing Thread

from  Thread
int
getThreadId()

(PECL pthreads >= 2.0.0)
Will return the identity of the referenced Thread

from  Thread
bool
isJoined()

(PECL pthreads >= 2.0.0)
Tell if the referenced Thread has been joined

from  Thread
bool
isStarted()

(PECL pthreads >= 2.0.0)
Tell if the referenced Thread was started

from  Thread
bool
join()

(PECL pthreads >= 2.0.0)
Causes the calling context to wait for the referenced Thread to finish executing

from  Thread
bool
start(int $options = PTHREADS_INHERIT_ALL)

(PECL pthreads >= 2.0.0)
Will start a new Thread to execute the implemented run method

from  Thread
int
collect(callable|null $collector = null)

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

int
getStacked()

(PECL pthreads >= 2.0.0)
Returns the number of tasks left on the stack

bool
isShutdown()

(PECL pthreads >= 2.0.0)
Whether the worker has been shutdown or not

bool
shutdown()

(PECL pthreads >= 2.0.0)
Shuts down the Worker after executing all of the stacked tasks

int
stack(Threaded $work)

(PECL pthreads >= 2.0.0)
Appends the new work to the stack of the referenced worker

Threaded|null
unstack()

(PECL pthreads >= 2.0.0)
Removes the first task (the oldest one) in the stack

Details

void addRef()

(PECL pthreads >= 3.0.0)
Increments the internal number of references to a Threaded object

Return Value

void

array chunk(int $size, bool $preserve = false)

(PECL pthreads >= 2.0.0)
Fetches a chunk of the objects property table of the given size, optionally preserving keys

Parameters

int $size

The number of items to fetch

bool $preserve

[optional]

Preserve the keys of members, by default false

Return Value

array

An array of items from the objects property table

int count()

(PECL pthreads >= 2.0.0)
Returns the number of properties for this object

Return Value

int

The custom count as an integer.

The return value is cast to an integer.

void delRef()

(PECL pthreads >= 3.0.0)
Decrements the internal number of references to a Threaded object

Return Value

void

static bool extend(string $class)

(PECL pthreads >= 2.0.8)
Makes thread safe standard class at runtime

Parameters

string $class

The class to extend

Return Value

bool

A boolean indication of success

int getRefCount()

(PECL pthreads >= 3.0.0)
Retrieves the internal number of references to a Threaded object

Return Value

int

The number of references to the Threaded object

bool isRunning()

(PECL pthreads >= 2.0.0)
Tell if the referenced object is executing

Return Value

bool

A boolean indication of state

bool isGarbage()

(PECL pthreads >= 3.1.0)

Return Value

bool

Whether this object is garbage or not

See also

Collectable::isGarbage

bool isTerminated()

(PECL pthreads >= 2.0.0)
Tell if the referenced object was terminated during execution; suffered fatal errors, or threw uncaught exceptions

Return Value

bool

A boolean indication of state

bool merge($from, $overwrite = true)

(PECL pthreads >= 2.0.0)
Merges data into the current object

Parameters

$from
$overwrite

Return Value

bool

A boolean indication of success

bool notify()

(PECL pthreads >= 2.0.0)
Send notification to the referenced object

Return Value

bool

A boolean indication of success

bool notifyOne()

(PECL pthreads >= 3.0.0)
Send notification to the referenced object. This unblocks at least one of the blocked threads (as opposed to unblocking all of them, as seen with Threaded::notify()).

Return Value

bool

A boolean indication of success

mixed pop()

(PECL pthreads >= 2.0.0)
Pops an item from the objects property table

Return Value

mixed

The last item from the objects property table

void run()

(PECL pthreads >= 2.0.0)
The programmer should always implement the run method for objects that are intended for execution.

Return Value

void

The methods return value, if used, will be ignored

mixed shift()

(PECL pthreads >= 2.0.0)
Shifts an item from the objects property table

Return Value

mixed

The first item from the objects property table

mixed synchronized(Closure $block, mixed ...$_)

(PECL pthreads >= 2.0.0)
Executes the block while retaining the referenced objects synchronization lock for the calling context

Parameters

Closure $block

The block of code to execute

mixed ...$_

[optional]

Variable length list of arguments to use as function arguments to the block

Return Value

mixed

The return value from the block

bool wait(int $timeout = 0)

(PECL pthreads >= 2.0.0)
Will cause the calling context to wait for notification from the referenced object

Parameters

int $timeout

[optional]

An optional timeout in microseconds

Return Value

bool

A boolean indication of success

bool offsetExists(TKey $offset)

No description

Parameters

TKey $offset

An offset to check for.

Return Value

bool

true on success or false on failure.

The return value will be casted to boolean if non-boolean was returned.

See also

ArrayAccess::offsetExists

mixed offsetGet(TKey $offset)

No description

Parameters

TKey $offset

The offset to retrieve.

Return Value

mixed

Can return all value types.

See also

ArrayAccess::offsetGet

void offsetSet(TKey $offset, TValue $value)

No description

Parameters

TKey $offset

The offset to assign the value to.

TValue $value

The value to set.

Return Value

void

No value is returned.

See also

ArrayAccess::offsetSet

void offsetUnset(TKey $offset)

No description

Parameters

TKey $offset

The offset to unset.

Return Value

void

No value is returned.

See also

ArrayAccess::offsetUnset

int getCreatorId()

(PECL pthreads >= 2.0.0)
Will return the identity of the Thread that created the referenced Thread

Return Value

int

A numeric identity

static Thread getCurrentThread()

(PECL pthreads >= 2.0.0)
Return a reference to the currently executing Thread

Return Value

Thread

An object representing the currently executing Thread

static int getCurrentThreadId()

(PECL pthreads >= 2.0.0)
Will return the identity of the currently executing Thread

Return Value

int

A numeric identity

int getThreadId()

(PECL pthreads >= 2.0.0)
Will return the identity of the referenced Thread

Return Value

int

A numeric identity

bool isJoined()

(PECL pthreads >= 2.0.0)
Tell if the referenced Thread has been joined

Return Value

bool

A boolean indication of state

bool isStarted()

(PECL pthreads >= 2.0.0)
Tell if the referenced Thread was started

Return Value

bool

A boolean indication of state

bool join()

(PECL pthreads >= 2.0.0)
Causes the calling context to wait for the referenced Thread to finish executing

Return Value

bool

A boolean indication of success

bool start(int $options = PTHREADS_INHERIT_ALL)

(PECL pthreads >= 2.0.0)
Will start a new Thread to execute the implemented run method

Parameters

int $options

[optional]

An optional mask of inheritance constants, by default PTHREADS_INHERIT_ALL

Return Value

bool

A boolean indication of success

int collect(callable|null $collector = null)

(PECL pthreads >= 3.0.0)
Allows the worker 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 on the worker's stack to be collected

int getStacked()

(PECL pthreads >= 2.0.0)
Returns the number of tasks left on the stack

Return Value

int

Returns the number of tasks currently waiting to be executed by the worker

bool isShutdown()

(PECL pthreads >= 2.0.0)
Whether the worker has been shutdown or not

Return Value

bool

Returns whether the worker has been shutdown or not

bool shutdown()

(PECL pthreads >= 2.0.0)
Shuts down the Worker after executing all of the stacked tasks

Return Value

bool

Whether the worker was successfully shutdown or not

int stack(Threaded $work)

(PECL pthreads >= 2.0.0)
Appends the new work to the stack of the referenced worker

Parameters

Threaded $work

A Threaded object to be executed by the Worker

Return Value

int

The new size of the stack

Threaded|null unstack()

(PECL pthreads >= 2.0.0)
Removes the first task (the oldest one) in the stack

Return Value

Threaded|null

The item removed from the stack