Namespace: System.Threading | Assembly: System.Runtime
Provides methods that support atomic operations on variables that are accessible by multiple threads.
These methods are implemented using hardware-level instructions, such as the Compare-and-Swap (CAS) instruction. This means that the operations are performed in a single, uninterruptible step. This is critical for maintaining data integrity in multithreaded applications where multiple threads might try to access and modify the same data concurrently.
The Interlocked class provides methods for atomic increment, decrement, addition, subtraction, exchange, and compare-exchange operations on integer types (int, long, IntPtr, UIntPtr) and nullable integer types.
This is a generic method that works with any reference type.
Parameters:
ref T location1: A reference to the variable to compare and exchange.T value: The object that replaces the contents of location1 if the comparison is successful.T comparand: The object to compare with the contents of location1.Returns: The original value of location1.
location1 with the specified value and returns the original value.Parameters:
ref int location1: The location of the 32-bit unsigned integer to compare and exchange.int value: The value to compare with the 32-bit unsigned integer at location1.int comparand: The value to compare with the 32-bit unsigned integer at location1.Returns: The original value at location1.
location1 with the specified value and returns the original value.Parameters:
ref long location1: The location of the 64-bit signed integer to compare and exchange.long value: The value to compare with the 64-bit signed integer at location1.long comparand: The value to compare with the 64-bit signed integer at location1.Returns: The original value at location1.
This is a generic method that works with any reference type.
Parameters:
ref T location1: The variable to replace.T value: The new value for the variable.Returns: The original value of location1.
Parameters:
ref int location1: The variable to replace.int value: The new value for the variable.Returns: The original value of location1.
Parameters:
ref long location1: The variable to replace.long value: The new value for the variable.Returns: The original value of location1.
Parameters:
ref int location: The variable to increment.Returns: The value of the variable after the increment operation.
Parameters:
ref long location: The 64-bit signed integer to increment.Returns: The value of the 64-bit signed integer after the increment operation.
Parameters:
ref int location: The variable to decrement.Returns: The value of the variable after the decrement operation.
Parameters:
ref long location: The 64-bit signed integer to decrement.Returns: The value of the 64-bit signed integer after the decrement operation.
Parameters:
ref int location: The first integer to add.int value: The second integer to add.Returns: The sum of the two integers.
Parameters:
ref long location: The first 64-bit signed integer to add.long value: The second 64-bit signed integer to add.Returns: The sum of the two 64-bit signed integers.
The following example demonstrates how to use Interlocked.Increment to safely increment a counter from multiple threads:
// Assume a shared counter variable
int sharedCounter = 0;
// Number of threads to create
int numberOfThreads = 10;
// List to hold thread objects
var threads = new List<Thread>();
for (int i = 0; i < numberOfThreads; i++)
{
var thread = new Thread(() =>
{
for (int j = 0; j < 1000; j++)
{
// Safely increment the counter
Interlocked.Increment(ref sharedCounter);
}
});
threads.Add(thread);
thread.Start();
}
// Wait for all threads to complete
foreach (var thread in threads)
{
thread.Join();
}
// The final value of sharedCounter should be numberOfThreads * 1000
Console.WriteLine($"Final counter value: {sharedCounter}");
| Assembly | Framework |
|---|---|
| System.Runtime | .NET Core 1.0, .NET Framework 1.1, .NET Standard 1.0, .NET 5+, .NET 6+, .NET 7+, .NET 8+ |