PHP - Manual: pht\Thread::addFunctionTask
2024-12-26
(PECL pht >= 0.0.1)
pht\Thread::addFunctionTask — Function threading
Adds a new function task to a pht\Threads internal task queue.
func
The function to be threaded. If it is bound to an instance, then
$this will become NULL
.
...funcArgs
An optional list of arguments for the function. These arguments will be serialised (since they are being passed to another thread).
No return value.
Example #1 Adding a new function task to a thread
<?php
use pht\Thread;
class Test
{
public static function run(){var_dump(5);}
public static function run2(){var_dump(6);}
}
function aFunc(){var_dump(3);}
$thread = new Thread();
$thread->addFunctionTask(static function($one) {var_dump($one);}, 1);
$thread->addFunctionTask(function() {var_dump(2);});
$thread->addFunctionTask('aFunc');
$thread->addFunctionTask('array_map', function ($n) {var_dump($n);}, [4]);
$thread->addFunctionTask(['Test', 'run']);
$thread->addFunctionTask([new Test, 'run2']);
$thread->start();
$thread->join();
以上例程会输出:
int(1) int(2) int(3) int(4) int(5) int(6)
官方地址:https://www.php.net/manual/en/pht-thread.addFunctionTask.php