编译器错误 C2384

“membe”:不能对托管类或 WinRT 类的成员应用 __declspec(thread)

thread__declspec 修饰符不能用于托管或 Windows 运行时类的成员。

托管代码中的静态线程本地存储仅可用于静态加载的 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;
};