Find out what is your ‘PHP Extension Build’ version by using phpinfo(). You can use this - http://localhost/?phpinfo=1
Download the pthreads that matches your php version (32 bit or 64 bit) and php extension build (currently used VC11). Use this link for download - http://windows.php.net/downloads/pecl/releases/pthreads/
Extract the zip -
Move php_pthreads.dll to the ‘bin\php\ext\’ directory.
Move pthreadVC2.dll to the ‘bin\php\’ directory.
Move pthreadVC2.dll to the ‘bin\apache\bin’ directory.
Move pthreadVC2.dll to the ‘C:\windows\system32’ directory.Open php\php.ini and add
extension=php_pthreads.dll
Now restart server and you are done. Thanks.
<?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();
?>
使用线程池测试多线程
If you try to test threading, remember to let php think slow:
Skript: -- C:\Webserver\htdocs>php mttest.php
<?php
class My extends Thread{
function run(){
for($i=1;$i<10;$i++){
echo Thread::getCurrentThreadId() . "\n";
sleep(2); // <------
}
}
}
for($i=0;$i<2;$i++){
$pool[] = new My();
}
foreach($pool as $worker){
$worker->start();
}
foreach($pool as $worker){
$worker->join();
}
?>
Output: -- C:\Webserver\htdocs>php mttest.php
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
If you leave sleep() out, the cpu-time for the threads is long enough to complete the script at once.