Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php  
  2.     class SleepThread extends Thread
  3.     {
  4.         public $c = 0;
  5.         public $hit = false;
  6.  
  7.         public function __construct($nap, $n)
  8.         {
  9.             $this->nap = $nap;
  10.             $this->thread_no = $n;
  11.         }
  12.  
  13.         public function run()
  14.         {
  15.             while (1)
  16.             {
  17.                 $r = mt_rand(0, 9);
  18.                 echo $r . " found in thread " . $this->thread_no . "\n";
  19.                 if ($r == 9)
  20.                 {
  21.                     $this->hit = true;
  22.                 }
  23.                 if ($this->hit)
  24.                 {
  25.                     $this->synchronized
  26.                     (
  27.                         function ()
  28.                         {
  29.                             $obj = new stdClass;
  30.                             $obj->message = "Test\n";
  31.                             echo "CHILD:  Hit... \n";
  32.                             $this->result = 'CHILD:  DONE in thread ' .  $this->thread_no . ' at ' . time();
  33.                             echo "CHILD:  Waiting for parent thread ... \n";
  34.                             $time = microtime();
  35.                             $this->wait();
  36.                             echo "CHILD:  Done waiting after " . (microtime() - $time) . " seconds\n";
  37.                             $this->hit = false;
  38.                             echo "CHILD:  Child thread continuing. Thread number is:  " . $this->thread_no . " You're a thread master.\n";
  39.                         }
  40.                     );
  41.                 }
  42.                 sleep($this->nap);
  43.             }
  44.         }
  45.     }
  46.    
  47.     $thread[0] = new SleepThread(5, "0");
  48.     $thread[1] = new SleepThread(5, "1");
  49.     foreach ($thread as $job)
  50.     {
  51.         $job->start();
  52.     }
  53.    
  54.     while (1)
  55.     {
  56.         sleep(1);
  57.         foreach ($thread as $job)
  58.         {
  59.             if ($job->hit)
  60.             {
  61.                 echo ("PARENT:  Detected a hit in thread " . $job->thread_no . "\n");
  62.                 $job->synchronized
  63.                 (
  64.                     function ($job)
  65.                     {
  66.                         echo "PARENT:  Notifying child thread that we are ready for its result.\n";
  67.                         $job->notify();
  68.                         echo "PARENT:  " . $job->result . "  <----That's the result.\n";
  69.                         // echo "PARENT:  " . $job->obj->message . "^^^Getting object created in child thread oesn't work for some rasons^^^.\n";
  70.                     },
  71.                     $job
  72.                 );
  73.             }
  74.         }
  75.     }
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement