开发者

Return Double from Boost thread

开发者 https://www.devze.com 2023-02-14 07:29 出处:网络
Hi I have an Boost thread which should return a do开发者_运维技巧uble. The function looks like this:

Hi I have an Boost thread which should return a do开发者_运维技巧uble. The function looks like this:

void analyser::findup(const double startwl, const double max, double &myret){
  this->data.begin();
  for(int i = (int)data.size() ; i >= 0;i--){
    if(this->data[i].lambda > startwl){
      if(this->data[i].db >= (max-30)) {
        myret = this->data[i+1].lambda;
        std::cout <<"in thread " << myret << std::endl;
        return;
      }
    }
  }
}

this function is called by another function:

void analyser::start_find_up(const double startwl, const double max){
  double tmp = -42.0;
  boost::thread up(&analyser::findup,*this, startwl,max,tmp);
  std::cout << "before join " << tmp << std::endl;
  up.join();
  std::cout << "after join " << tmp << std::endl;
}

Anyway I've tried and googled almost anything but i can't get it to return a value.

The output looks like this right now.

before join -42
in thread 843.487
after join -42

Thanks for any help.


tmp does not get the expected value after thread join because creating a boost::thread object copies all arguments to thread storage (see documentation).

You must encapsulate tmp with boost::ref, which has the effect of providing a "copy-constructible reference" (sorry if the terminology is inadequate, but that's the idea)

double tmp = -42.0;
boost::thread up(&analyser::findup,*this, startwl,max, boost::ref(tmp));

More on the Thread Management documentation

0

精彩评论

暂无评论...
验证码 换一张
取 消