php 多线程 pthreads安装

http://php.net/manual/en/pthreads.installation.php

  1. Find out what is your ‘PHP Extension Build’ version by using phpinfo(). You can use this - http://localhost/?phpinfo=1

  2. Download the pthreads that matches your php version (32 bit or 64 bit) and php extension build (currently used VC11). Use this link for download - http://windows.php.net/downloads/pecl/releases/pthreads/

  3. Extract the zip -
    Move php_pthreads.dll to the ‘bin\php\ext\’ directory.
    Move pthreadVC2.dll to the ‘bin\php\’ directory.
    Move pthreadVC2.dll to the ‘bin\apache\bin’ directory.
    Move pthreadVC2.dll to the ‘C:\windows\system32’ directory.

  4. Open php\php.ini and add
    extension=php_pthreads.dll

Now restart server and you are done. Thanks.

<?php
class AsyncOperation extends Thread {  
  public function __construct($arg){  
    $this->arg = $arg;  
  }  

  public function run(){  
    if($this->arg){  
      printf("Hello %s\n", $this->arg);  
    }  
  }  
}  
$thread = new AsyncOperation("World");  
if($thread->start())  
  $thread->join();  
?>  

使用线程池测试多线程

If you try to test threading, remember to let php think slow:

Skript: -- C:\Webserver\htdocs>php mttest.php

<?php
class My extends Thread{
    function run(){
        for($i=1;$i<10;$i++){
            echo Thread::getCurrentThreadId() .  "\n";
            sleep(2);     // <------
        }
    }
}

for($i=0;$i<2;$i++){
    $pool[] = new My(); 
}

foreach($pool as $worker){
    $worker->start();
}
foreach($pool as $worker){
    $worker->join();
}
?>

Output: -- C:\Webserver\htdocs>php mttest.php
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816

If you leave sleep() out, the cpu-time for the threads is long enough to complete the script at once.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值