首页 技术 正文
技术 2022年11月16日
0 收藏 566 点赞 3,534 浏览 1449 个字

php5.3或以上,且为线程安全版本。apache和php使用的编译器必须一致。
通过phpinfo()查看Thread Safety为enabled则为线程安全版。
通过phpinfo()查看Compiler项可以知道使用的编译器。本人的为:MSVC9 (Visual C++ 2008)。

一、下载pthreads扩展
下载地址:http://windows.php.net/downloads/pecl/releases/pthreads

二、安装pthreads扩展
复制php_pthreads.dll 到目录 bin\php\ext\ 下面。
复制pthreadVC2.dll 到目录 bin\php\ 下面。
复制pthreadVC2.dll 到目录 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();
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,966
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,487
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,332
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,115
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,748
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,783