php pthreads won't launch on xampp 7.0.2

北战南征 提交于 2019-12-02 07:56:06

Essentially, this is caused by pthread_create returning EAGAIN: It means that the system lacks resources to create another thread, or that the system imposed limit on the maximum number of threads (in a process, or system wide) has been reached.

This can be caused by two things, the purposeful use of more threads than a process can handle simultaneously as a result of the way some software is designed, or more perniciously, as a result of less than graceful joining of threads.

If you only seem to hit such errors sometimes, it would suggest the latter is going on; Be sure to cleanup (explicitly join) threads you are done with to make behaviour predictable.

My PHP version: 7.2.6 x82 And pthreads: php_pthreads-3.1.6-7.2-ts-vc15-x86 I created 25 threads, when created 21th thread then occurred same error. I thought that it only can create 20 threads. So I edited my code and that error does not occur My code:

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--;

            }

        }

    }

} 

Hope this help. Sorry about my English.

P/s: If I use PHP version: 7.2.6 x64 and php_pthreads-3.1.6-7.2-ts-vc15-x64 then it does not occur this error. I think x64 allocation more memory.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!