这个例子很好理解
#include <thread>
#include <windows.h>
using namespace std;
void fun1()
{
cout << "fun1:" << GetCurrentThreadId() << endl;
}
void fun2(int parameter)
{
cout << "fun2:" << GetCurrentThreadId() << endl;
}
int main()
{
cout << "main:" << GetCurrentThreadId() << endl;
thread t1(fun1);
t1.join();
cout << "main:" << GetCurrentThreadId() << endl;
thread t2(fun2, 10);
t2.detach();
cout << "main:" << GetCurrentThreadId() << endl;
return 0;
}
fun1的输出必定在第一和第二个”main:”之间,表明了join对当前线程的阻塞。
而fun2的输出有时在最后,有时不输出,因为在fun2输出之前main函数就运行结束了,fun2线程的资源被回收