相关疑难解决方法(0)

我什么时候应该使用std :: thread :: detach?

有时我必须使用它std::thread来加速我的应用程序.我也知道join()等待线程完成.这很容易理解,但是呼叫detach()和不呼叫之间的区别是什么?

我认为没有detach(),线程的方法将独立使用线程.

不分离:

void Someclass::Somefunction() {
    //...

    std::thread t([ ] {
        printf("thread called without detach");
    });

    //some code here
}
Run Code Online (Sandbox Code Playgroud)

呼叫分离:

void Someclass::Somefunction() {
    //...

    std::thread t([ ] {
        printf("thread called with detach");
    });

    t.detach();

    //some code here
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11 stdthread

124
推荐指数
4
解决办法
7万
查看次数

标签 统计

c++ ×1

c++11 ×1

stdthread ×1