c++ - std::thread in C++11, how to ensure a thread is finished inside a destructor -


i want make sure thread completed before object destructed.

here basic example think of:

struct user {     std::thread worker_thread;      ~user() {         if (worker_thread.joinable()) {             worker_thread.join();         }     } }; 

is correct approach problem?

it not correct, join() may throw exception, , don't want let exception escape destructor. herb sutters exceptional c++ in section destructors throw , why they're evil.:

observe canonical exception safety rules: never allow exception escape destructor or overloaded operator delete() or operator delete[](); write every destructor , deallocation function though had exception specification of "throw() ."


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -