C++ thread 在堆内申请创建线程对象,线程会直接运行而不需要join、detach,这是为什么?
请看相关源码:
#include <iostream>
#include <thread>
#include <functional>
using namespace std;
void test()
{
cout << "线程已启动" << endl;
int i = 90;
while(--i > 0)
{
cout << i << endl;
}
cout << "线程结束" << endl;
}
int main()
{
thread *t1 = new thread(test);
int i = 900000;
while(--i > 0) ;
return 0;
}
运行结果:
另以下方式也是直接运行,同样让人不解:
thread th_;
th_ = thread(test);
int i = 900000;
while(--i > 0) ;