PHPTS:一键免费搭建 Nginx + PHP + MySQL 运行环境

pht\Thread::addFunctionTask

(PECL pht >= 0.0.1)

pht\Thread::addFunctionTaskFunction threading

说明

public pht\Thread::addFunctionTask ( callable $func [, mixed $...funcArgs ] ) : void

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)