Interlocked::Increment Method (Int32%)

Increments a specified variable and stores the result, as an atomic operation.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public:
static int Increment(
	int% location
)

Parameters

location
Type: System::Int32%

The variable whose value is to be incremented.

Return Value

Type: System::Int32
The incremented value.

ExceptionCondition
NullReferenceException

The address of location is a null pointer.

This method handles an overflow condition by wrapping: if location = Int32::MaxValue, location + 1 = Int32::MinValue. No exception is thrown.

The following code example shows a thread-safe way to increment and decrement an integer value. SafeInstanceCount will always be zero. However, UnsafeInstanceCount will not necessarily be zero because a race condition occurs between incrementing and decrementing the count. This effect is especially marked on a multiprocessor computer.

using namespace System;
using namespace System::Threading;
ref class CountClass
{
private:
   static int unsafeInstanceCount;
   static int safeInstanceCount;

public:

   static property int UnsafeInstanceCount 
   {
      int get()
      {
         return unsafeInstanceCount;
      }

   }

   static property int SafeInstanceCount 
   {
      int get()
      {
         return safeInstanceCount;
      }

   }
   CountClass()
   {
      unsafeInstanceCount++;
      Interlocked::Increment( safeInstanceCount );
   }

   // Destructor and finalizer. The finalizer is used to decrement the 
   // count, so garbage collection enforces decrementing.
   ~CountClass() { this->!CountClass(); }
   !CountClass()
   {
      unsafeInstanceCount--;
      Interlocked::Decrement( safeInstanceCount );
   }
};

ref class Test
{
public:
   static void ThreadMethod()
   {
      CountClass^ cClass;

      // Create 100,000 instances of CountClass. 
      for ( int i = 0; i < 100000; i++ )
      {
         cClass = gcnew CountClass;
      }
   }

};

int main()
{
   Thread^ thread1 = gcnew Thread( gcnew ThreadStart( &Test::ThreadMethod ) );
   Thread^ thread2 = gcnew Thread( gcnew ThreadStart( &Test::ThreadMethod ) );
   thread1->Start();
   thread2->Start();
   thread1->Join();
   thread2->Join();

   // Have the garbage collector run the finalizer for each 
   // instance of CountClass and wait for it to finish.
   GC::Collect();
   GC::WaitForPendingFinalizers();
   Console::WriteLine( "UnsafeInstanceCount: {0}" 
   "\nSafeCountInstances: {1}", CountClass::UnsafeInstanceCount.ToString(), CountClass::SafeInstanceCount.ToString() );
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft