title: php pthreads and using a class to store data that is shared between threads
body: <p>I'm fairly new to PHP, and now very new to pthreads.</p>
  
  <p>I'm using the latest PHP7 RC6 build, with pthreads built from git/src to get the latest (and tried the 'official' v3.0.8 one), on Ubuntu 3.13.0-66-generic</p>
  
  <p>I'm trying to write a threaded solution to read in data from a socket and process it. I'm using threading to try to maximize my performance, mainly due to the fact I'm doing operations like http requests (to AWS DynamoDB and other services) and such that are waiting for responses from external systems, and therefore I can benefit from threading.</p>
  
  <p>The real code I have is more complicated than this is. This is a simple example to show my problem.</p>
  
  <p>What I am trying to do is to 'cache' certain information in an 'array' that I get from a database (AWS DynamoDB) so that I can get better performance. I need each thread to be able to use/access and modify this 'global' cache, and with multiple 'records' in the cache.</p>
  
  <p>I had great success with testing and simply storing a string in this way, but now I'm doing it for real, I need to store more complicated data, and I decided to use a little class (cacheRecord) for each record, instead of a simple string of data. But the problem is that when I try to assign a value back to a class member, it seems to not want to 'save', back to the array.</p>
  
  <p>I managed to get it to work by copying the whole 'class' to a tmp variable, modifying that, and then saving back the whole class to the array, but that seems like an overhead of code, and also I would need to wrap it in a ->synchronized to keep integrity between threads.</p>
  
  <p>Is this the only way to do it correctly, with copying it to a tmp and copying it back and using 'synchronized', or am I doing something else wrong/stupid?</p>
  
  <p>Experimenting with it, I made the cacheRecord class 'extends Threaded'. This made the single assign of the member work fine, but this then made it immutable, and I couldn't unset/delete that record in the cache later.</p>
  
  <p>Code to show what I mean:</p>
  
  <pre><code>&lt;?php
  
  class cacheRecord {
      public $currentPos;
      public $currentRoom;
      public $someOtherData;
  }
  
  class cache extends Threaded {
      public function run() {}
  }
  
  class socketThread extends Thread {
  
      public function __construct($myCache) {
          $this-&gt;cacheData = $myCache;
      }
  
      public function run() {
  
          // This will be in a loop, waiting for sockets, and then responding to them, indefinitely.
  
          // At some point, add a record to the cache
          $c = new cacheRecord;
          $c-&gt;currentPos = '1,2,4';
          $c-&gt;currentRoom = '2';
          $this-&gt;cacheData['record1'] = $c;
  
          var_dump($this);
  
          // Later on, update the cache record, but this doesnt work
          $this-&gt;cacheData['record1']-&gt;currentRoom = '3';
  
          var_dump($this);
  
          // However this does work, but is this the correct way? Seems like more code to execute, than a simple assign, and obviously, I would need to use synchronized to keep integrity, which would further slow it down.
  
          $tmp = $this-&gt;cacheData['record1'];
          $tmp-&gt;currentRoom = '3';
          $this-&gt;cacheData['record1'] = $tmp;
  
          var_dump($this);
  
          // Later on some more, remove the record
          unset($this-&gt;cacheData['record1']);
  
          var_dump($this);
  
          // Also will be using -&gt;synchronized to enforce integrity of certain other operations
          // Just an example of how I might use it
  
  /*
          $this-&gt;cacheData-&gt;synchronized(function() {
              if ($this-&gt;cacheData['record1']-&gt;currentRoom == '3') {
                  $this-&gt;cacheData['record1']-&gt;Pos = '0,0,0'; // Obviously this wont work as above.
                  $this-&gt;cacheData['record1']-&gt;currentRoom = '4';
              }
          });
  */
      }
  }
  
  // Main
  
  $myCache = new cache;
  
  for ($th=0;$th&lt;1;$th++) { // Just 1 thread for testing
      $socketThreads[$th] = new socketThread($myCache);
      $socketThreads[$th]-&gt;start();
  }
  </code></pre>