mercredi 1 novembre 2017

How to reuse an std::thread as a member of a class

I have the following simple way of starting a thread with a call to a member function from within a lambda

auto worker_thread = std::thread { [this, &some_data] {
    this->SomeMethod(some_data);
}};

Issue:
I want to make worker_thread as a class member. Provided that some_data is also a member. I want to create it only once when the class constructor is called & destroy once when destructor called. At the same time I want to be able to pause and resume it at will. Looks like std::condition_vaiable might be handy in enforcing this technique ? Are there any ways other than using a std::condition_vaiable? Or any easier ways of doing this?

Question:
How can I design this to resume worker_thread on request & pause on request without having to create it every time want to execute this->SomeMethod(some_data)?

Aucun commentaire:

Enregistrer un commentaire