php pthreads 不会在 xampp 7.0.2 上启动

php pthreads won't launch on xampp 7.0.2

我已经安装了新的 xampp (7.0.2 atm)。我创建了 php-cli.ini,在那里添加了 pthread 扩展并将内存限制设置为 3 GB。但是当我尝试启动线程脚本时,我得到了这个:

PHP Fatal error:  Uncaught RuntimeException: cannot start my_thread, out of reso
urces in C:\xampp\htdocs\w\start_threads.php:160
Stack trace:
#0 C:\xampp\htdocs\w\start_threads.php(160): Thread->start()
#1 {main}
  thrown in C:\xampp\htdocs\w\start_threads.php on line 160

Fatal error: Uncaught RuntimeException: cannot start my_thread, out of resources
 in C:\xampp\htdocs\w\start_threads.php:160

(我正在使用 pthreds 3.1.5 x86) 我在这里做错了什么?谢谢!

本质上,这是由于pthread_create返回EAGAIN造成的:这意味着系统缺乏创建另一个线程的资源,或者系统限制了最大线程数(在一个进程或系统范围)已达到。

这可能是由两件事引起的,由于某些软件的设计方式,有目的地使用比一个进程可以同时处理的线程更多的线程,或者更有害的是,由于线程连接不够优雅.

如果您似乎只是偶尔遇到此类错误,则表明后者正在发生;请务必清理(明确加入)您已完成的线程,以使行为可预测。

我的 PHP 版本:7.2.6 x82 和线程:php_pthreads-3.1.6-7.2-ts-vc15-x86 我创建了 25 个线程,当创建第 21 个线程时发生了同样的错误。 我认为它只能创建 20 个线程。 所以我编辑了我的代码并且没有发生错误 我的代码:

class ReadAllFile extends Thread {

    public $folder;

    public function __construct($folder) {
        $this->folder = $folder;
    }

    public function run() {
        //code process
    }

}

$dir = "F:/sbd/sbdstore/20180606/";
$subFolders = scandir ( $dir );

$stack = array();

foreach ( $subFolders as $folder ) {

    if ($folder != '.' && $folder != '..') {

        $stack[] = new ReadAllFile ( $dir.$folder );
    }


}

$maxNumberOfThread = 20;
$numberOfRunning = 0;
$numberOfStack = count($stack);
$elementIsStarted = array();
$allElementIsProcess = false;

while(count($stack)){

    if($numberOfRunning <= $maxNumberOfThread && !$allElementIsProcess){

        for($i=0;$i<$numberOfStack;$i++){

            if(!in_array($i,$elementIsStarted)){

                $numberOfRunning++;
                $elementIsStarted[] = $i;
                $stack[$i]->start();

                if($i == $numberOfStack - 1){

                    $allElementIsProcess = true;

                }

                $i = $numberOfStack + 1;

            }

        }

    }else{

        foreach($elementIsStarted AS $element){

            if(isset($stack[$element]) && $stack[$element]->isRunning() !== true){

                unset($stack[$element]);
                $numberOfRunning--;

            }

        }

    }

} 

希望对您有所帮助。 对不起我的英语。

P/s: 如果我使用 PHP version: 7.2.6 x64 和 php_pthreads-3.1.6-7.2-ts-vc15-x64 那么就不会出现这个错误。 我认为 x64 分配更多内存。