-1

I have been playing with the c++11 thread for a while, and have some questions. People say that when you call a class member function in thread, this function has to be static. But it seems that this is not true. For example, I found this:

class bar
{ public:
void foo() {std::out << "Hello" << std::endl};
}
int main()
{
std::thread t0(&bar::foo, bar());
t0.join();
}

The above code works fine. It seems that the member function do not have to be static in c++11 standard. I want to know if my understanding is true. Another question is that, if I simply modify "void foo()" with "static void foo()", I get an error:

error: no type named 'type' in 'class std::result_of<void (*(bar))()>'

I do not understand this, but it seems that this is because the way I call the thread is not correct. I am very confuse in calling a function in a thread. For example, I found another way to call the same member function in the above example as

int main()
{ bar A;
std::thread t0(&bar::foo, &A);
}

It works also! I don't know the difference between these two ways. It seems that in the first way the constructor of the class will be performed each time I call foo(), while the in the second one it will not. Is that true? Besides, when calling member function in another class member function, the 'this' has to be passed.

I search the internet, all I can find is examples, without explaining the meaning of the arguments. Can anyone tell me how should I set the arguments (especially the first three) in a std::thread?

Hongkun Li
  • 13
  • 1
  • 3

1 Answers1

2

std::thread t0(&bar::foo) works pretty fine with static foo method.

Also, when asking a question consider to provide working code but not your text what you actually wrote just now right in the text field of this site.

Short answer to your question: provide this parameter to non-static class methods or don't provide it if it is static class method.

But, if you don't understand even that you must know one thing: each class method works with this object which means a constant pointer to current object. While binding your function (I guess exactly binding works inside std::thread) the first parameter must be the object which method you want to call. Otherwise it is meaningless: you are trying to do something with what object then? The this exists in each non-static method of the class, implicitly, by first parameter of the method.

I recommend you to read the definitive c++ stackoverflow book guide and list

Community
  • 1
  • 1
VP.
  • 15,509
  • 17
  • 91
  • 161
  • Thanks for the answer! I have checked that std::thread t0(&bar::foo) works well with a static foo() member function. So it is clearer for me now: as the static foo() do not have a 'this' pointer, I need only to pass the address of the function &bar::foo, while for a non-static foo(), I need to pass the address of the object also. That makes sense! So this means that with c++11, we can call a non-static member function in a thread, right? – Hongkun Li Jun 08 '15 at 18:20