Since there seems to be a legitimate interest in throwing exceptions, and this is a little, at least somewhat related to the question, here's my suggestion: std::thread should be considered an unsafe primitive for building, for example. higher level abstractions. They are doubly risky for exceptions: if an exception comes out of the thread that we just launched, everything, as we have shown, everything explodes. But if the exception disappears in the thread that started std::thread , we may have problems because std::thread destructor requires *this be either connected or disconnected (or, what is the same, be a non-thread ) Violation of these requirements leads to ... a call to std::terminate !
Hazard code card std::thread :
auto run = [] { // if an exception escapes here std::terminate is called }; std::thread thread(run); // notice that we do not detach the thread // if an exception escapes here std::terminate is called thread.join(); // end of scope
Of course, some may argue that if we just detach we edit every thread that we start, we will be safe at this second point. The problem is that in some situations, join is the most sensible thing. For example, the βnaiveβ parallelization of quicksort requires you to wait until the subtasks end. In such situations, join serves as a synchronization primitive (rendez-vous).
Fortunately for us, those higher-level abstractions that I talked about exist and come with a standard library. They are std::async , std::future , as well as std::packaged_task , std::promise and std::exception_ptr . Equivalent, exception-free version above:
auto run = []() -> T // T may be void as above { // may throw return /* some T */; }; auto launched = std::async(run); // launched has type std::future<T> // may throw here; nothing bad happens // expression has type T and may throw // will throw whatever was originally thrown in run launched.get();
And in fact, instead of calling get on a thread called async , you can instead pass the dollar to another thread:
Luc Danton Sep 01 '11 at 17:15 2011-09-01 17:15
source share