Getting return value from a boost::threaded member function?

前端 未结 5 1423
星月不相逢
星月不相逢 2020-12-17 01:19

I have a worker class like the one below:

class Worker{
public:
  int Do(){
    int ret = 100;
    // do stuff
    return ret;
  }
}

It\'s

5条回答
  •  北海茫月
    2020-12-17 01:59

    class Worker{
    public:
      int Do(){
      int ret = 100;
      // do stuff
      return ret;
      }
    }
    
    Worker worker;
    boost::packaged_task ptask(boost::bind(&Worker::Do, &worker));
    boost::unique_future future_int = ptask.get_future();
    boost::thread th(boost::move(ptask));
    th.join();
    if (future_int.is_ready())
       int return_value = future_int.get();
    

    You can take a look at the "boost::future" concept, ref this link

提交回复
热议问题