小编Yur*_*ura的帖子

Prevent the application from crashing if one of the threads crashed

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)

c++ multithreading

2
推荐指数
1
解决办法
1667
查看次数

标签 统计

c++ ×1

multithreading ×1