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