pthread Thread objects reset their state pthread Thread objects reset their state multithreading multithreading

pthread Thread objects reset their state


Any object not descended from a pthreads definition will be serialized upon setting it a member of an object descended from pthreads.

Operations like += and [] use pointers internally, serialization is incompatible with pointers for other objects. In the manual on the introduction page, it states that any object intended to be manipulated by multiple contexts should extend Stackable, Thread or Worker, like

<?phpclass Sum extends Stackable {    private $value = 0;    public function add($inc)  { $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();?>

If Sum weren't using pointers you would have the option of retrieving the reference from the threaded object after join.

These are simple operations, you are not required to synchronize. The only time you should synchronize is when you plan to wait on an object or notify one.

The objects bundled with pthreads are very much more suited to this environment and are never serialized.

Please do read the intro in the manual and all the examples in the methods you wish to utilize to find out exactly what is what, then feel free to ask why :)

I know that users of PHP aren't used to having to do research, but we are pushing the envelope here, you will find there are correct ways to do things an incorrect ways, most of them are documented in examples, and anything thats not I'm sure will be extracted from me on SO and eventually find it's way to the documentation.

I'm not sure if the example you gave was testing out objects in particular, but the code you provided need not be two objects, and shouldn't be two objects either, consider the following:

<?phpclass MyThread extends Thread {    public $sum;    public function run(){        for ($i=0; $i < 10; $i++) {            $this->add(5);            printf("%d ", $this->sum);        }    }    public function add($num) { $this->sum += $num; }    public function getValue() { return $this->sum; }}$thread = new MyThread();$thread->start();$thread->join();var_dump($thread->getValue());?>

It may be useful for you to see a couple more features in action with an explanation, so here goes, here's a similar example to yours:

<?phpclass MyThread extends Thread {    public $sum;    public function __construct() {        $this->sum = 0;    }    public function run(){        for ($i=0; $i < 10; $i++) {            $this->add(5);            $this->writeOut("[%d]: %d\n", $i, $this->sum);        }        $this->synchronized(function($thread){            $thread->writeOut("Sending notification to Process from %s #%lu ...\n", __CLASS__, $thread->getThreadId());            $thread->notify();        }, $this);    }    public function add($num) { $this->sum += $num; }    public function getValue() { return $this->sum; }    /* when two threads attempt to write standard output the output will be jumbled */    /* this is a good use of protecting a method so that         only one context can write stdout and you can make sense of the output */    protected function writeOut($format, $args = null) {        $args = func_get_args();        if ($args) {            vprintf(array_shift($args), $args);        }    }}$thread = new MyThread();$thread->start();/* so this is synchronization, rather than joining, which requires an actual join of the underlying thread *//* you can wait for notification that the thread is done what you started it to do *//* in these simple tests the time difference may not be apparent, but in real complex objects from *//* contexts populated with more than 1 object having executed many instructions the difference may become very real */$thread->synchronized(function($thread){    if ($thread->getValue()!=50) {        $thread->writeOut("Waiting for thread ...\n");        /* you should only ever wait _for_ something */        $thread->wait();        $thread->writeOut("Process recieved notification from Thread ...\n");    }}, $thread);var_dump($thread->getValue());?>

This combines some of the more advanced features in some simple examples, and is commented to help you along. On the subject of sharing objects, there's nothing wrong with passing around a Thread object if it contains some functionality and data required in other threads or stackables. You should aim to use as few threads and objects as possible in order to get the job done.


Your problem is that you are accessing the variable from the main thread and from the MyThread thread. The CPU caches the variable and it gets updated in the cache for MyThread but not in the cache for the main thread, so your both threads never see the others thread changes. In Java / C etc. there is the keyword volatile but I don't know if that exists in PHP.

I think you should try to call the methods in sum synchronized ( http://php.net/manual/en/thread.synchronized.php )

For example, instead of:

        $this->sum->add(5);

Call:

        $this->synchronized(function($thread){            $thread->sum->add(5);        }, $this);


I thought I am doing something wrong too. As mentioned [] and other operations using pointers will not work. This is the workaround:

class MyThread extends Thread {    public $table;    public function __construct($data) {        $this->table= $data;    }    public function run(){        //Set temporary array with current data        $temp = $this->table;        for ($i=0; $i < 10; $i++) {             //Add new elements to the array            $temp[] = $i . ' New Value';        }        //Overwrite class variable with temporary array        $this->table = $temp;    }}

This works as we are using [] operation on a temporary function variable and than we set it as class variable.

Better example why is it necessary:

class MyThread extends Thread {        public $table;        public function __construct($data) {            $this->table= $data;        }        public function run(){            $this->addElem();            $temp = $this->table;            $temp[] = 1;            $this->table = $temp;        }        protected function addElem() {            $temp = $this->table;            $temp[] = $i . ' New Value';            $this->table = $temp;        }    }

So we can use and add new elements to the variable in more than one function.The same will work for += and other operators, but you need the temporary variable for it.

Easy to extend function for this:

protected function overload($name, $val, $type = '[]'){        $temp = $this->$name;        switch($type){            case '[]':                $temp[] = $val;                break;            case '+=':                $temp += $val;                break;            case '.=':                $temp .= $val;                break;        }        $this->$name = $temp;    }

Eval could work here but I find this much safer.