Q:

PHP - PTHREAD Worker Threads not executing concurrently

I am working on improving a process in php so I resorted into using multithreading using Worker to manage my threads. But the threads in the worker executes one after the other not concurrently.
This is my sample code
namespace App\Engine\Threads\ThreadClass;

class StatePaperAttendancePDFThread extends \Threaded
{
   private $write_folder_name;
   private $center_code;
   private $paper_id;
   private $title;
   public function __construct($write_folder_name, $center_code, $paper_id, $title)
{
    $this->write_folder_name = $write_folder_name;
    $this->center_code = $center_code;
    $this->paper_id = $paper_id;
    $this->title = $title;

}

public function run(){

    echo " Retrieving paper attendance ".$this->center_code." ".$this->paper_id." ".\Thread::getCurrentThreadId()." ".date("H:i:s:u").PHP_EOL;
    $artisan = 'C:/xampp/htdocs/attendance/artisan';
    $cmd = "php $artisan attendancereport:center $this->write_folder_name $this->center_code $this->paper_id $this->title";
    $return = exec($cmd, $output, $return_var);
    echo $return;

 }
}

 foreach ($centers as $i=>$center){
        $center_code = $center->getCenterCode();
        $thread = new StatePaperAttendancePDFThread($folder_name, $center_code, $paper_id, $title);
        $worker->stack($thread);
    }

 $worker->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);
 $worker->shutdown();

but when I monitor it from the CLI using the time been printed I can see that none of the threads starts together. They all starts with some seconds interval
Please what am I missing

A:

I was able to solve my problem by creating a customized Thread Pool Executor as below. Please I am open to suggestions and Improvement
class ThreadPoolExecutor{
   private $poolSize;
   private $threadPool;
   private $done = false;
   private $workingThreads;

public function __construct($poolSize, array $threadPool)
{
    $this->poolSize = $poolSize;
    $this->threadPool = $threadPool;
}

public function execute()
{
    $this->parametersOk();

    try {
        while (!empty($this->threadPool)) {
            $this->extractThreads();

            foreach ($this->workingThreads as $thread) {
                $thread->start(PTHREADS_INHERIT_ALL ^ PTHREADS_INHERIT_CLASSES);
            }

            foreach ($this->workingThreads as $thread) {
                $thread->join();
            }

        }
        $this->done = true;
    } catch (\Exception $ex) {

        var_dump($ex->getMessage());
    }

}

private function parametersOk()
{
    if (!is_array($this->threadPool))
        throw new \RuntimeException("threadPool expected to be an array of threads");
    if (count($this->threadPool) <= 0)
        throw new \RuntimeException("expected at least an element in threadPool");

    foreach ($this->threadPool as $thread) {
        if (!is_subclass_of($thread, \Thread::class, false)) {
            throw new \RuntimeException(" an element of threadPool does not extend class \\Thread");
        }

    }

    if ($this->poolSize > count($this->threadPool)) {
        throw new \RuntimeException("The number of threads set to execute can not be greater than the threadPool");
    }

    return true;
}

private function extractThreads()
{
    $this->workingThreads = [];
    $this->workingThreads = array_slice($this->threadPool, 0, $this->poolSize);

    for ($i = 0; $i < count($this->workingThreads); $i++) {
        array_shift($this->threadPool);
    }
}

public function isDone()
{
    return $this->done;
}

}

I will appreciate any addition or correction to this.

