Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SleepThread extends Thread
- {
- public $hit = false;
- public function __construct($nap, $n)
- {
- $this->nap = $nap;
- $this->thread_no = $n;
- }
- public function run()
- {
- while (1)
- {
- $r = random_int(0, 9);
- echo $r . " \tdrawn in thread " . $this->thread_no . "\n";
- if ($r == 9)
- {
- $this->hit = true;
- $this->synchronized
- (
- function ()
- {
- $job = new stdClass;
- $job->message = "Test\n";
- echo "*CHILD:\tHit... \n";
- $this->result = 'Found in thread ' . $this->thread_no . ' at ' . time() . ". You're a thread master...";
- echo "*CHILD:\tWaiting for parent thread ... \n";
- $time = microtime();
- $this->wait();
- echo "*CHILD:\tDone waiting after " . (microtime() - $time) . " seconds\n";
- $this->hit = false;
- echo "*CHILD:\tChild " . $this->thread_no . " taking a long nap...\n";
- sleep(3 * $this->nap);
- echo "*CHILD:\tChild thread continuing. Thread number is: " . $this->thread_no . "...\n";
- }
- );
- }
- sleep($this->nap);
- }
- }
- }
- $thread[0] = new SleepThread(5, "0");
- $thread[1] = new SleepThread(5, "1");
- $thread[2] = new SleepThread(5, "2");
- foreach ($thread as $job)
- {
- $job->start();
- }
- while (1)
- {
- sleep(1);
- foreach ($thread as $job)
- {
- if ($job->hit)
- {
- echo ("PARENT:\tDetected a hit in thread " . $job->thread_no . "\n");
- $job->synchronized
- (
- function ($job)
- {
- echo "PARENT:\tNotifying child thread that we are ready for its result.\n";
- $job->notify();
- echo "PARENT:\t" . $job->result . " <----That's the result.\n";
- // echo "PARENT: " . $job->obj->message . "^^^Getting object created in child thread oesn't work for some rasons^^^.\n";
- },
- $job
- );
- }
- }
- }
- /*
- 3 drawn in thread 0
- 3 drawn in thread 1
- 0 drawn in thread 2
- 0 drawn in thread 0
- 0 drawn in thread 1
- 9 drawn in thread 2
- *CHILD: Hit...
- *CHILD: Waiting for parent thread ...
- PARENT: Detected a hit in thread 2
- PARENT: Notifying child thread that we are ready for its result.
- PARENT: Found in thread 2 at 1542168674. You're a thread master... <----That's the result.
- *CHILD: Done waiting after 0.00082700000000002 seconds
- *CHILD: Child 2 taking a long nap...
- 8 drawn in thread 0
- 0 drawn in thread 1
- 5 drawn in thread 0
- 9 drawn in thread 1
- *CHILD: Hit...
- *CHILD: Waiting for parent thread ...
- 4 drawn in thread 0
- *CHILD: Child thread continuing. Thread number is: 2...
- PARENT: Detected a hit in thread 1
- PARENT: Notifying child thread that we are ready for its result.
- PARENT: Found in thread 1 at 1542168684. You're a thread master... <----That's the result.
- *CHILD: Done waiting after 0.004718 seconds
- *CHILD: Child 1 taking a long nap...
- 6 drawn in thread 0
- 7 drawn in thread 2
- 2 drawn in thread 0
- 5 drawn in thread 2
- 3 drawn in thread 0
- 2 drawn in thread 2
- *CHILD: Child thread continuing. Thread number is: 1...
- 1 drawn in thread 0
- 5 drawn in thread 2
- 5 drawn in thread 1
- */
- ?>
Add Comment
Please, Sign In to add comment