James
James

Reputation: 771

Returning a value to the calling thread

I'm trying to use pthreads in PHP in an attempt to speed up a series of calculations that at the moment max out one CPU core for several seconds. I have split the calculations into a number of ranges and am running each range in the parent thread:

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

I then want to array_merge the arrays created in each child thread (for each range) in the parent thread to get a value over the whole data set.

I gather that I need to use join() in the parent thread to wait for a thread to finish but how do I actually get a value out of the child thread into the parent thread?

Upvotes: 3

Views: 1948

Answers (2)

cpugourou
cpugourou

Reputation: 781

The 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)

Upvotes: 2

cpugourou
cpugourou

Reputation: 781

Try this:

This will split the Payload according to your available cores, looping across batches, not to exceed the available Cpus maximum threads. I am using this very simple principle over many other Pthreads possibilities I have tested. Simplest and most efficient one for me so far.

Works flawlessly with xenons servers and 80 available Cpus threads over 100M iterations for 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);

Upvotes: 3

Related Questions