PHP安装threads多线程扩展基础教程


一、下载pthreads扩展

下载地址:http://windows.php.net/downloads/pecl/releases/pthreads

二、判断PHP是ts还是nts版

通过phpinfo(); 查看其中的 Thread Safety 项,这个项目就是查看是否是线程安全,如果是:enabled,一般来说应该是ts版,否则是nts版。

三、根据PHP ts\nts版选择对应pthreads的版本

本人php版本是5.4.17的所以下载php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包,其中0.1.0表示为当前pthreads版本号,5.4为php版本号,ts就是之前判断php对应的ts、nts版,vs9代表是Visual Studio 2008 compiler编译器编译的,最后的x86代表的是32位的版本。

四、下载pthreads扩展

下载地址:http://windows.php.net/downloads/pecl/releases/pthreads

五、安装pthreads扩展

复制php_pthreads.dll 到目录 bin\php\ext\ 下面。
复制pthreadVC2.dll 到目录 bin\php\ 下面。
复制pthreadVC2.dll 到目录 C:\windows\system32 下面。
打开php配置文件php.ini。在后面加上extension=php_pthreads.dll
提示!Windows系统需要将 pthreadVC2.dll 所在路径加入到 PATH 环境变量中。我的电脑--->鼠标右键--->属性--->高级--->环境变量--->系统变量--->找到名称为Path的--->编辑--->在变量值最后面加上pthreadVC2.dll的完整路径(本人的为C:\WINDOWS\system32\pthreadVC2.dll)。

六、添加thread类

<?php
class Thread
{
  var $hooks = array();
  var $args = array();
  function thread()
  {
  }
  function addthread($func)
  {
    $args = array_slice(func_get_args(), 1);
    $this->hooks[] = $func;
    $this->args[] = $args;
    return true;
  }
  function runthread()
  {
    if(isset($_GET['flag']))
    {
      $flag = intval($_GET['flag']);
    }
    if($flag || $flag === 0)
    {
      call_user_func_array($this->hooks[$flag], $this->args[$flag]);
    }
    else
    {
      for($i = 0, $size = count($this->hooks); $i < $size; $i++)
      {
        $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']);
        if($fp)
        {
          $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1rn";
          $out .= "Host: {$_SERVER['HTTP_HOST']}rn";
          $out .= "Connection: Closernrn";
          fputs($fp,$out);
          fclose($fp);
        }
      }
    }
  }
}

七、测试pthreads扩展

include('thread.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();

以上内容给大家介绍了PHP安装threads多线程扩展基础教程,希望大家喜欢。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3