Is there any way to prevent main thread from crashing?
I want the main thread to keep running after this memory access violation exception happens.
std::thread {
[]() {
for (auto i = 0; i < 10; ++i) {
std::cout << "Hello World from detached thread!\n";
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << "Boom!\n";
*(int*)(0) = 42;
}
}.detach();
while (true) {
std::cout << "Hello World!\n";
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
Run Code Online (Sandbox Code Playgroud)