开发者

C++ boost::thread operator()() question

开发者 https://www.devze.com 2023-04-03 04:52 出处:网络
This is my first attempt to use boost::threads and I have a silly question. I 开发者_StackOverflow中文版call a boost:thread to use one of my template class functions. However after reading this tutori

This is my first attempt to use boost::threads and I have a silly question. I 开发者_StackOverflow中文版call a boost:thread to use one of my template class functions. However after reading this tutorial it says to construct an operator()() which I did. Will the code below work properly ?

template <class S>
class SarsaL : public Task<S,Policy>, protected Method
{
  protected:
    ...
    void updateEpsilons(S* avoid);
    void step();
    ...
  public:
    ...
    void operator()();
    ...
};

template <class S>
void SarsaL<S>::operator()()
{
  updateEpsilons();
}

template <class S>
void SarsaL<S>::step()
{
   S* now_state =  Task<S,Policy>::checkIfAdd();
   ...
   ...
   boost::thread workerThread(&SarsaL<S>::updateEpsilons, this, now_state);
   ...
   ...
   workerThread.join();
}

The reason I am asking is because I am calling updateEpsilons() within the operator without a parameter, yet when creating the thread I send the parameter now_state. Will this work or take no argument ? Code compiles and executes without error, I am just puzzled.


You don't have to use the operator()() if you provide a method on the object to be executed (in this case updateEpsilons). Obviously this operator()() is not correct because it does not call the appropriate updateEpsilons method with a parameter.

Note that in the tutorial, the new thread is created giving just an instance of a class, and no method. In this case, the class has to implement the operator()(), which is what will be called for the code of the thread.

0

精彩评论

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