php Pthread 多线程基本介绍
我们可以通过安装Pthread扩展来让PHP支持多线程。
<?php //实现多线程必须继承Thread类
class test extends Thread {
public function __construct($arg){
$this->arg = $arg;
} //当调用start方法时,该对象的run方法中的代码将在独立线程中异步执行。
public function run(){
if($this->arg){
printf("Hello %s\n", $this->arg);
}
}
}
$thread = new test("World"); if($thread->start()) {
//join方法的作用是让当前主线程等待该线程执行完毕
//确认被join的线程执行结束,和线程执行顺序没关系。
//也就是当主线程需要子线程的处理结果,主线程需要等待子线程执行完毕
//拿到子线程的结果,然后处理后续代码。
$thread->join();
}
?>
我们把上述代码修改一下,看看效果
<?php class test extends Thread {
public function __construct($arg){
$this->arg = $arg;
}
public function run(){
if($this->arg){
sleep(3);
printf("Hello %s\n", $this->arg);
}
}
}
$thread = new test("World"); $thread->start(); echo "main thread\r\n";
?>
<?php
class test extends Thread {
private $name = '';
private $res = null; public function __construct($name, $res){
$this->name = $name;
$this->res = $res;
}
public function run(){
while(!feof($this->res)) {
if(flock($this->res, LOCK_EX)) {
$data = fgets($this->res);
$data = trim($data);
echo "Thread {$this->name} Read {$data} \r\n";
sleep(1);
flock($this->res, LOCK_UN);
}
}
}
} $fp = fopen('./test.log', 'rb'); $threads[] = new test('a', $fp);
$threads[] = new test('b', $fp); foreach($threads as $thread) {
$thread->start();
} foreach($threads as $thread) {
$thread->join();
}
?>
111111
222222
333333
444444
555555
666666

<?php
class Total extends Thread {
public $name = '';
private $total = 0;
private $startNum = 0;
private $endNum = 0; public function __construct($name, $startNum, $endNum){
$this->name = $name;
$this->startNum = $startNum;
$this->endNum = $endNum;
}
public function run(){
for($ix = $this->startNum; $ix < $this->endNum; ++$ix) {
$this->total += $ix;
}
echo "Thread {$this->name} total: {$this->total} \r\n";
}
public function getTotal() {
return $this->total;
}
} $num = 10000000;
$threadNum = 10;
$setp = $num / $threadNum;
$startNum = 0; $startTime = microtime(true);
for($ix = 0; $ix < $threadNum; ++$ix) {
$endNum = $startNum + $setp;
$thread = new Total($ix, $startNum, $endNum);
$thread->start();
$startNum = $endNum;
$threads[] = $thread;
} $total = 0;
foreach($threads as $thread) {
$thread->join();
$total += $thread->getTotal();
} $endTime = microtime(true);
$time = $endTime - $startTime; echo "total : {$total} time : {$time} \r\n";

我们不使用多线程,来计算这累加和,代码如下:
<?php
$total = 0; $startTime = microtime(true); for($ix = 0; $ix < 10000000; ++$ix) {
$total += $ix;
} $endTime = microtime(true);
$time = $endTime - $startTime; echo "total : {$total} time : {$time} \r\n";

我们可以看到使用多线程和不使用,得到的结果是一样的,但是处理时间,多线程就慢很多。(*主要是线程的创建也是需要资源的,而且线程之间的相互切换也需要时间,这里的例子主要说明如何把一个问题分配给多个子线程去处理,然后主线程拿到子线程的结果并处理得到我们需要的结果。)
php Pthread 多线程基本介绍的更多相关文章
- pthread多线程编程的学习小结
pthread多线程编程的学习小结 pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写 程序员必上的开发者服务平台 —— DevSt ...
- clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结(转)
进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合,这些资源在Linux中被抽 象成各种数据对象:进程控制块.虚存空间.文件系统,文件I/O.信号处理函数.所以创建一个进程的 过程就是这 ...
- iOS开发多线程篇—多线程简单介绍
iOS开发多线程篇—多线程简单介绍 一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcod ...
- iOS边练边学--多线程NSOperation介绍,子类实现多线程的介绍(任务和队列),队列的取消、暂停(挂起)和恢复,操作依赖与线程间的通信
一.NSOperation NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加 ...
- VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程 转载
VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程 转载 #include <stdio.h>#include &l ...
- 城市经纬度 json 理解SignalR Main(string[] args)之args传递的几种方式 串口编程之端口 多线程详细介绍 递归一个List<T>,可自己根据需要改造为通用型。 Sql 优化解决方案
城市经纬度 json https://www.cnblogs.com/innershare/p/10723968.html 理解SignalR ASP .NET SignalR 是一个ASP .NET ...
- C语言使用pthread多线程编程(windows系统)二
我们进行多线程编程,可以有多种选择,可以使用WindowsAPI,如果你在使用GTK,也可以使用GTK实现了的线程库,如果你想让你的程序有更多的移植性你最好是选择POSIX中的Pthread函数库,我 ...
- C#多线程编程介绍——使用thread、threadpool、timer
C#多线程编程介绍——使用thread.threadpool.timer 在system.threading 命名空间提供一些使得能进行多线程编程的类和接口,其中线程的创建有以下三种方法:thread ...
- php Pthread 多线程 (一) 基本介绍
我们可以通过安装Pthread扩展来让PHP支持多线程. 线程,有时称为轻量级进程,是程序执行的最小单元.线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,它与同属 ...
随机推荐
- post-image.sh hacking
#********************************************************************************* #* post-image.sh ...
- CF449 (Div. 1简单题解)
A .Jzzhu and Chocolate pro:现在给定一个大小为N*M的巧克力,让你横着或者竖着切K刀,都是切的整数大小,而且不能切在相同的地方,求最大化其中最小的块. (N,M,K<1 ...
- 硬盘安装Linux(ubuntu,centos)
硬盘安装Linux 使用硬盘安装Linux最大的好处不只是方便,是快速.之前使用U盘安装,很慢,没有记录具体时间.Ubuntu区别不大,本身比较小,安装介质只有2G(ubuntu18.10):Cent ...
- hdu1846 Brave Game 博弈
十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻.今天,大家选择上 ...
- java exception 01
问题:java.util.concurrentmodificationexception 背景:java thread 网上找到的出现的例子如下(项目中真实的code不便给出) public clas ...
- AtCoder Grand Contest 031 简要题解
AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...
- JS 冒泡排序法 输出最大值
<html lang="en-US"> <head> <meta charset="UTF-8"> <title> ...
- 【liunx】linux后台执行命令:&和nohup
当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我们更希望它能够在每天的非负荷高峰时间段运行(例如凌晨).为 ...
- 芯灵思SinA33开发板怎样安装虚拟机
芯灵思SinA33开发板怎样安装虚拟机 今天入手一块芯灵思的开发板,型号为SIN-A33,用的是全志的A33芯片,与其它开发板不同的是, 芯灵思开发板手册上用来搭建开发环境的linux系统是cento ...
- CH5701 开车旅行
题意 5701 开车旅行 0x50「动态规划」例题 描述 小A和小B决定利用假期外出旅行,他们将想去的城市从1到N编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 ...