This is automatic generated document for things in Swow\Thread class

Class Thread

An OS thread with its own PHP request.

The thread body and its arguments are serialized at start. The child request has no user code, no autoloader and no global state. A closure cannot be serialized, so a closure body cannot start. Use an internal function or a method of a Swow object that shares state by id.

Methods

Static method getCurrent

public static function getCurrent ( ) : self

Return the Thread object for the current thread.

Throws ThreadException when Swow has no current thread.

Static method getCurrentId

public static function getCurrentId ( ) : int

Return the native id of the current thread.

This method returns 0 when Swow has no current thread.

Static method killAll

public static function killAll ( ) : int

Send a kill request to every child thread.

This method does not wait for the threads to exit.

Returns int the number of threads that accepted the request

Static method run

public static function run ( callable $callable , mixed ... $args ) : self

Create and start a thread.

The body and the arguments are serialized. See the class description. This method waits for the child runtime. In a coroutine, this method suspends the current coroutine.

Param callable $callable The thread body. Param mixed $args Arguments for the thread body.

Throws ThreadException when the thread cannot start. Throws \Exception when the body or an argument cannot be serialized.

Static method runCode

public static function runCode ( string $code ) : self

Create and start a thread that runs PHP code.

The code runs as top level code in a fresh request. Parent globals are not visible. The code has no $context variable. The return value and the exceptions of the code are discarded. Read getExitStatus() for the result. This method waits for the child runtime. In a coroutine, this method suspends the current coroutine.

Param string $code PHP code without an opening tag

Throws ThreadException when the thread cannot start.

Method __construct

public function __construct ( callable $callable , mixed ... $args )

Create a thread.

This method does not start the thread. This method does not yield.

Param callable $callable The thread body. Param mixed $args Arguments for the thread body.

Method __debugInfo

public function __debugInfo ( ) : array

Return debug information for var_dump.

Method detach

public function detach ( ) : self

Stop waiting for the thread.

RSHUTDOWN joins the thread later. Do not call wait after this method. This method does not yield.

Method getExitStatus

public function getExitStatus ( ) : int

Return the exit status of the thread.

The status is 0 for a normal return, N for exit(N), 255 for an uncaught exception, and -1 for a kill.

Method getId

public function getId ( ) : int

Return the native thread id.

The value is fixed at start and does not change after join. The OS can reuse the id after the thread exits. This method returns 0 before start.

Method isDetached

public function isDetached ( ) : bool

Return true after detach was called.

Method isRunning

public function isRunning ( ) : bool

Return true while the thread is running.

Method isStarted

public function isStarted ( ) : bool

Return true after start was called.

Method kill

public function kill ( ) : self

Send an asynchronous kill request.

This method does not wait for the thread to exit. The thread stops at the next safe point. A blocking native call completes first. After the kill the status is -1. Call wait to get the result.

Throws ThreadException when the thread is not running.

Method start

public function start ( int $timeout ) : self

Start the thread.

This method waits for the child runtime. In a coroutine, this method suspends the current coroutine. Do not call this method twice. A second call throws ThreadException.

Param int $timeout timeout in milliseconds, -1 waits forever

Throws ThreadException when the thread cannot start or the wait times out.

Method wait

public function wait ( int $timeout ) : mixed

Wait for the thread to exit.

A later call returns the cached result. A fatal error in the body gives status 255.

context switching will happen here

Param int $timeout timeout in milliseconds, -1 waits forever

Returns mixed the return value of the body, or null after a fatal error

Throws ThreadException when the wait times out, the thread was killed, or the thread was detached. Throws \Throwable the exception that the thread body threw


Copyright 2022 the Swow contributors