2
#include <pthread.h>
#include <thread>
#include <iostream>
using namespace std;
struct A
{
~A()
{
cout << "~A()" << endl;
}
};
void* f(void*)
{
thread_local A a;
return 0;
}
int main()
{
pthread_t tid;
pthread_create(&tid, nullptr, f, nullptr);
this_thread::sleep_for(5min);
}
根據cppreference:「thread_local」是否適用於裸線?
線程開始時的對象被分配和 螺紋結束時釋放。每個線程都有自己的對象實例。只有 對象聲明
thread_local
有這個存儲時間。
我只是想知道:
如何C++編譯器創建和退出裸線程(,而不是std::thread
)時,知道嗎?
換句話說:
是否裸線程函數f
結束後的C++標準保證A::~A()
會叫什麼名字?
從C++標準的角度來看,這很難回答,因爲C++標準沒有使用它的方式的「裸線程」的概念。但是,當然C++線程將以特定於平臺的方式實現,如果您碰巧直接編寫相同的實現,最終可能會得到相同的行爲。 –