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()
oroperator delete[]();
write every destructor , deallocation function though had exception specification of "throw()
."
Comments
Post a Comment