0

I have installed fresh xampp (7.0.2 atm). I've created php-cli.ini, added pthread extension there and set memory limit to 3 gb. But when I'am trying to launch thread script I got this:

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

(I'am using pthreds 3.1.5 x86) What am I doing wrong here? Thank you!

2 Answers2

2

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.

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62
-1

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.

daklan
  • 1
  • 1
  • While this is a nice work-around, it doesn't really fix or solve anything. Also, since the original post doesn't mention any limit, it seems they are unable to launch *any* thread, meaning your answer might not be relevant – Sjon Jun 14 '18 at 07:37
  • @Sjon, I think this error occur because out of resource. In my case, it only can create 20 threads, so if I create more than 20 threads then it occur that error. Therefore, I only create 20 threads. And if I have more than 20 threads then I loop until have a thread finished then I start new thread. At less in my case that error don't occur. It is my ideal, but maybe my code not good perfomace ^^ – daklan Jun 15 '18 at 06:37