Exemplo n.º 1
1
    }
}
echo "Master main EP " . getmypid() . "\n";
$criticalSection = new GPhpThreadCriticalSection();
$criticalSection->cleanPipeGarbage();
// remove any garbage left from any ungracefully terminated previous executions
$threadPool = array();
$tpSize = 20;
for ($i = 1; $i <= $tpSize; ++$i) {
    $threadPool[$i] = new MyThread($criticalSection, true);
}
// xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_NO_BUILTINS);
GPhpThread::BGN_HIGH_PRIOR_EXEC_BLOCK();
// this will result fast thread creation but, because of that all threads will try
// to reach the critical section at once in some cases may take twice the time for each one in order to lock the critical section
for ($i = 1; $i <= $tpSize; ++$i) {
    $threadPool[$i]->start();
    echo "{$i} of {$tpSize} started\n";
}
GPhpThread::END_HIGH_PRIOR_EXEC_BLOCK();
for ($i = 1; $i <= $tpSize; ++$i) {
    $threadPool[$i]->join();
}
// $xhprof_data = xhprof_disable();
echo "\n\n---The last writing in the critical section was done by thread---\n";
echo "---" . $criticalSection->getResourceValueFast('IAM') . "\n\n";
//uasort($xhprof_data, function ($a, $b) {
//	if ($a['wt'] > $b['wt']) return -1;
//	else return 1;
//});
// print_r($xhprof_data);
Exemplo n.º 2
0
    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);
var_dump($thr1->start());
var_dump($thr1->start());
echo "Thread1 pid is: " . $thr1->getPid() . "\n";
$thr1->join();
echo "I was " . $criticalSection->getResourceValueFast('IAM') . "\n";
echo "\nRecycling thread instance...\n\n";
var_dump($thr1->start());
var_dump($thr1->start());
$thr1->join();
echo "I was " . $criticalSection->getResourceValueFast('IAM') . "\n";
Exemplo n.º 3
0
            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);
$thr1->start();
$thr1->join();
echo "\n---Thread1 id was: " . $criticalSection->getResourceValueFast('IAM') . "---\n";
echo "Master after the join of Thread1.\n\n";
echo "\nLaunching Thread2...\n\n";
$thr2 = new MyThread($criticalSection, true);
$thr2->start();
$thr2->join();
echo "---Thread2 id was: " . $criticalSection->getResourceValueFast('IAM') . "---\n";
echo "Master after the join of Thread2.\n\n";
echo "\n\nThe resources that left in the critical section:\n";
var_dump($criticalSection->getResourceNames());
$thr1 = null;
$thr2 = null;