dongxi7722
dongxi7722
2015-04-27 16:35
采纳率: 0%
浏览 30
已采纳

将值返回给调用线程

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongxi0523
    dongxi0523 2015-05-01 01:14
    已采纳

    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);
    
    点赞 评论
  • douyousu9691
    douyousu9691 2015-05-03 23:32

    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)
    
    点赞 评论

相关推荐