©
本文檔使用 php中文網手册 發布
[#1] r3x at engelhardt-stefan dot de [2015-11-17 06:17:20]
If you try to test threading, remember to let php think slow:
Skript: -- C:\Webserver\htdocs>php mttest.php
<?php
class My extends Thread{
function run(){
for($i=1;$i<10;$i++){
echo Thread::getCurrentThreadId() . "\n";
sleep(2); // <------
}
}
}
for($i=0;$i<2;$i++){
$pool[] = new My();
}
foreach($pool as $worker){
$worker->start();
}
foreach($pool as $worker){
$worker->join();
}
?>
Output: -- C:\Webserver\htdocs>php mttest.php
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
If you leave sleep() out, the cpu-time for the threads is long enough to complete the script at once.
[#2] 312036773 at qq dot com [2015-11-06 04:52:21]
My code is :
<?php
class My extends Thread{
function run(){
for($i=1;$i<10;$i++){
echo Thread::getCurrentThreadId() . "<br/>";
}
}
}
for($i=0;$i<2;$i++){
$pool[] = new My();
}
foreach($pool as $worker){
$worker->start();
$worker->join();
}
?>
The result is :
4076
4076
4076
4076
4076
4076
4076
4076
4076
5760
5760
5760
5760
5760
5760
5760
5760
5760
How can I make it run like below??? Thread switch?
4076
5760
4076
5760
4076
5760
4076
5760
4076
5760
5760
[#3] admin at deosnet dot com [2014-08-19 10:01:15]
Hello,
WARNING : When using Stackable objects in callable functions by your Threads, you must be very careful if you use it as an array. Indeed, if you do not copy your Stackable "array" in a local variable, the execution time can drop drastically !
Also, if you want to modify an array() in a function, you will also store in a local variable in order that your array is in a thread-safe context.
[#4] jasonrlester at yahoo dot com [2014-02-23 00:08:57]
Note that this extension *is* a high level implementation of POSIX threads, including on Windows (which is why pthreadsV*.dll is required)
[#5] Anonymous [2014-02-08 11:04:44]
This seriously should be renamed. People will interpret this as the widely known POSIX threads, which it's not. Just a heads up for anyone wanting to use "pthreads".