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

前端 未结 5 1425
星月不相逢
星月不相逢 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:55

    I don't think you can get the return value.

    Instead, you can store the value as a member of Worker:

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

    And use it like so:

    Worker worker;
    boost::function th_func = boost::bind(&Worker::Do, &worker);
    boost::thread th(th_func);
    th.join();
    //do something with worker.m_ReturnValue
    

提交回复
热议问题