When I run the following code, the individual Threads seems to be executed in sequence, one at the time. I was expecting them to run in parallel. Is there something wrong with my code, or shall I start to look at my environment? I am running Apache, PHP 7 on a Windows machine.
<?php
class Searcher extends Worker
{
public $data = [];
public function addData($data)
{
$this->data[] = $data;
}
}
class MyThread extends Threaded
{
public function __construct($param)
{
$this->param = $param;
}
public function run()
{
sleep(3);
$this->worker->addData(
$this->param." - TIME: ".microtime(true)
);
}
}
$worker = new Searcher();
$params = ['thread1', 'thread2', 'thread3', 'thread4', 'thread5', 'thread6', 'thread7'];
foreach ($params as &$param) {
$worker->stack(new MyThread($param));
}
$worker->start();
$worker->join();
foreach ($worker->data as $data) {
echo $data."<br/>";
}
?>
The output of the code above always becomes something like this:
thread1 - TIME: 1512249412.3757
thread2 - TIME: 1512249415.7776
thread3 - TIME: 1512249419.1792
thread4 - TIME: 1512249422.5813
thread5 - TIME: 1512249425.9825
thread6 - TIME: 1512249429.384
thread7 - TIME: 1512249432.7859