tags:

views:

541

answers:

1

I would like to keep a static counter in a garbage collected class and increment it using Interlocked::Increment. What's the C++/CLI syntax to do this?

I've been trying variations on the following, but no luck so far:

ref class Foo
{
   static __int64 _counter;

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

};
+1  A: 

You need to use a tracking reference to your _int64 value, using the % tracking reference notation:

ref class Bar
{
    static __int64 _counter;

    __int64 Next()
    {
     __int64 %trackRefCounter = _counter;
     return System::Threading::Interlocked::Increment(trackRefCounter);
    }
};
McKenzieG1