I'm currently working with pthreads to implement multithreading on a very demanding function. So far I got this working:
class Operation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$parameters = $this->arg;
echo my_function($parameters[0],$parameters[1]);
}
}
}
$stack = array();
foreach ($work as $operation) { $stack[] = new Operation($operation); };
foreach ($stack as $t) { $t->start(); };
It outputs the results directly. I'd like to have my results stored one by one in an array (in the same order would be nice) but of course this does not work :
class Operation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$parameters = $this->arg;
$results[] = my_function($parameters[0],$parameters[1]);
}
}
}
$stack = array();
foreach ($work as $operation) { $stack[] = new Operation($operation); };
foreach ($stack as $t) { $t->start(); };
var_dump($results);
Any help would be appreciated.
Details:
- my_function outputs an UTF-8 string.
my_function()code here?