我想用pthreads写一些PHP代码。对于此pthread,我需要知道有多少个 Activity 线程。因为下一个系统只能处理固定数量的请求。我可以计算已启动的线程,但无法在线程停止时减去。
我试图对线程对象进行计数,但我也没有减去。如果某个线程完成了他的工作,我该如何管理我获得有关启动脚本的信息?
最佳答案
我找到了一种获取有关运行和不运行线程的信息的方法。所以我可以数一下。如果有人有更好的解决方案,我很高兴看到它。但是首先,我想分享自己的解决方案。
#!/usr/bin/php
<?php
class AsyncOperation extends Thread
{
public function __construct($threadId)
{
$this->threadId = $threadId;
}
public function run()
{
printf("T %s: Sleeping 3sec\n", $this->threadId);
sleep(1);
printf("T %s: Hello World\n", $this->threadId);
$this->kill;
}
}
$a=0;
$start = microtime(true);
for ($i = 1; $i <= 5; $i++) {
$test[$i] = new AsyncOperation($i);
$test[$i]->start();
}
$arg=true;
while($arg){
$arg=false;
foreach($test as $key => $object){
// for ($i = 1; $i <= 5; $i++) {
$arg2=$object->isRunning();
if($arg2){
$arg=$arg2;
}else{
//var_dump($key);
unset ($test[$key]);
}
//var_dump($key);
if(!$arg){
var_dump($arg);
}
}
}
var_dump($test);
echo count($test)."\n";
echo "\n".microtime(true) - $start . "\n";
echo "end\n";
关于php - PHP pthreads计数事件线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24611576/