线程和异常处理 我在多线程C++代码中处理异常。以下程序退出时调用了terminate,未中止活动异常(内核转储) #包括 #包括 #包括 结构物{ void runComponent() { 抛出std::runtime_错误(“哦,不!\n”); } void runController() { while(true){} } 无效运行() { 自动控制_thread=std::thread([this]{runController();}); runComponent(); if(control_thread.joinable()) control_thread.join(); } }; int main() { 试一试{ 事物; 运行(); }捕获(标准::运行时错误和ex){ STR::CURR 这里的问题是,您在调用 Run()/Cux>方法中创建的线程中既不调用“代码> Debug()),也不调用“代码>连接”( > >代码>。这是C++线程中的一个错误。参见< /P>

线程和异常处理 我在多线程C++代码中处理异常。以下程序退出时调用了terminate,未中止活动异常(内核转储) #包括 #包括 #包括 结构物{ void runComponent() { 抛出std::runtime_错误(“哦,不!\n”); } void runController() { while(true){} } 无效运行() { 自动控制_thread=std::thread([this]{runController();}); runComponent(); if(control_thread.joinable()) control_thread.join(); } }; int main() { 试一试{ 事物; 运行(); }捕获(标准::运行时错误和ex){ STR::CURR 这里的问题是,您在调用 Run()/Cux>方法中创建的线程中既不调用“代码> Debug()),也不调用“代码>连接”( > >代码>。这是C++线程中的一个错误。参见< /P>,c++,multithreading,exception,C++,Multithreading,Exception,如果您将代码更改为以下任一项,则一切正常 #include <thread> #include <iostream> #include <stdexcept> struct Thing { void runComponent() { throw std::runtime_error("oh no!\n"); } void runController() { return; }

如果您将代码更改为以下任一项,则一切正常

#include <thread>
#include <iostream>
#include <stdexcept>

struct Thing {

    void runComponent()
    {
       throw std::runtime_error("oh no!\n");
    }

    void runController()
    {
        return;
    }

    void run()
    {
        auto control_thread = std::thread([this] { runController(); });
        // either of the following two will work
        // control_thread.join();
        control_thread.detach();

        runComponent();
    }
};

int main()
{
    try {
        Thing thing;
        thing.run();
    } catch (std::runtime_error &ex) {
        std::cerr << ex.what();
    }
}
#包括
#包括
#包括
结构物{
void runComponent()
{
抛出std::runtime_错误(“哦,不!\n”);
}
void runController()
{
返回;
}
无效运行()
{
自动控制_thread=std::thread([this]{runController();});
//以下两种方法中的任何一种都有效
//control_thread.join();
控制_线程。分离();
runComponent();
}
};
int main()
{
试一试{
事物;
运行();
}捕获(标准::运行时错误和ex){

可能是好奇。我知道有异常情况。但这里的问题(我想…)是投掷器(runComponent)实际上在主线程上。即使在这种情况下,我也需要使用异常指针吗?在下面的答案中回答!我误解了问题是的,这是有效的。我想我确实想要分离,但我注意到您提供的SO链接的答案中提出的要点“除非您需要更灵活,并且愿意提供一个同步机制来等待线程完成,否则您可能会使用分离”。在这个特定的情况下,这意味着什么?如果我使用Debug,将分配由<代码> RunMeungReor()/C++的资源吗?也可以通过“C++线程中的错误”来释放。"你的意思是在标准库实现中吗?@jonnew我想这意味着,如果你想用某种自定义方式来处理异常,那么你必须用锁手动实现它。最简单的事情是按照异常发生的顺序列出线程ID的线程列表。我不这么认为就是这样relevant@jonnew,则
std::thread
析构函数检查以确保用户调用了
detach
join
,如果两者都没有被调用,它将终止程序。因此,我想是的,标准库实现将导致程序退出并出现错误。啊,我明白了。这是有意义的。多谢各位。
#include <thread>
#include <iostream>
#include <stdexcept>

struct Thing {

    void runComponent()
    {
       throw std::runtime_error("oh no!\n");
    }

    void runController()
    {
        return;
    }

    void run()
    {
        auto control_thread = std::thread([this] { runController(); });
        // either of the following two will work
        // control_thread.join();
        control_thread.detach();

        runComponent();
    }
};

int main()
{
    try {
        Thing thing;
        thing.run();
    } catch (std::runtime_error &ex) {
        std::cerr << ex.what();
    }
}