// Simple Demonstration how use GPhpThread require_once __DIR__ . '/../GPhpThread.php'; class MyThread extends GPhpThread { public function run() { echo 'Hello, I am a thread with id ' . $this->getPid() . "!\nTrying to lock the critical section\n"; if ($this->criticalSection->lock()) { echo "=--- locked " . $this->getPid() . "\n"; $this->criticalSection->addOrUpdateResource('IAM', $this->getPid()); $this->criticalSection->addOrUpdateResource('IAMNOT', '0xdead1'); $this->criticalSection->removeResource('IAMNOT'); while (!$this->criticalSection->unlock()) { $this->sleep(200000); } echo "=--- unlocked " . $this->getPid() . "\n"; } } } echo "Master main EP " . getmypid() . "\n"; $criticalSection = new GPhpThreadCriticalSection(); $criticalSection->cleanPipeGarbage(); // remove any garbage left from any ungracefully terminated previous executions echo "\nLaunching Thread1...\n\n"; $thr1 = new MyThread($criticalSection, true); $thr2 = new MyThread($criticalSection, true); $thr1->start(); echo "Thread1 pid is: " . $thr1->getPid() . "\n"; $thr2->start(); $thr2->join(); $thr1->join();
// Simple Demonstration how use GPhpThread require_once __DIR__ . '/../GPhpThread.php'; class MyThread extends GPhpThread { public function run() { echo 'Hello, I am a thread with pid ' . $this->getPid() . "!\n"; $this->sleep(0, 2); // 2 seconds } } echo "Master process pid is " . getmypid() . "\n"; echo "Creating threads...\n"; $thr1 = new MyThread($nothing = null, true); $thr2 = new MyThread($nothing = null, true); echo "\nLaunching Thread1...\n\n"; $thr1->start(); echo "Thread1 pid is: " . $thr1->getPid() . "\n"; echo "\nLaunching Thread2...\n\n"; $thr2->start(); echo "Thread2 pid is: " . $thr2->getPid() . "\n"; echo "Waiting for the threads to finish...\n"; while (!$thr1->join(false)) { // non blocking join echo "Thread1 is not done yet!\n"; usleep(500000); } echo "Thread1 is done!\n"; $thr2->join(); // blocking join echo "Thread2 is done!\n";