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

前端 未结 5 1436
星月不相逢
星月不相逢 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 02:06

    In addition, you also have some redundant calls to boost::bind() and boost::function(). You can instead do the following:

    class Worker{
        public:
           void operator(){
              int ret = 100;
              // do stuff
              m_ReturnValue = ret;
           }
        int m_ReturnValue;
    }
    
    Worker worker;
    boost::thread th(worker());//or boost::thread th(boost::ref(worker));
    

    You can do this because Thread's constructor is a convenience wrapper around an internal bind() call. Thread Constructor with arguments

提交回复
热议问题