Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Segmentation fault, shared counter error #371

Closed
Gramzivi opened this issue Nov 16, 2014 · 5 comments
Closed

Segmentation fault, shared counter error #371

Gramzivi opened this issue Nov 16, 2014 · 5 comments

Comments

@Gramzivi
Copy link

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.

@krakjoe
Copy link
Owner

krakjoe commented Nov 16, 2014

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.

@Gramzivi
Copy link
Author

removed code

@krakjoe
Copy link
Owner

krakjoe commented Nov 16, 2014

I'm not going to debug a 2000 line file with globals in it, please reproduce the error with simple code.

You need to reduce the problem to the simplest possible case and use that code to open a bug report.

@krakjoe krakjoe closed this as completed Nov 16, 2014
@Gramzivi
Copy link
Author

I found solution here:
#33

@krakjoe
Copy link
Owner

krakjoe commented Nov 17, 2014

Glad you got it sorted :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants