This is my first attempt to use boost::threads and I have a silly question.
I 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 caseupdateEpsilons). Obviously thisoperator()()is not correct because it does not call the appropriateupdateEpsilonsmethod 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.