Home >Backend Development >PHP Tutorial >PHP installs threads multi-thread extension
php5.3 or above, and it is a thread-safe version. The compilers used by apache and php must be consistent.
If Thread Safety is enabled through phpinfo(), it is a thread-safe version.
You can know the compiler used by viewing the Compiler item through phpinfo(). Mine is: MSVC9 (Visual C++ 2008).
1. Download the pthreads extension
Download address: http://windows.php.net/downloads/pecl/releases/pthreads
2. 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).
3. Add thread class
<?php<br />class Thread<br />{<br /> var $hooks = array();<br /> var $args = array();<br /> function thread()<br /> {<br /> }<br /> function addthread($func)<br /> {<br /> $args = array_slice(func_get_args(), 1);<br /> $this->hooks[] = $func;<br> $this->args[] = $args;<br> return true;<br> }<br> function runthread()<br> {<br> if(isset($_GET['flag']))<br> {<br> $flag = intval($_GET['flag']);<br> }<br> if($flag || $flag === 0)<br> {<br> call_user_func_array($this->hooks[$flag], $this->args[$flag]);<br> }<br> else<br> {<br> for($i = 0, $size = count($this->hooks); $i < $size; $i++)<br /> {<br /> $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']);<br /> if($fp)<br /> {<br /> $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1rn";<br /> $out .= "Host: {$_SERVER['HTTP_HOST']}rn";<br /> $out .= "Connection: Closernrn";<br /> fputs($fp,$out);<br /> fclose($fp);<br /> }<br /> }<br /> }<br /> }<br />}<br />
4. Test pthreads extension
include('thread.php');<br />class AsyncOperation extends Thread {<br /> public function __construct($arg){<br /> $this->arg = $arg;<br> }<br> public function run(){<br> if($this->arg){<br> printf("Hello %s\n", $this->arg);<br> }<br> }<br>}<br>$thread = new AsyncOperation("World");<br>if($thread->start())<br> $thread->join();<br>
The above introduces the installation of threads multi-thread extension in PHP, including the content on my computer. I hope it will be helpful to friends who are interested in PHP tutorials.