1. Download pthreads extension
Download address: http://windows.php.net/downloads/pecl/releases/pthreads
2. Determine whether PHP is ts or nts version
Check the Thread Safety item through phpinfo();. This item is to check whether it is thread safe. If it is: enabled, generally it should be the ts version, otherwise it is the nts version.
3. Select the version corresponding to pthreads 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, and ts is Previously, it was determined that php corresponds to the ts and nts versions. vs9 represents the Visual Studio 2008 compiler, and the final x86 represents the 32-bit version.
4. Download pthreads extension
Download address: http://windows.php.net/downloads/pecl/releases/pthreads
5. Install 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
hint! 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['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); } } } } }
7. 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 on installing threads multi-thread extension in PHP. I hope you like it.