PHP使用Pthread实现的多线程操作实例_php技巧_脚本之家

2020-02-07 23:42栏目:bob体育平台
TAG:

本文实例叙述了PHP使用Pthread完结的四线程操作。共享给咱们供大家参照他事他说加以调查,具体如下:

res = '暂无,第一次运行.'; $this->param = 0; $this->lurl = 0; $this->name = $name; $this->runing = true; $this->lc = false; } public function run() { while  { if  { $nt = rand; echo "线程[{$this->name}]收到任务参数::{$this->param},需要{$nt}秒处理数据.n"; $this->res = rand; $this->lurl = $this->param; $this->param = ''; } else { echo "线程[{$this->name}]等待任务..n"; } sleep; } }}//这里创建线程池.$pool[] = new vote;$pool[] = new vote;$pool[] = new vote;//启动所有线程,使其处于工作状态foreach  { $w->start();}//派发任务给线程for ($i = 1; $i < 10; $i++) { $worker_content = rand { foreach  { //参数为空则说明线程空闲 if  { $worker->param = $worker_content; echo "[{$worker->name}]线程空闲,放入参数{$worker_content},上次参数[{$worker->lurl}]结果[{$worker->res}].n"; break 2; } } sleep; }}echo "所有线程派发完毕,等待执行完成.n";//等待所有线程运行结束while  { //遍历检查线程组运行结束 foreach ($pool as $key => $threads) { if  { echo "[{$threads->name}]线程空闲,上次参数[{$threads->lurl}]结果[{$threads->res}].n"; echo "[{$threads->name}]线程运行完成,退出.n"; //设置结束标志 $threads->runing = false; unset; } } echo "等待中...n"; sleep;}echo "所有线程执行完毕.n";

目的在于本文所述对我们php程序设计有着扶植。

版权声明:本文由bob体育app发布于bob体育平台,转载请注明出处:PHP使用Pthread实现的多线程操作实例_php技巧_脚本之家