मैं साझा ऑब्जेक्ट का उपयोग करने के लिए एक सुरक्षित और तेज़ तरीका खोज रहा हूं।PHP pthreads - साझा वस्तुओं
मैंने पहले से ही प्रश्न पूछा: https://github.com/krakjoe/pthreads/issues/470 लेकिन obviuously यह सही जगह नहीं थी।
कई अन्य संदर्भों (थ्रेड) के साथ ऑब्जेक्ट (थ्रेडेड) साझा करने का प्रयास कर रहा है। सभी धागे इस शर्ड ऑब्जेक्ट को अपडेट कर रहे हैं - वे अपने अनुरोध सेट कर सकते हैं और दूसरों से अनुरोधों का जवाब भी दे सकते हैं।
अब क्रैकोजो ने जवाब दिया कि लॉक/अनलॉक 7 में उपलब्ध नहीं होगा मुझे कोई समस्या है।
मुझे पता है: सिंक्रनाइज़ किया गया है, लेकिन मुझे यह नहीं पता कि यह मेरी आवश्यकताओं के लिए काम करने के लिए इसका उपयोग कैसे करें।
मैं कैसे उपयोग कर सकते हैं :: तरह
- ताला()
- अनलॉक() पद्धतियों लिखने के लिए सिंक्रनाइज़
- is_locked() - जाँच करता है, तो बंद कर दिया, और यदि हां न कोशिश - बस जाओ पर और बाद में
संपादित करें का प्रयास करें:
मैं एक (IMO) बहुत आसान परीक्षण पटकथा लिखी।
इस स्क्रिप्ट में कोई syc/lock/... विधियां एटीएम शामिल हैं।
यह दिखाना चाहिए कि मैं क्या करने की कोशिश कर रहा हूं।
अभी भी यह साझा करने के लिए एक तरीका खोज रहा है :: यह साझा सुरक्षित बनाने के लिए।
कोड:
<?php
/*
TEST:
create n threads
each will
- Shared::set() its own ref
- check if Shared::exists() its own ref
- Shared::get() its ref back
- call method ::isRunning() at returned val to easily check if is ref or got overwritten by another context
TODO:
using ::synchronized to handle multi-context-access
NOTES:
every method as public to prevent pthreads v2 "Method Modifiers - Special Behaviour"
see: "Method Modifiers - Special Behaviour"
at http://blog.krakjoe.ninja/2015/08/a-letter-from-future.html
*/
class Shared extends Threaded
{
public $data;
public function exists($ident)
{
return isset($this->data[$ident]);
}
public function set($ident, $ref)
{
$return = false;
if(!isset($this->data[$ident])){
$data = $this->data;
$data[$ident] = $ref;
$this->data = $data;
$return = $this->data[$ident];
}
#echo __METHOD__ . '(' . $ident . ') => ' . gettype($return) . PHP_EOL;
return $return;
}
public function get($ident)
{
$return = false;
if($this->exists($ident) === true){
$data = $this->data;
$return = $data[$ident];
unset($data[$ident]);
$this->data = $data;
}
#echo __METHOD__ . '(' . $ident . ') => ' . gettype($return) . PHP_EOL;
return $return;
}
}
class T extends Thread
{
public $count;
public function __construct(Shared $Shared, $ident)
{
$this->Shared = $Shared;
$this->ident = $ident;
}
public function run()
{
$slowdown = true;
$this->count = 0;
while(true){
if($slowdown){
// "don't allow usleep or sleep" : https://github.com/krakjoe/pthreads/commit/a157b34057b0f584b4db326f30961b5c760dead8
// loop a bit to simulate work:
$start = microtime(true);
$until = rand(1, 100000)/1000000;
while(microtime(true)-$start < $until){
// ...
}
}
if($this->Shared->exists($this->ident) === true){
$ref = $this->Shared->get($this->ident);
}
else{
$ref = $this->Shared->set($this->ident, $this);
}
// calling a method on $ref -- if not a ref we crash
$ref->isRunning();
unset($ref);
$this->count++;
}
}
}
echo 'start ...' . PHP_EOL;
$n = 8;
$Shared = new Shared();
for($i = 0, $refs = array(); $i < $n; $i++){
$refs[$i] = new T($Shared, $i);
$refs[$i]->start();
}
while(!empty($refs)){
// print status:
if(!isset($t)or microtime(true)-$t > 1){
$t = microtime(true);
echo 'status: ' . count($refs) . ' running atm ...' . PHP_EOL;
}
// join crashed threads:
foreach($refs as $i => $thread){
if($thread->isRunning() === false){
echo 'T-' . $i . ' stopped after ' . $thread->count . PHP_EOL;
if($thread->isJoined() === false){
$thread->join();
}
unset($refs[$i]);
}
}
}
echo 'no thread running anymore.' . PHP_EOL;
/* output
start ...
status: 8 running atm ...
Notice: Undefined offset: 6 in ...\shared_test.php on line 33
Fatal error: Call to a member function isRunning() on null in ...\shared_test.php on line 82
T-6 stopped after 10
status: 7 running atm ...
Notice: Undefined offset: 4 in ...\shared_test.php on line 33
Fatal error: Call to a member function isRunning() on null in ...\shared_test.php on line 82
T-4 stopped after 35
status: 6 running atm ...
Notice: Undefined offset: 7 in ...\shared_test.php on line 33
Fatal error: Call to a member function isRunning() on null in ...\shared_test.php on line 82
T-7 stopped after 43
status: 5 running atm ...
status: 5 running atm ...
status: 5 running atm ...
[...]
*/
?>
यह एक अच्छा सवाल है – r3wt