編譯器錯誤 C2384
'member':無法將 __declspec(thread) 套用至 Managed 或 WinRT 類別的成員
thread __declspec
修飾元無法使用於受控或 Windows 執行階段類別的成員。
Managed 程式碼中的靜態執行緒區域儲存區僅可以使用於以靜態方式載入的 DLL — 在處理序啟動時 DLL 必須以靜態方式載入。 Windows 執行階段不支援執行緒區域儲存區。
下列範例會產生 C2384,並示範如何在 C++/CLI 程式碼中修正此問題:
// C2384.cpp
// compile with: /clr /c
public ref class B {
public:
__declspec( thread ) static int tls_i = 1; // C2384
// OK - declare with attribute instead
[System::ThreadStaticAttribute]
static int tls_j;
};