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";
?>
我们直接调用start方法,而没有调用join。主线程不会等待,而是在输出main thread。子线程等待3秒才输出Hello World。
<?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();
}
?>
我们通过创建两个线程a和b来读取文件test.log中的内容。(*注意,在并发读写文件时,一定要给文件加锁。这里给文件加上独占锁,如果加共享锁会出现读取相同数据。)
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";
我们通过创建10个线程,分别计算累加和,而后主线程把10个线程计算的结果统一相加得到最后结果。

我们不使用多线程,来计算这累加和,代码如下:
<?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开发多线程篇—GCD介绍
iOS开发多线程篇—GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 G ...
- 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 ...
- C语言使用pthread多线程编程(windows系统)二
我们进行多线程编程,可以有多种选择,可以使用WindowsAPI,如果你在使用GTK,也可以使用GTK实现了的线程库,如果你想让你的程序有更多的移植性你最好是选择POSIX中的Pthread函数库,我 ...
- php Pthread 多线程基本介绍
我们可以通过安装Pthread扩展来让PHP支持多线程. 线程,有时称为轻量级进程,是程序执行的最小单元.线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,它与同属 ...
- 多线程02---pThread简单介绍
1.简单介绍 pthread 是属于 POSIX 多线程开发框架. 它是c语言提供的一个跨平台的多线程解决方式.因为其在iOS编程中,操作比較麻烦.一般不用,这里介绍只作为了解. 2.pthread的 ...
- pthread 多线程基础
本文主要介绍如何通过 pthread 库进行多线程编程,并通过以下例子进行说明. 基于莱布尼兹级数计算 \(\pi\) . 多线程归并排序 参考文章: [1] https://computing.ll ...
- NDK中使用pthread多线程中自己写的一个BUG
在使用pthread进行NDK中的多线程开发时,自己写了一个BUG, void *darkGrayThread(void *args) { ThreadParam *param = (ThreadPa ...
随机推荐
- JVM内部细节之二:偏向锁(Biased Locking)
在前面一片文章<JVM内部细节之一:synchronized关键字及实现细节>中已经提到过偏向锁的概念,在理解什么是偏向锁前必须先理解什么是轻量级锁(Lightweight Locking ...
- Apache Doris通过supervisor进行进程管理
下面一段文字是摘自doris官方文档:注:在生产环境中,所有实例都应使用守护进程启动,以保证进程退出后,会被自动拉起,如 Supervisor.如需使用守护进程启动,需要修改各个 start_xx.s ...
- mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat'
mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat' 1.问题起因:搭建环境初始化mysql的时候看到mysql配置文 ...
- 跳表(skiplist)Python实现
# coding=utf-8 # 跳表的Python实现 import random # 最高层数设置为4 MAX_LEVEL = 4 def randomLevel(): ""& ...
- UVA-575-水题-模拟
题意: 按照这个公式模拟 10120skew = 1×(25 −1)+0×(24 −1)+1×(23 −1)+2×(22 −1)+0×(21 −1) = 31+0+7+6+0 = 44. #inclu ...
- OpenACC kernels
▶ 使用 kernels 导语并行化 for 循环 ● 一重循环 #include <stdio.h> #include <time.h> #include <opena ...
- Python简单实现基于VSM的余弦相似度计算
在知识图谱构建阶段的实体对齐和属性值决策.判断一篇文章是否是你喜欢的文章.比较两篇文章的相似性等实例中,都涉及到了向量空间模型(Vector Space Model,简称VSM)和余弦相似度计算相关知 ...
- HTML5 浏览器接收的常用 content-type
<1> 常见的设置方法 response.setHeader("content-type", 'text/html'); <2> 浏览器接收的常用 cont ...
- leetcode191
public class Solution { public int HammingWeight(uint n) { var list = new List<uint>(); do { ; ...
- VBA 打开带密码的文件
' 打开文件 ROSE 为只读密码 CHECK 为编辑密码 Set wb = Workbooks.Open(file, 0, True, , "ROSE", "CH ...