使用detach分离线程时,为什么我的用以打印流的子线程会重复执行

在下面的这段代码中,多次执行偶尔会出现预料之外的情况,cout将数字3进行了多次输出,这是怎么回事?

 


```c++
#include <iostream>
#include <thread>

using namespace std;

class A
{
    public:
        int a;
        char c;
        A(int x) : a(x){};
}
void myprint(const A& q)
{
    cout << q.a << endl;
}
int main()
{
    char buf[] = "hello world!";
    thread obj(myprint, 3);
    obj.detach();
    cout << "end!!!" << endl;
}

```

这个问题我解决了,测一下join()和detach()的cpu时间,答案呼之欲出。此贴终结。