加入收藏 | 设为首页 | 会员中心 | 我要投稿 站长网 (https://www.86zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP多线程的实现方法详解

发布时间:2022-10-24 14:02:16 所属栏目:PHP教程 来源:互联网
导读: * @title: PHP多线程类(Thread)
* @version: 1.0
* @author: phper.org.cn < web@phper.org.cn >
* @published: 2010-11-2
*
* PHP多线程应用示例:
* require_once 'thread.class.php';
*

* @title: PHP多线程类(Thread)

* @version: 1.0

* @author: phper.org.cn < web@phper.org.cn >

* @published: 2010-11-2

*

* PHP多线程应用示例:

* require_once 'thread.class.php';

* $thread = new thread();

* $thread->addthread('action_log'php多线程,'a');

* $thread->addthread('action_log','b');

* $thread->addthread('action_log','c');

* $thread->runthread();

*

* function action_log($info) {

* $log = 'log/' . microtime() . '.log';

* $txt = $info . "rnrn" . 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn";

* $fp =fopen($log, 'w');

* fwrite($fp, $txt);

* fclose($fp);

* }

*/

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);

}

}

}

}

}

(编辑:站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!