{ echo "destr __ " . getmypid() . " \n"; debug_print_backtrace(); echo "___________________________\n"; } } $g = new MyStuff(); class MyThread extends GPhpThread { public function run() { echo 'Hello, I am a thread with id ' . $this->getPid() . "!\nTrying to lock the critical section\n"; $lock = new GPhpThreadLockGuard($this->criticalSection); echo "=--- locked " . $this->getPid() . "\n"; $this->criticalSection->addOrUpdateResource('IAM', $this->getPid()); $this->criticalSection->addOrUpdateResource('IAMNOT', '0xdead1'); $this->criticalSection->removeResource('IAMNOT'); echo "=--- unlocked " . $this->getPid() . "\n"; $this->setExitCode(8); } } 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, false); $thr1->start(); echo "Thread1 pid is: " . $thr1->getPid() . "\n"; $thr1->join(); echo "Thread 1 exit code: " . $thr1->getExitCode() . "\n";
// 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();
{ echo 'Hello, I am a thread with id ' . $this->getPid() . "!\nTrying to lock the critical section\n"; $lock = new GPhpThreadLockGuard($this->criticalSection); echo "=--- locked " . $this->getPid() . "\n"; $this->criticalSection->addOrUpdateResource('IAM', $this->getPid()); $criticalSection2 = new GPhpThreadCriticalSection(); $a = new MyThreadThread($criticalSection2, true); $b = new MyThreadThread($criticalSection2, true); $a->start(); $b->start(); $a->join(); $b->join(); $criticalSection2->lock(); echo "=###= Last internal cs value: " . $criticalSection2->getResourceValue('IAM') . "\n"; $criticalSection2->unlock(); $this->criticalSection->addOrUpdateResource('IAMNOT', '0xdead1'); $this->criticalSection->removeResource('IAMNOT'); echo "=--- unlocked " . $this->getPid() . "\n"; GPhpThread::resetThreadIdGenerator(); $ms2 = new MyStuff('heavy thread 1'); } } 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); $thr1->start(); echo "Thread1 pid is: " . $thr1->getPid() . "\n"; $thr1->join();
public function run() { /* read once, see above ... */ if ($storage = $this->storage) { $this->stored = $storage->getUniqueId(); $storage->addToObject($this->stored, array(rand() * 120)); /* * This stackable, being executed by a worker to manipulate a global object, created in another thread ... * Is now going to create a thread and pass it a reference to the global object * The thread created by the stackable executing in the worker can reference the same objects * that all the things I just said can ... */ $thread = new MyThread($storage); if ($thread->start()) { $thread->join(); $this->tstored = $thread->getStorageId(); /* @NOTE doesnt matter what reference to the stackable is passed to the thread */ } } }
{ $this->value += $inc; } public function getValue() { return $this->value; } public function run() { } } class MyThread extends Thread { public $sum; public function __construct(Sum $sum) { $this->sum = $sum; } public function run() { for ($i = 0; $i < 10; $i++) { $this->sum->add(5); echo $this->sum->getValue() . " "; } } } $sum = new Sum(); $thread = new MyThread($sum); $thread->start(); $thread->join(); echo $sum->getValue();
{ public $array; public $num_min; public $num_max; public function __construct($array, $num_min, $num_max) { $this->array = $array; $this->num_min = $num_min; $this->num_max = $num_max; } public function run() { for ($i = $this->num_min; $i <= $this->num_max; ++$i) { $this->array[] = $i; usleep(1000); } } } $array = new SharedArray(); $thread_a = new MyThread($array, 0, 49); $thread_b = new MyThread($array, 50, 99); $thread_a->start(); $thread_b->start(); $thread_a->join(); $thread_b->join(); foreach ($array as $i) { echo "{$i}\n"; } ?>
<?php //header("Content-Type:text/html;charset=utf-8"); class MyThread extends Thread { private $args; public function __construct($args) { $this->args = $args; } public function run() { echo $this->args; } } $myThread = new MyThread("thread !"); if ($myThread->start()) { $myThread->join(); }