public sealed class Interlocked
Object
Interlocked
mscorlib
BCL
The Interlocked class provides atomic operations for variables that are shared by multiple threads.
The Interlocked methods protect against errors that can occur when the scheduler switches contexts while a thread is updating a variable that can be accessed by other threads. The members of this class do not throw exceptions.[Note: The System.Threading.Interlocked.Increment(System.Int32@) method and its counterpart, System.Threading.Interlocked.Decrement(System.Int32@), increment or decrement a variable and store the resulting value, as an atomic operation.
The System.Threading.Interlocked.Exchange(System.Int32@,System.Int32) method atomically exchanges the values of the specified variables. The System.Threading.Interlocked.CompareExchange(System.Int32@,System.Int32,System.Int32) method provides an atomic operation that compares two values and stores a third value in one of the variables, based on the outcome of the comparison.
]
System.Threading Namespace
Interlocked Methods
Interlocked.CompareExchange(System.Int32&, int, int) Method
Interlocked.CompareExchange(System.Single&, float, float) Method
Interlocked.CompareExchange(System.Object&, System.Object, System.Object) Method
Interlocked.Decrement(System.Int32&) Method
Interlocked.Decrement(System.Int64&) Method
Interlocked.Exchange(System.Int32&, int) Method
Interlocked.Exchange(System.Single&, float) Method
Interlocked.Exchange(System.Object&, System.Object) Method
Interlocked.Increment(System.Int32&) Method
Interlocked.Increment(System.Int64&) Method
public static int CompareExchange(ref int location1, int value, int comparand);
Compares two Int32 values for equality and stores a specified value if they are equal.
- location1
- A Int32 reference whose value is updated with value if the original value of location1 is equal to comparand.
- value
- A Int32 whose value will replace the value of location1 if location1 and comparand are equal.
- comparand
- A Int32 to be compared to location1.
The original value of location1.
The compare and store operations are performed as an atomic operation.
System.Threading.Interlocked Class, System.Threading Namespace
public static float CompareExchange(ref float location1, float value, float comparand);
Compares two Single values for equality and stores a specified value if they are equal.
- location1
- A Single whose value is updated with value if its original value is equal to comparand.
- value
- The Single value that will replace value of location1 if location1 and comparand are equal.
- comparand
- A Single to be compared to location1.
A Single containing the original value of location1.
Exception Type Condition ArgumentNullException The address of location1 is null
.
The compare and store operations are performed as an atomic operation.
ExtendedNumerics
System.Threading.Interlocked Class, System.Threading Namespace
public static object CompareExchange(ref object location1, object value, object comparand);
Compares two Object variables for equality and stores a specified object if they are equal.
- location1
- A Object reference that is set to value if the object to which it refers is equal to comparand.
- value
- The reference that will replace the value of location1 if location1 and comparand are equal.
- comparand
- An object to be compared to that referred to by location1.
A Object containing the original value of location1.
Exception Type Condition ArgumentNullException The address of location1 is null
.
The compare and store operations are performed as an atomic operation.
System.Threading.Interlocked Class, System.Threading Namespace
public static int Decrement(ref int location);
Decrements the specified variable and stores the result as an atomic operation.
- location
- A Int32 containing the variable whose value is to be decremented.
A Int32 containing the decremented value.
This method handles an overflow condition by wrapping: if location = System.Int32.MinValue , location - 1 = System.Int32.MaxValue . No exception is thrown.
System.Threading.Interlocked Class, System.Threading Namespace
public static long Decrement(ref long location);
Decrements the specified variable and stores the result as an atomic operation.
- location
- A Int64 containing the variable whose value is to be decremented.
A Int64 containing the decremented value.
This method handles an overflow condition by wrapping: if location = System.Int64.MinValue , location - 1 = System.Int64.MaxValue . No exception is thrown.The 64-bit versions of System.Threading.Interlocked.Increment(System.Int32@) and System.Threading.Interlocked.Decrement(System.Int32@) are truly atomic only on systems where a IntPtr is 64-bits long. On other systems, these methods are atomic with respect to each other, but not with respect to other means of accessing the data.
System.Threading.Interlocked Class, System.Threading Namespace
public static int Exchange(ref int location1, int value);
Sets a Int32 variable to a specified value as an atomic operation and returns the original value.
- location1
- A Int32 variable to set to the supplied value as an atomic operation.
- value
- The Int32 value to which location1 is set.
A Int32 containing the value of location1 before the exchange.
System.Threading.Interlocked Class, System.Threading Namespace
public static float Exchange(ref float location1, float value);
Sets a Single variable to a specified value as an atomic operation and returns the original value.
- location1
- A Single variable to set to the supplied value as an atomic operation.
- value
- The Single value to which location1 is set.
A Single containing the value of location1 before the exchange.
ExtendedNumerics
System.Threading.Interlocked Class, System.Threading Namespace
public static object Exchange(ref object location1, object value);
Sets a Object reference to refer to a specified object as an atomic operation and returns a reference to the original object.
- location1
- The variable to set.
- value
- The reference to which location1 is set.
The original value of location1 .
Exception Type Condition ArgumentNullException The address of location1 is null
.
System.Threading.Interlocked Class, System.Threading Namespace
public static int Increment(ref int location);
Increments the specified variable and stores the result as an atomic operation.
- location
- A Int32 containing the variable whose value is to be incremented.
A Int32 containing the incremented value.
This method handles an overflow condition by wrapping: if location = System.Int32.MaxValue , location + 1 = System.Int32.MinValue . No exception is thrown.
System.Threading.Interlocked Class, System.Threading Namespace
public static long Increment(ref long location);
Increments the specified variable and stores the result as an atomic operation.
- location
- A Int64 containing the variable whose value is to be incremented.
A Int64 containing the incremented value.
This method handles an overflow condition by wrapping: if location = System.Int64.MaxValue , location + 1 = System.Int64.MinValue . No exception is thrown.The 64-bit versions of System.Threading.Interlocked.Increment(System.Int32@) and System.Threading.Interlocked.Decrement(System.Int32@) are truly atomic only on systems where a IntPtr is 64-bits long. On other systems, these methods are atomic with respect to each other, but not with respect to other means of accessing the data.
System.Threading.Interlocked Class, System.Threading Namespace