vendredi 23 janvier 2015

C++11 thread from an existent class [duplicate]


This question already has an answer here:




I'm certain that this is a simple design error, but I'm not sure where to go with it anyway.


I'd like to spawn a thread from an instance of a class. Specifically:



class Foo
{
public:
void bar() { /*do stuff*/ }
};


Usage:



int main()
{
Foo foo_instance();

std::thread foo_thread(foo_instance.bar);

foo_thread.join();

return 0;
}


When I compile the more detailed version of this, I get invalid use of non-static member function referring to the line std::thread foo_thread(foo_instance.bar);.


So, what am I misunderstanding here? I'd like the object to be initialized and "functional" before I spin it off into a thread, but clearly I'm not using the tools correctly.


Aucun commentaire:

Enregistrer un commentaire