获取pthreads扩展资源
一、获取pthreads扩展资源
官方下载地址:http://windows.php.net/downloads/pecl/releases/pthreads
二、确认PHP的线程安全模式
执行phpinfo();函数,查找输出中的“Thread Safety”项。若显示为enabled,则表明当前PHP为线程安全(TS)版本;反之则为非线程安全(NTS)版本。
三、根据PHP版本匹配pthreads
假设当前PHP版本为5.4.17,应下载名为php_pthreads-0.1.0-5.4-ts-vc9-x86.zip的文件包。其中,0.1.0代表pthreads的版本号,5.4对应PHP主版本号,ts标识线程安全版本(若为nts则选对应项),vc9表示由Visual Studio 2008编译器构建,x86指代32位架构。
四、下载对应的pthreads压缩包
下载地址:http://windows.php.net/downloads/pecl/releases/pthreads
五、部署pthreads扩展文件
将php_pthreads.dll复制到bin\php\ext\目录下。将pthreadVC2.dll分别复制到bin\php\目录以及C:\windows\system32目录下。编辑php配置文件php.ini,在末尾添加extension=php_pthreads.dll。注意:Windows系统需将pthreadVC2.dll所在路径加入PATH环境变量。操作路径为:我的电脑右键属性->高级->环境变量->系统变量->找到Path->编辑->在变量值末尾追加pthreadVC2.dll的完整路径(例如C:\WINDOWS\system32\pthreadVC2.dll)。
六、定义Thread子类
<?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);
}
}
}
}
}
七、验证pthreads扩展功能
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();
以上内容介绍了PHP安装threads多线程扩展的基础教程,希望对大家有所帮助。