PHP OOP Script using Multithreading, terminates unexpectedly -


hi i'm having problem trying threads perform properly. problem php script terminate unexpectedly @ 1 of 2 specific points, during execution, , works rest of time.

i've created test scenario produces same result:

this script creates 5 threads. each thread adds up, each iteration, of random number ranging 10 20.

secondly, summary_thread checks see when threads completed before printing summary... , there in lies issue.

the script terminates during summary_thread::run , stack_item_container_stack::compile_summary, indirectly called @ end of summary_thread::run.

#!/usr/bin/php <?php  ini_set('max_execution_time', 0); ini_set('display_errors', 1); error_reporting(e_all ^ e_notice); ignore_user_abort();  class summary_thread_container {     public $stack_item_container_stack;     private $summary_thread;      function __construct($thread_count, stack_item_container_stack $stack_item_container_stack)     {         $this->stack_item_container_stack = $stack_item_container_stack;          $this->summary_thread = new summary_thread($thread_count, $this);         $this->summary_thread->start();     }      public function compile_summary(){ $this->stack_item_container_stack->compile_summary(); } }  class summary_thread extends worker {     private $summary_thread_container;     private $thread_count;      function __construct($thread_count, summary_thread_container $summary_thread_container)     {         $this->summary_thread_container = $summary_thread_container;         $this->thread_count = $thread_count;     }      public function run()     {         $thread_count = 0;          echo "\n**************************************  stack thread count: {$this->thread_count} \n";          echo "*** start.\n";          if($this->thread_count == $thread_count)             echo "*** thread counts match.\n";         else             echo "*** thread counts not match.\n";          while($this->thread_count != $thread_count)         {             $temp_sic = $this->summary_thread_container->stack_item_container_stack->first_stack_item_container;             $thread_count = 0;              while($temp_sic)             {                 $thread_count++;                  echo "**************************************  thread count: {$thread_count} \n";                  $temp_sic = $temp_sic->get_next_stack_item_container();             }              echo "*** end.\n";              if($this->thread_count == $thread_count)                 echo "*** thread counts match.\n";             else                 echo "*** thread counts not match.\n";         }          $this->compile_summary();     }      public function compile_summary(){ $this->summary_thread_container->compile_summary(); } }  class stack_item_container_stack {     public $first_stack_item_container;     private $thread_count;     private $summary_thread_container;      function __construct()     {         $this->first_stack_item_container = null;         $this->thread_count = 0;          for($i = 0; $i < 5; $i++)         {             echo "       * creating stack item container: {$i}\n";              $this->thread_count++;             $this->add_stack_item_container(new stack_item_container(rand(10, 20), $i, $this));         }          $this->summary_thread_container = new summary_thread_container($this->thread_count, $this);     }      public function add_stack_item_container(stack_item_container $stack_item_container)     {         echo "       * adding stack item container *\n";          if($this->first_stack_item_container)         {             $temp_stack_item_container = $this->first_stack_item_container;              while($temp_stack_item_container->get_next_stack_item_container())                 $temp_stack_item_container = $temp_stack_item_container->get_next_stack_item_container();              $temp_stack_item_container->set_next_stack_item_container($stack_item_container);         }         else $this->first_stack_item_container = $stack_item_container;     }      public function compile_summary()     {         echo "\n";         echo "===============\n";         echo "=== summary ===\n";         echo "===============\n";         echo "\n";          $temp_sic = $this->first_stack_item_container;          while($temp_sic)         {             echo "    thread id {$temp_sic->member_variables[0]} ({$temp_sic->member_variables[4]}) has total of {$temp_sic->member_variables[2]}";             echo "\n";              $temp_sic = $temp_sic->get_next_stack_item_container();         }          echo "\n";          $this->kill();     }      private function kill()     {         while($this->first_stack_item_container)         {             $temp_sic = $this->first_stack_item_container;              $this->first_stack_item_container = $this->first_stack_item_container->get_next_stack_item_container();              $temp_sic->kill();         }          unset($this->summary_thread_container);         unset($this);     } }  class stack_item_container {     private $stack_item_container_stack;     private $next_stack_item_container;      public $member_variables;      public $stack_item_thread;      function __construct($time, $index, stack_item_container_stack $stack_item_container_stack)     {         $this->stack_item_container_stack = $stack_item_container_stack;         $this->next_stack_item_container = null;          $this->member_variables = new stackable();         $this->member_variables[] = -1;         $this->member_variables[] = $time;         $this->member_variables[] = 0;         $this->member_variables[] = false;         $this->member_variables[] = $index;          $this->stack_item_thread = new stack_item_thread($this->member_variables, $this);         $this->stack_item_thread->start();     }      public function get_stack_item_container_stack(){ return $this->stack_item_container_stack; }      public function get_next_stack_item_container(){ return $this->next_stack_item_container; }     public function set_next_stack_item_container(stack_item_container $next_sic){ $this->next_stack_item_container = $next_sic; }      public function kill()     {         $this->stack_item_thread->kill();         unset($this->member_variables);         unset($this);     } }  class stack_item_thread extends worker {     private $stack_item_container;     private $member_variables;      function __construct($member_variables, stack_item_container $stack_item_container)     {         $this->member_variables = $member_variables;         $this->stack_item_container = $stack_item_container;     }      public function run()     {         $this->member_variables[0] = $this->getthreadid();         $total = 0;          echo "\n";          for($i = 0; $i < $this->member_variables[1]; $i++)         {             $total += $i;             $val = $i + 1;              echo "thread id ({$this->member_variables[4]}): {$this->member_variables[0]}:";             echo " count: {$val} of {$this->member_variables[1]}";             echo "\n";         }          echo "\n";          $this->member_variables[2] = $total;         $this->member_variables[3] = true;     } }  $stack_item_container_stack = new stack_item_container_stack(); 

output 1 (when work):

**************************************  stack thread count: 5 *** start. *** thread counts not match. **************************************  thread count: 1 **************************************  thread count: 2 **************************************  thread count: 3 **************************************  thread count: 4 **************************************  thread count: 5 *** end. *** thread counts match.  =============== === summary === ===============      thread id 139975400195840 (0) has total of 105     thread id 139975389705984 (1) has total of 153     thread id 139975378360064 (2) has total of 153     thread id 139975367014144 (3) has total of 55     thread id 139975130801920 (4) has total of 153 

output 2: (the first point terminate):

**************************************  stack thread count: 5 *** start. *** thread counts not match. 

output 3: (the second point terminate)

**************************************  stack thread count: 5 *** start. *** thread counts not match. **************************************  thread count: 1 **************************************  thread count: 2 **************************************  thread count: 3 **************************************  thread count: 4 **************************************  thread count: 5 *** end. *** thread counts match. 

just give information can: (might not relevant in case)

changes made config:

file: /etc/sysctl.conf, changes made: net.ipv4.tcp_fin_timeout=10

file: php.ini, changes made: extension=php_pthreads.dll

server:

linux 2.6.32-504.8.1.el6.x86_64

php 5.5.13

apache/2.2.15 (centos)

max requests per child: 4000 - keep alive: off - max per connection: 100

timeouts connection: 60 - keep-alive: 15

virtual server no


please :) , questions ... i'm not seeing?

thanks in advance

i cant test myself, if php exits normaly without errors propably caused main thread exiting before other thread finish. try thread::join them (http://php.net/manual/en/thread.join.php) parent thread waits until finished.


Popular posts from this blog