下載地址:http://windows.php.net/downloads/pecl/releases/pthreads
二、判斷PHP是ts還是nts版
通過phpinfo(); 查看其中的 Thread Safety 項,這個項目就是查看是否是線程安全,如果是:enabled,一般來說應該是ts版,否則是nts版。
三、根據PHP ts\nts版選擇對應pthreads的版本
本人php版本是5.4.17的所以下載php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包,其中0.1.0表示為當前pthreads版本號,5.4為php版本號,ts就是之前判斷php對應的ts、nts版,vs9代表是Visual Studio 2008 compiler編譯器編譯的,最后的x86代表的是32位的版本。
四、下載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類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<?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擴展
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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多線程擴展基礎教程,希望大家喜歡。