Basic tutorial _ PHP instance for installing threads multi-thread extension in php

Source: Internet
Author: User
Php5.3 or later, and is a thread-safe version. The Compiler used by apache and php must be the same. If ThreadSafety is set to enabled through phpinfo (), the thread-safe version is used. If you use phpinfo () to view the Compiler item, you can know the Compiler used, this article will introduce you to the basic tutorial on installing threads multi-thread extension in PHP. For more information, see section 1. Download pthreads extension.

: Http://windows.php.net/downloads/pecl/releases/pthreads

Ii. Determine whether PHP is a ts or an ETS version.

Use phpinfo (); to check the Thread Safety item. This project is to check whether it is Thread security. If it is: enabled, it should be ts version; otherwise, it will be the ETS version.

3. Select the corresponding pthreads version based on PHP ts \ NT

The idea file package, where 0.1.0 indicates the current pthreads version number, 5.4 indicates the php version number, ts indicates the ts version corresponding to php, and vs9 indicates that it is compiled by the Visual Studio 2008 compiler, the last x86 version represents a 32-bit version.

4. Download The pthreads Extension

: Http://windows.php.net/downloads/pecl/releases/pthreads

5. Install pthreads Extension

Copy php_pthreads.dll to the directory bin \ php \ ext.
Copy pthreadVC2.dll to the directory bin \ php.
Copy pthreadVC2.dll to the directory C: \ windows \ system32.
Open the php configuration file php. ini. Add extension = php_pthreads.dll
Prompt! In Windows, you must add the PATH of pthreadVC2.dll to the path environment variable. My computer ---> right-click ---> properties ---> advanced ---> environment variables ---> system variables ---> find the Path named ---> edit ---> Add pthreadVC2.dll to the end of the variable value complete path (my C: \ WINDOWS \ system32 \ pthreadVC2.dll ).

6. Add thread class

<?phpclass 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);        }      }    }  }}

VII. Test pthreads Extension

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();

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.