thread_local c 11,VS2015中的错误C2492

thread_local C++11, Error C2492 in VS2015

本文关键字:错误 C2492 VS2015 local thread      更新时间:2023-10-16

我有这样的类:

在CustomAllocator.h文件中:

namespace MemoryMgmt
{
  class PoolMemory 
  {
   ....
  }
}
class CustomAllocator
  {
  public:
    void * operator new(size_t i_size);
    void operator delete(void*, void*);
    virtual ~CustomAllocator(){}; 

#pragma warning(suppress: 4251)
    static thread_local MemoryMgmt::PoolMemory memory_manager_current;
  };
inline void * CustomAllocator::operator new(size_t i_size)
  {
  return memory_manager_current.AllocateChars(i_size);
  }
inline void CustomAllocator::operator delete(void * , void*)
  {
  //Should not be used
  ASSERT(!"Placement delete should not be used for memory objects");
  }

在customallocator.cpp文件中:

#include <CustomAllocator.h>
thread_local MemoryMgmt::PoolMemory CustomAllocator::memory_manager_current;

我在许多不同的库中使用了自定义辅助器类。我会收到以下错误(许多错误与:

相同)
error C2492: 'public: static MemoryMgmt::PoolMemory CustomAllocator::memory_manager_current': data with thread storage duration may not have dll interface (compiling source file D:*******.cpp)

我使用Visual Studio 2015,我想它在此版本中得到了完全支持。

当您一起使用__declspec(dllexport)__declspec(thread)时,会发生此错误。在您的情况下,memory_manager_currentstatic类成员用作线程本地存储。但是,我强烈怀疑该类可能已经用属性__declspec(dllexport)声明(尽管在您的帖子中没有看到)。

您可以安全地将memory_manager_current移出课堂,并将其作为class CustomAllocator实现文件中的全局变量。

希望这会有所帮助。