jeudi 20 octobre 2016

Terminating an std::thread which runs in endless loop

How can I terminate my spun off thread in the destructor of Bar (without having to wait until the thread woke up form its sleep)?

class Bar {

public:

Bar() : thread(&Bar:foo, this) {
}

~Bar() { // terminate thread here}



...

void foo() {
  while (true) {
     std::this_thread::sleep_for(
     std::chrono::seconds(LONG_PERIOD));

    //do stuff//
   }

}

private:
  std::thread thread;

};

Aucun commentaire:

Enregistrer un commentaire