join () 和 detach () 的理解
https://blog.csdn.net/qq_36784975/article/details/87699113
#include <iostream> | |
#include <thread> | |
using namespace std; | |
void func() | |
{ | |
for (int i = 0; i < 100; i++) | |
{ | |
cout << "from func():" << i << endl; | |
} | |
} | |
int main() // 主线程 | |
{ | |
thread t(func); // 子线程,一旦传入即可执行 | |
t.join(); // 等待子线程结束后才进入主线程 | |
// 此时先打印完 "from func ():" 再打印 "mian ()" | |
// 说明主线程在等待子线程运行完再运行 | |
cout << "mian()" << endl; | |
cout << "mian()" << endl; | |
cout << "mian()" << endl; | |
return 0; | |
} |
如果去掉 join,则主线程会同时运行。
将 t.join();
放在最后,即 return 0;
的前面,会有作用么?会有,在打印的时候主线程和子线程是同时执行的
传递类的成员函数给 thread
https://blog.csdn.net/o1101574955/article/details/77339854