mercredi 29 juin 2016

Starting a thread from a member function while inside another class's member function

I need to start a thread that calls a public member function of the class Foo while inside of a public function that belongs to the class Bar. How do I achieve this?

I have tried the following (made trivial):

void Bar::BarFunc()
{ 
    // Do some BarFunc stuff 

    // Start a thread that needs to do stuff independently of BarFunc
    std::thread t(&Foo::FooFunc, FooFunc params,..,.., ???);
    t.detach();

    return;
}

This is my first time dealing with threading and the actual problem is a little more complex - BarFunc is a virtual function of a State class, with n-concrete classes implementing the different states my application can exist in, hence the question. I am not sure what to put as the last parameter, if anything. I have looked at this answer but cannot discern which syntax to use, if any of them even apply.

Finally, if this is bad practice all together, I would be grateful for any design advice.

Aucun commentaire:

Enregistrer un commentaire