有时我必须使用它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)