Interlocked.Increment Method (Int32)
Assembly: mscorlib (in mscorlib.dll)
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 System; using System.Threading; class Test { static void Main() { Thread thread1 = new Thread(new ThreadStart(ThreadMethod)); Thread thread2 = new Thread(new ThreadStart(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()); } static void ThreadMethod() { CountClass cClass; // Create 100,000 instances of CountClass. for(int i = 0; i < 100000; i++) { cClass = new CountClass(); } } } class CountClass { static int unsafeInstanceCount = 0; static int safeInstanceCount = 0; static public int UnsafeInstanceCount { get {return unsafeInstanceCount;} } static public int SafeInstanceCount { get {return safeInstanceCount;} } public CountClass() { unsafeInstanceCount++; Interlocked.Increment(ref safeInstanceCount); } ~CountClass() { unsafeInstanceCount--; Interlocked.Decrement(ref safeInstanceCount); } }
import System.*;
import System.Threading.*;
import System.Threading.Thread;
class Test
{
public static void main(String[] args)
{
Thread thread1 = new Thread(new ThreadStart(ThreadMethod));
Thread thread2 = new Thread(new ThreadStart(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}",
String.valueOf(CountClass.get_UnsafeInstanceCount()),
String.valueOf(CountClass.get_SafeInstanceCount()));
} //main
static void ThreadMethod()
{
CountClass cClass;
// Create 100,000 instances of CountClass.
for (int i = 0; i < 100000; i++) {
cClass = new CountClass();
}
} //ThreadMethod
} //Test
class CountClass
{
private static int unsafeInstanceCount = 0;
private static int safeInstanceCount = 0;
/** @property
*/
public static int get_UnsafeInstanceCount()
{
return unsafeInstanceCount;
} //get_UnsafeInstanceCount
/** @property
*/
public static int get_SafeInstanceCount()
{
return safeInstanceCount;
} //get_SafeInstanceCount
public CountClass()
{
unsafeInstanceCount++;
Interlocked.Increment(safeInstanceCount);
} //CountClass
public void finalize()
{
try {
unsafeInstanceCount--;
Interlocked.Decrement(safeInstanceCount);
super.finalize();
}
catch (Throwable th) {
}
} //finalize
} //CountClass
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.