>  기사  >  백엔드 개발  >  PHP 初试多线程pthreads扩展

PHP 初试多线程pthreads扩展

WBOY
WBOY원래의
2016-06-23 13:12:041029검색

pthread是unix-like多线程支持库,这里可以作为php的多线程扩展支持库。

我下载的是php_pthreads-2.0.10-5.3-ts-vc9-x86.zip,电脑是64bit,也就是说pthreads在64bit系统上兼容32bit,此版本支持php5.3.x线程安全(ts)版本,当然也名称标注了5.3-ts版本。


在这里安装方式请参阅

PHP安装pthreads多线程扩展教程

注意:不要安装太新的版本,否则有可能安装不成功,一定要和php的版本匹配。


来段代码测试一下:

<?phperror_reporting(E_ALL ^ E_NOTICE);ob_implicit_flush();header('content-type:text/html;charset=utf-8');class AsyncTask extends Thread {  private $arg;  private $index;  public function __construct($arg){    $this->arg = $arg;    $this->index = 0;  }  public function run(){    if($this->arg){         while($this->index<10)    {    	  echo  microtime(true)."---线程ID:".(Thread::getCurrentThreadId())."---index=".$this->index."<br/>";    	 $this->index++;    }    }  }}$thread = new AsyncTask("Woker");if($thread->start()) { 	$index = 0; 	while($index<10){ 	    //在主线程输出红色文本 	    echo "<font color='red'>".microtime(true)."---主线程ID".(Thread::getCurrentThreadId())."--index=".$index."</font><br/>"; 	    $index++; 	} }else{ 	 echo "failed"; }?>


运行结果

通过时间输出对比,我们发现,主线程和子线程符合多线程运行效果,但对于结果的输出。



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.