如何在C++/CLI的静态变量上使用System :: Threading :: Interlocked :: Increment?

Eri*_*ric 1 c++-cli

我想在垃圾收集类中保留一个静态计数器,并使用Interlocked :: Increment递增它.这样做的C++/CLI语法是什么?

我一直在尝试以下变化,但到目前为止没有运气:

ref class Foo
{
   static __int64 _counter;

   __int64 Next()
   {
      return System::Threading::Interlocked::Increment( &_counter );
   }

};
Run Code Online (Sandbox Code Playgroud)

McK*_*eG1 5

你需要使用一个跟踪参考你的_int64值,使用%跟踪参考符号:

ref class Bar
{
    static __int64 _counter;

    __int64 Next()
    {
        __int64 %trackRefCounter = _counter;
        return System::Threading::Interlocked::Increment(trackRefCounter);
    }
};
Run Code Online (Sandbox Code Playgroud)