Home  >  Article  >  php教程  >  Basic tutorial on installing threads multi-thread extension in PHP

Basic tutorial on installing threads multi-thread extension in PHP

高洛峰
高洛峰Original
2016-12-21 11:54:291333browse

1. Download the pthreads extension

Download address: http://windows.php.net/downloads/pecl/releases/pthreads

2. Determine whether PHP is a ts or nts version

View the Thread through phpinfo(); Safety item, this item is to check whether it is thread safe. If it is: enabled, generally speaking it should be the ts version, otherwise it should be the nts version.

3. Select the corresponding pthreads version according to the PHP tsnts version

My php version is 5.4.17, so download the php_pthreads-0.1.0-5.4-ts-vc9-x86.zip file package, where 0.1.0 represents the current pthreads version number, 5.4 is the PHP version number, ts is the ts and nts versions that were previously judged to correspond to PHP, vs9 represents the version compiled by the Visual Studio 2008 compiler, and the last x86 represents the 32-bit version.

4. Download the pthreads extension

Download address: http://windows.php.net/downloads/pecl/releases/pthreads

5. Install the pthreads extension

Copy php_pthreads.dll to the directory binphpext.
Copy pthreadVC2.dll to the directory binphp.
Copy pthreadVC2.dll to the directory C:windowssystem32.
Open the php configuration file php.ini. Add extension=php_pthreads.dll at the end
tip! Windows systems need to add the path of pthreadVC2.dll to the PATH environment variable. My Computer--->right mouse button--->Properties--->Advanced--->Environment Variables--->System Variables--->Find the path named Path---> ;Edit--->Add the full path of pthreadVC2.dll at the end of the variable value (mine is C:WINDOWSsystem32pthreadVC2.dll).

6. Add thread class

<?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[&#39;flag&#39;]))
    {
      $flag = intval($_GET[&#39;flag&#39;]);
    }
    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[&#39;HTTP_HOST&#39;],$_SERVER[&#39;SERVER_PORT&#39;]);
        if($fp)
        {
          $out = "GET {$_SERVER[&#39;PHP_SELF&#39;]}?flag=$i HTTP/1.1rn";
          $out .= "Host: {$_SERVER[&#39;HTTP_HOST&#39;]}rn";
          $out .= "Connection: Closernrn";
          fputs($fp,$out);
          fclose($fp);
        }
      }
    }
  }
}


7. Test pthreads extension

include(&#39;thread.php&#39;);
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();

The above content introduces you to the basic tutorial of installing threads multi-thread extension in PHP. I hope you like it.

For more articles related to PHP installation threads multi-thread extension basic tutorial, please pay attention to PHP Chinese website!


Statement:
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