有时我不得不使用std::thread
来加速我应用程序。我还知道join()
会一直等到线程完成。这很容易理解,但是调用detach()
和不调用它有什么区别呢?
我认为如果没有detach()
,线程的方法将独立地使用线程工作。
不分离:
void Someclass::Somefunction() {
//...
std::thread t([ ] {
printf("thread called without detach");
});
//some code here
}
带分离的调用:
void Someclass::Somefunction() {
//...
std::thread t([ ] {
printf("thread called with detach");
});
t.detach();
//some code here
}
https://stackoverflow.com/questions/22803600
复制相似问题