I am lately working with PHP’s pthreads extension for one of my applications. I needed a chunk on my code to run parallelly instead of it running inside a loop because each iteration was time-consuming and blocking. As each iteration was independent of other, I decided to use PHP’s pthreads extension and wrapped my loop’s code chunk inside a Threaded class’ run method for quick test. My code failed to run. When I looked for the cause, I was bit surprised.

My PHP code made use of relative paths. But after wrapping it inside run() method, relative path’s calculation was failing. Reason? It was calculating relative path from wrong directory. I checked working directory of the thread and it was not the directory where I run my script from, but it was inside Apache’s installation directory! I presume, each thread created using PHP’s pthreads extension should inherit the working directory of the script that started the thread, this should be the intended behavior. I could be wrong.

Here is an example that will help you understand the issue:

<?php class ThreadExample extends Thread { public function run() { echo "

Thread's Working Directory: ".getcwd(); } } echo "

Script's Working Directory: ".getcwd(); $oThread = new ThreadExample(); $oThread->start(); $oThread->join(); ?>

Thread's Working Directory: C:\wamp\bin\apache\apache2.4.9 Script's Working Directory: C:\wamp\www\Thread

I am going to raise a bug ticket anyway. Feel free to post your thought on this in comments.