PHP 初试多线程pthreads扩展

WBOY
Release: 2016-06-23 13:12:04
Original
1029 people have browsed it

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"; }?>
Copy after login


运行结果

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



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!