title: pthreads access to resource globally in PHP
body: <p>I'm having problems with accessing resources from global context using php pthreads for Windows.</p>
  
  <p>The logic is very simple: there's a Server as a main class, only one, which will:</p>
  
  <ul>
  <li>create logfile</li>
  <li>create couple of threads</li>
  <li>each thread will need to write something to this logfile</li>
  </ul>
  
  <p>Problem is, the resource of logfile handle is somehow totally messed up from inside the thread. When I create the resource, it's fine, I can write in it. When I try to call the log from inside the running thread, the resource of logfile handler appears to be integer 0, not even a resource.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>$main_server = new CMainServer();
  $main_server-&gt;init();
  $main_server-&gt;go();
  
  $main_server-&gt;log("All done");
  </code></pre>
  
  <p>Inside the CMainServer class:</p>
  
  <pre><code>class CMainServer
  {
    private $logfile = null;
  
    public function init()
    {
      $this-&gt;logfile = fopen('wstext.log', 'w');
    }
  
    public function log($str)
    {
      if ($this-&gt;logfile === null)
      {
        echo "[".date("H:i:s", time())."]: logfile is null&lt;BR /&gt;";
        return false;
      }
  
      if (!is_resource($this-&gt;logfile))
      {
        echo "[".date("H:i:s", time())."]: logfile is NOT a resource, can't write {$str}&lt;BR /&gt;";
        return false;
      }
  
      echo "[".date("H:i:s", time())."]: logfile is resource, not null, writing {$str}&lt;BR /&gt;";
      flush();
  
      fwrite($this-&gt;logfile, "[".date("H:i:s", time())."]: {$str}\r\n");
      return true;
    }
  
    public function go()
    {
      $this-&gt;log('Before creating a thread');
  
      $first_thread = new CThread();
      $first_thread-&gt;start(PTHREADS_INHERIT_ALL | PTHREADS_ALLOW_GLOBALS);
      $second_thread = new CThread();
      $second_thread-&gt;start(PTHREADS_INHERIT_ALL | PTHREADS_ALLOW_GLOBALS);
      $first_thread-&gt;join();
      $second_thread-&gt;join();
    }
  
    public function __destruct()
    {
      if ($this-&gt;logfile)
        fclose($this-&gt;logfile);
    }
  }
  </code></pre>
  
  <p>And, finally, the CThread class:</p>
  
  <pre><code>class CThread extends Thread
  {
    public function run()
    {
      global $main_server;
      $thread_id = $this-&gt;getThreadId();
  
      Thread::globally(function()
      {
        for ($i = 0; $i &lt; 2; $i++)
        {
          $main_server-&gt;log("({$i}) writing random number ".rand(0, 100)." to log from running thread id={$thread_id}");
          sleep(1);
        }
      });
    }
  }
  </code></pre>
  
  <p>Result is sad:</p>
  
  <pre><code>[13:38:10]: logfile is NOT a resource, can't write (0) writing random number 21 to log from running thread id=9080
  [13:38:11]: logfile is NOT a resource, can't write (1) writing random number 91 to log from running thread id=9080
  [13:38:10]: logfile is NOT a resource, can't write (0) writing random number 16 to log from running thread id=17316
  [13:38:11]: logfile is NOT a resource, can't write (1) writing random number 50 to log from running thread id=17316
  [13:38:10]: logfile is resource, not null, writing Before creating a thread
  [13:38:12]: logfile is resource, not null, writing All done
  </code></pre>
  
  <p>So while I'm outside the thread, all is fine. However, from within a thread the $logfile is not a resource at all.</p>
  
  <p>I tried different options: tried calling from CThread::run() a global function:</p>
  
  <pre><code>function LogFromThread($i, $thread_id)
  {
    global $main_server;
  
    $main_server-&gt;log("({$i}) writing random number ".rand(0, 100)." to log from running thread id={$thread_id}");
  }
  </code></pre>
  
  <p>The result is the same.</p>
  
  <p>Tried without Thread::globally() at all, but all for no good.</p>
  
  <p>I'm running <code>Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3</code>, tried pthreads version 2.0.8, 2.0.9. Also tried with PHP 7RC2 and RC3, but there seems to be a problem to start a new thread at all, apache logs an error, so I returned to 5.6.3.</p>
  
  <p>Maybe someone could give me a hint out about this?</p>
  
  <p>Much appreciated! =)</p>