阅读背景:

在原始调用者被破坏后,分离会导致崩溃

来源:互联网 
struct Test {
    bool active{true};

    void threadedUpdate() {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
        if(!active) // crashes here after Test instance is destroyed
            return; 
    }

    Test() { 
        std::thread([this]{ while(true) threadedUpdate(); }).detach();
    }

    ~Test() { 
        // somehow stop the detached thread?
    } 
};
struct Test {
    bool active{true};

    void 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: