读书人

关于_declspec(thread) 在XP中的有关问

发布时间: 2013-01-07 10:02:24 作者: rapoo

关于__declspec(thread) 在XP中的问题
ocx用到一个动态库库A.dll,A.dll中如果使用静态的thread local storage(__declspec(thread))变量,用regsvr32去注册ocx就会报分配内存错误,如果去掉__declspec(thread),只是一个简单的全局变量,就一切正常,请问MS对__declspec(thread)的使用有什么特殊要求么.
[解决办法]
最好不用全局变量
[解决办法]
If a DLL declares any nonlocal data or object as __declspec( thread ), it can cause a protection fault if dynamically loaded. After the DLL is loaded with LoadLibrary, it causes system failure whenever the code references the nonlocal __declspec( thread ) data. Because the global variable space for a thread is allocated at run time, the size of this space is based on a calculation of the requirements of the application plus the requirements of all the DLLs that are statically linked. When you use LoadLibrary, there is no way to extend this space to allow for the thread local variables declared with __declspec( thread ). Use the TLS APIs, such as TlsAlloc, in your DLL to allocate TLS if the DLL might be loaded with LoadLibrary.

读书人网 >VC/MFC

热点推荐