有时我需要使用
我认为如果没有
不分离:
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
}
std
和boost
线程都有detach
和join
,这两个函数的模型是基于 POSIX 线程的。 - n. m.