.NET API Documentation

Interlocked Class

Provides methods for performing atomic operations on variables that are accessible by multiple threads.

Namespace: System.Threading

Assembly: System.Private.CoreLib.dll

Methods

Increment(ref int location)

public static int Increment(ref int location)

Atomically increments the specified variable by one.

Increment(ref long location)

public static long Increment(ref long location)

Atomically increments the specified variable by one.

Decrement(ref int location)

public static int Decrement(ref int location)

Atomically decrements the specified variable by one.

Decrement(ref long location)

public static long Decrement(ref long location)

Atomically decrements the specified variable by one.

Add(ref int location, int value)

public static int Add(ref int location, int value)

Atomically adds two 32-bit integers and returns the sum.

Add(ref long location, long value)

public static long Add(ref long location, long value)

Atomically adds two 64-bit integers and returns the sum.

Exchange(ref int location, int value)

public static int Exchange(ref int location, int value)

Atomically exchanges the given value with the given variable and returns the original value of the variable.

Exchange(ref long location, long value)

public static long Exchange(ref long location, long value)

Atomically exchanges the given value with the given variable and returns the original value of the variable.

Exchange(ref IntPtr location, IntPtr value)

public static IntPtr Exchange(ref IntPtr location, IntPtr value)

Atomically exchanges the given value with the given variable and returns the original value of the variable.

Exchange(ref object location, object value)

public static object Exchange(ref object location, object value)

Atomically exchanges the given value with the given variable and returns the original value of the variable.

CompareExchange(ref int location, int value, int comparand)

public static int CompareExchange(ref int location, int value, int comparand)

Atomically compares two 32-bit integers for equality and, if they are equal, replaces the second with the third and returns the original value of the second. Otherwise, returns the original value of the second.

CompareExchange(ref long location, long value, long comparand)

public static long CompareExchange(ref long location, long value, long comparand)

Atomically compares two 64-bit integers for equality and, if they are equal, replaces the second with the third and returns the original value of the second. Otherwise, returns the original value of the second.

CompareExchange(ref IntPtr location, IntPtr value, IntPtr comparand)

public static IntPtr CompareExchange(ref IntPtr location, IntPtr value, IntPtr comparand)

Atomically compares two pointers for equality and, if they are equal, replaces the second with the third and returns the original value of the second. Otherwise, returns the original value of the second.

CompareExchange(ref object location, object value, object comparand)

public static object CompareExchange(ref object location, object value, object comparand)

Atomically compares two references for equality and, if they are equal, replaces the second with the third and returns the original value of the second. Otherwise, returns the original value of the second.

ExchangeAdd(ref int location, int value)

public static int ExchangeAdd(ref int location, int value)

Atomically adds two 32-bit integers, replaces the first with the sum of the two, and returns the original value of the first.

ExchangeAdd(ref long location, long value)

public static long ExchangeAdd(ref long location, long value)

Atomically adds two 64-bit integers, replaces the first with the sum of the two, and returns the original value of the first.

Read(ref long location)

public static long Read(ref long location)

Atomically reads the value from the specified variable.

Write(ref long location, long value)

public static void Write(ref long location, long value)

Atomically writes the specified value to the specified variable.

CompareExchange(ref T location, T value, T comparand) (Generic)

public static T CompareExchange<T>(ref T location, T value, T comparand) where T : class

Atomically compares two references for equality and, if they are equal, replaces the second with the third and returns the original value of the second. Otherwise, returns the original value of the second. This is a generic overload for reference types.

CompareExchange(ref TLocation location, T value, T comparand) (Generic)

public static TLocation CompareExchange<TLocation, T>(ref TLocation location, T value, T comparand) where TLocation : struct

Atomically compares two structure values for equality and, if they are equal, replaces the second with the third and returns the original value of the second. Otherwise, returns the original value of the second. This is a generic overload for structure types.

Example Usage

using System; using System.Threading; public class Example { private static int _counter = 0; public static void Main() { // Increment the counter atomically int originalValue = Interlocked.Increment(ref _counter); Console.WriteLine($"Counter after increment: {_counter}, Original Value: {originalValue}"); // Atomically compare and exchange int expectedValue = 1; int newValue = 10; int oldValue = Interlocked.CompareExchange(ref _counter, newValue, expectedValue); if (oldValue == expectedValue) { Console.WriteLine($"Successfully updated counter from {expectedValue} to {newValue}. Old value was: {oldValue}"); } else { Console.WriteLine($"Counter was not {expectedValue}. Current value is {_counter}. Old value was: {oldValue}"); } } }