问题
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!
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/35112678/php-pthreads-wont-launch-on-xampp-7-0-2