C ++ exception not handled in thread

Why is the unhandled exception VS 2013, or any interrupt signal that occurs when the following code is executed?

 #include <thread> void f1() { throw(1); } int main(int argc, char* argv[]) { std::thread(f1); } 

The C ++ standard states that std :: terminate should be called in the following situation:

when the exception handling mechanism cannot find a handler for a thrown exception (15.5.1)

in such cases, std::terminate() is called (15.5.2)

+6
source share
1 answer

The problem is that in this code, main () may terminate before the spawned stream (f1).

Try this instead:

 #include <thread> void f1() { throw(1); } int main(int argc, char* argv[]) { std::thread t(f1); t.join(); // wait for the thread to terminate } 

This call completes () on Coliru (gcc).

Unfortunately, Visual Studio 2013 will directly call abort () instead of terminate () (at least in my tests) if I encounter this, so even adding a handler (using std :: set_handler ()) does not seem to work . I reported this to the VS team .

However, this code will throw an error while your source code is not guaranteed.

+3
source

Source: https://habr.com/ru/post/972490/


All Articles