You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.
I know that here was many times discussed about segmentation fault but I didnt found solution to my problem.
I have 8 threads who share same counter. I call thread like this:
$counter = new Counter();
$jobs = array(
new Process($name, $trigger, $counter),
new Process($name, $trigger, $counter),
new Process($name, $trigger, $counter),
new Process($name, $trigger, $counter),
new Process($name, $trigger, $counter),
new Process($name, $trigger, $counter),
new Process($name, $trigger, $counter)
);
foreach ($jobs as $job)
$job->start();
foreach ($jobs as $job)
$job->join();
First, main problem is that sometimes after 1000,2000 loops I get segmentation fault error.
Second if I don't get that error, last echo of $this->counter->value is over 6000 (mostly 6002,6003), and I have duplicate numbers echoing. For example I see in log 2,3 times same number.
Counter class
class Counter extends Threaded {
public function __construct($value = -1) {
$this->value = $value;
}
protected function increment() { return ++$this->value; }
protected function decrement() { return --$this->value; }
protected $value;
}
Thread class
class Process extends Thread {
protected $counter;
public function __construct($name, $trigger, Counter $counter )
{
$this->name = $name;
$this->trigger = $trigger;
$this->counter = $counter;
}
public function run() {
$address = new Link();
while (($job = $this->counter->increment() <= 6000))
{
echo "\n " . $this->counter->value;
$address->check_name($this->name, $this->trigger);
}
}
}
I am pretty new in php Threads and PHP CLI scripts. So I am very sorry if this is some dumb question.
The text was updated successfully, but these errors were encountered:
I need reproducing code to be able to investigate.
The code in Process::run is not synchronized with counter, there is nothing stopping any thread changing the value of the counter between counter::increment and the echo statement, which is what is happening to that.
I know that here was many times discussed about segmentation fault but I didnt found solution to my problem.
I have 8 threads who share same counter. I call thread like this:
First, main problem is that sometimes after 1000,2000 loops I get segmentation fault error.
Second if I don't get that error, last echo of $this->counter->value is over 6000 (mostly 6002,6003), and I have duplicate numbers echoing. For example I see in log 2,3 times same number.
Counter class
Thread class
I am pretty new in php Threads and PHP CLI scripts. So I am very sorry if this is some dumb question.
The text was updated successfully, but these errors were encountered: