PHP_Fork
[ class tree: PHP_Fork ] [ index: PHP_Fork ] [ all elements ]

Source for file basic.php

Documentation is available at basic.php

  1. <?php
  2. /**
  3.  * PHP_Fork class usage examples
  4.  * ==================================================================================
  5.  * NOTE: In real world you surely want to keep each class into
  6.  * a separate file, then include() it into your application.
  7.  * For this examples is more useful to keep all_code_into_one_file,
  8.  * so that each example shows a unique feature of the PHP_Fork framework.
  9.  * ==================================================================================
  10.  * basic.php
  11.  *
  12.  * This is the most basic example I can think about...
  13.  * Simply increment an internal counter every second, printing it to
  14.  * stdout. Repeat this 10 times, then exit. The class extends PHP_Fork, so we have
  15.  * multiple instances running cuncurrently, as shown by the timestamp of each thread.
  16.  *
  17.  * ==================================================================================
  18.  *
  19.  */
  20.  
  21. // Import of base class
  22. require_once ("PHP/Fork.php");
  23.  
  24. // number of executeThreads we want
  25. define ("NUM_THREAD"2);
  26.  
  27. // Class definition; this class have a very basic purpose,, it simply
  28. // has an inernal counter and increment it every each second...
  29. class executeThread extends PHP_Fork {
  30.     var $counter;
  31.  
  32.     function executeThread($name)
  33.     {
  34.         $this->PHP_Fork($name);
  35.         $this->counter = 0;
  36.     }
  37.  
  38.     function run()
  39.     {
  40.         $i = 0;
  41.         while ($i < 10{
  42.             print time("-(" $this->getName(")-" $this->counter++ . "\n";
  43.             sleep(1);
  44.             $i++;
  45.         }
  46.     }
  47. }
  48.  
  49. // Main program. Bring up NUM_THREAD instances of the executeThread class that
  50. // runs concurrently. It's a multi-thread app with a few lines of code!!!
  51. for ($i = 0;$i NUM_THREAD;$i++{
  52.     $executeThread[$i= new executeThread ("executeThread-" $i);
  53.     $executeThread[$i]->start();
  54.     echo "Started " $executeThread[$i]->getName(" with PID " $executeThread[$i]->getPid("...\n";
  55. }
  56.  
  57. echo "\nThis is the main process.\nNothing to do, so exit...\n";
  58.  
  59. ?>

Documentation generated on Mon, 11 Mar 2019 15:41:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.