Returning a value to the calling thread

I am trying to use pthreads in PHP to speed up a series of computations that are currently maxing one cpu core in a matter of seconds. I split the calculations into multiple ranges and run each range on a parent thread:

    $threads = array();
    foreach($cp as $c) {
        $threads[$c] = new ThreadedMatcher($params);
        $threads[$c]->start();
    }

      

Then I want array_merge

arrays created in each child thread (for each range) in the parent thread to get the value across the entire dataset.

I understand what I need to use join()

in the parent thread to wait for the thread to end, but how can I get the value from the child thread to the parent thread?

+3


source to share


2 answers


Try the following:

This will split the payload according to your available cores, going through batches so as not to exceed the maximum Cpus threads. I use this very simple principle for many of the other Pthreads features I have tested. Simplest and most effective for me so far.



Works flawlessly with xenon servers and 80 available Cpus streams through 100M iterations over a period of hours.

$Pay_Load=array(
array('id'=>$id,'query'=>$query1,'param'=>$param1),
array('id'=>$id,'query'=>$query2,'param'=>$param2),
/*... can be hundreds...just an example...*/
)

$Nb_of_Cpus=4;
$Nb_of_threads=$Nb_of_Cpus*2;
$Batch_Works=array_chunk($Pay_Load,$Nb_of_threads);

$threads = [];
$results = array();

foreach ($Batch_Works as $batch) {
    foreach ($batch as $key => $params) {
        $threads[$key] = new ThreadedMatcher($params);
        $threads[$key]->start();
    }
    foreach ($batch as $key => $params) {
        $threads[$key]->join();
        $returned_result=$threads[$key]->result;
        $returned_id=$threads[$key]->id;
        $result=array('id'=>$returned_id,'result'=>$returned_result);
        array_push($results, result);    
    }
}
/* all returned results are now in the same array */
/* with the original Payload id as an example here */
var_dump($results);

      

+2


source


Complete class:



class ThreadedMatcher extends Thread {

private $query;
private $param;
public $result;
public $id;


public function __construct($params) {

    $this->query = $params['query'];
    $this->param = $params['param'];
    $this->id= $params['id'];

}

public function run() {

    /* do some stuff*/
    echo ($this->query);
    echo ($this->param);

    $this->result = rand(100, 200);
}
}

$Pay_Load=array(
array('id'=>$id,'query'=>$query1,'param'=>$param1),
array('id'=>$id,'query'=>$query2,'param'=>$param2),
/*... can be hundreds...just an example...*/
)

$Nb_of_Cpus=4;
$Nb_of_threads=$Nb_of_Cpus*2;
$Batch_Works=array_chunk($Pay_Load,$Nb_of_threads);

$threads = [];
$results = array();

foreach ($Batch_Works as $batch) {
    foreach ($batch as $key => $params) {
        $threads[$key] = new ThreadedMatcher($params);
        $threads[$key]->start();
    }
    foreach ($batch as $key => $params) {
        $threads[$key]->join();
        $returned_result=$threads[$key]->result;
        $returned_id=$threads[$key]->id;
        $result=array($returned_id=>$returned_result);
        array_push($results, result);    
    }
}
/* all returned results are now in the same array */
/* with the original Payload id as an example here */

var_dump($results);

$results =(
1=>103,
2=>234,
3=>345,
4=>123)

      

+2


source







All Articles