現在功能十分簡單而已,大家互相學習一下吧,多多指教
//********************************************************Thread.php
<?php
/**************************************************************************************
class Thread
version:alpha
by:Alex Lau
email:alex621@gmail.com
If you got any questions, feel free to ask me.
In this class, there are six properties
var $func; //The function name that you want to call
var $arg; //The arguments you want to pass in
var $thisFileName; //This file 's name
var $fp; //File pointer
var $host; //Host
var $port; //Port
And there is four methods,
void Thread(string $host, [int $port = 80]) // constructor
void setFunc(string $func,array $arg)
$func is a string of the function name
$arg is an array of the arguments
Usage:
$arg = array ( 2, 3);
$func = "test ";
The method will call test(2,3).
void start() To start the thread
mixed getreturn() To get the return value from the function that called by setFunc
void setPort() To set the port
void setHost() To set the host
Since serialize() does not support the resource type, this class cannot be used to pass in or return the resource type.
**************************************************************************************/
/**************************************************************************************
class Thread
version:alpha
by:Alex Lau
email:alex621@gmail.com
有問題的話就問我吧
這個類別中有六個屬性
var $func; //你要呼叫的function名稱
var $arg; //你要傳遞的參數
var $thisFileName;//include這個物件的檔名
var $fp; //檔案指針
var $host; //伺服器的host
var $port; //伺服器的端口
有六個方法
void Thread(string $host, [int $port = 80]) //建構子
void setFunc(string $func,array $arg)
$func 是function的名稱
$arg 是一個參數的數組(array)
例如:
$arg = array ( 2, 3);
$func = "test ";
此物件會呼叫 test(2,3).
void start() //開始線程
mixed getreturn() //取得線程返回的數值
void setPort(int $port) //設定端口
void setHost(string $host) //設定主機位址
由於serialize()不支援resource,這個類別也不支援傳遞或返回resource。
**************************************************************************************/
class Thread{
var $func;
var $arg;
var $thisFileName;
var $fp;
var $host;
var $port;
function Thread($host,$port= " "){
$this-> host = $host;
if ($port != " "){
$this-> port = $port;
}else{
$this-> port = 80;
}
$this-> thisFileName = $_SERVER[ "SCRIPT_NAME "];
}
function setFunc($func,$arg=false){ 数据挖掘研究院
$i=0;
$this-> arg = " ";
if ($arg){
foreach ($arg as $argument){
$this-> arg .= "&a[]= ".urlencode(serialize($argument));
}
}
$this-> func = $func;
}
function setPort($port){
$this-> port = $port;
}
function setHost($host){
$this-> host = $host;
}
function start(){
$this-> fp = fsockopen($this-> host,30);
$header = "GET ".$this-> thisFileName. "?threadrun=1&f= ".urlencode($this-> func).$this-> arg. " HTTP/1.1 ";
$header .= "Host: ".$this-> host. " ";
$header .= "Connection: Close ";
fputs($this-> fp,$header);
}
function getreturn(){ 数据挖掘研究院
$flag=false;
while (!feof($this-> fp)) {
$buffer = fgets($this-> fp, 4096);
if ($flag){
$output .= $buffer;
}
if (trim($buffer) == " "){
$flag = true;
}
}
return unserialize(trim($output));
}
}
if (isset($_GET[ 'threadrun '])){
$arg_str = " ";
$i=0;
if (isset($_GET[ 'a '])){
foreach($_GET[ 'a '] as $argument){
if ($i == 0){
$arg_str .= unserialize($argument);
}else{
$arg_str .= ", ".unserialize($argument);
}
$i++;
}
}
eval( "$return = ".$_GET[ 'f ']. "( ".$arg_str. "); ");
echo serialize($return);
exit;
}
?>
//*****************************************************test.php
<?php
include_once( "Thread.php ");
function test($test_arg){
return "傳進來的參數為: ".$test_arg. "。 <br /> ";
//return "Pass in variable: ".$test_arg. ". <br /> ";
}
function test_2($test_arg){
$start = time();
while (time() < $start+$test_arg){
}
return $test_arg. "秒己經過了。 <br /> ";
//return $test_arg. "seconds have passed. <br /> ";
}
$program_start_time = time();
$thread_a = new Thread( "localhost ",30);
$thread_a-> setFunc( "test ",array(100));
$thread_a-> start();
$thread_b = new Thread( "localhost ",30); 数据挖掘研究院
$thread_b-> setFunc( "test_2 ",array(2));
$thread_b-> start();
$thread_c = new Thread( "localhost ",30);
$thread_c-> setFunc( "test_2 ",array(3));
$thread_c-> start();
echo $thread_a-> getreturn();
echo $thread_b-> getreturn();
echo $thread_c-> getreturn();
echo "主程式咝辛

