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

codeigniter - Fatal error: Call to undefined function lang() in CI Merchant using CardSave -

javascript - CasperJS/PhantomJS failing SSL handshakes on some sites even with --ssl-protocol=any -

python - Received unregistered task using Celery with Django -