I've got a problem: Worker threads seem to work serially. This is a very simple example just for a demonstration a problem.
I initialize my worker threads and run them. It should work in parallel however the result looks like it was processed serially.
PHP 5.6.12, Windows OS, Apache server
DBWorker: Which of course extends pThreads Worker class
class DBWorker extends Worker {
private $str;
public function __construct($str){
$this->str = $str;
}
public function run(){
$i=0;
while($i < 10) {
echo "$this->str: $i
";
$i++;
}
}
}
Initialization:
private $DBWorker1;
private $DBWorker2;
private $DBWorker3;
/* ... */
public function __construct(){
$this->DBWorker1 = new DBWorker("worker1");
$this->DBWorker2 = new DBWorker("worker2");
$this->DBWorker3 = new DBWorker("worker3");
}
/* ... */
$this->DBWorker1->start();
$this->DBWorker2->start();
$this->DBWorker3->start();
$i=0;
while($i < 10) {
echo "parent: $i
";
$i++;
}
var_dump($this->DBWorker1->shutdown());
var_dump($this->DBWorker2->shutdown());
var_dump($this->DBWorker3->shutdown());
Result:
parent: 0
parent: 1
parent: 2
...
parent: 6
worker1: 0
worker1: 1
worker1: 2
...
worker1: 59
worker2: 0
worker2: 1
worker2: 2
...
worker2: 59
worker3: 0
worker3: 1
worker3: 2
...
worker3: 59
parent: 7
parent: 8
parent: 9
bool(true)
bool(true)
bool(true)