• 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
    ", $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
    ", $this->arg);
            }
        }
    }
    $thread = new test("World");
    
    $thread->start();
    
    echo "main thread
    ";
    ?>
    
    我们直接调用start方法,而没有调用join。主线程不会等待,而是在输出main thread。子线程等待3秒才输出Hello World。
     
    例1如下:
    <?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} 
    ";
                    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中的内容。(*注意,在并发读写文件时,一定要给文件加锁。这里给文件加上独占锁,如果加共享锁会出现读取相同数据。)
    test.log的内容如下:
    111111
    222222
    333333
    444444
    555555
    666666
    
    执行结果如下:
    php Pthread 多线程 - 怀素真 - 因上努力 果上随缘
     
    例2如下:
    <?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} 
    ";
        }
        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} 
    ";
    
    我们通过创建10个线程,分别计算累加和,而后主线程把10个线程计算的结果统一相加得到最后结果。
    php Pthread 多线程 (一) - 怀素真 - 因上努力 果上随缘
     
    我们不使用多线程,来计算这累加和,代码如下:
    <?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} 
    ";
    
    php Pthread 多线程 (一) - 怀素真 - 因上努力 果上随缘

     我们可以看到使用多线程和不使用,得到的结果是一样的,但是处理时间,多线程就慢很多。(*主要是线程的创建也是需要资源的,而且线程之间的相互切换也需要时间,这里的例子主要说明如何把一个问题分配给多个子线程去处理,然后主线程拿到子线程的结果并处理得到我们需要的结果。)

     
     
     
     
  • 相关阅读:
    POJ1741 Tree(树分治)
    codeforces713D Animals and Puzzle(二维倍增)
    codeforces713C Sonya and Problem Wihtout a Legend(dp)
    codeforces724E Goods transportation(最小割——dp)
    codeforces710E Generate a String(dp)
    codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) 题解(A-D)
    BNUOJ52317 As Easy As Possible(树上倍增)
    hihocoder1386 Pick Your Players(dp)
    常用函数
    vector总结(更新中。。。)
  • 原文地址:https://www.cnblogs.com/jkko123/p/6351604.html
Copyright © 2020-2023  润新知