public sealed class Interlocked
|
The Interlocked.Increment and Interlocked.Decrement methods increment or decrement a variable and store the resulting value in a single operation. This atomic operation is useful in a multitasking operating system, where the system can interrupt one thread's execution to grant a slice of processor time to another thread. For example, without such synchronization, one thread could increment a variable but be interrupted by the system before it can check the resulting value of the variable. A second thread could then increment the same variable. When the first thread receives its next time slice, it will check the value of the variable, which has now been incremented not once but twice. The Interlocked methods protect against this kind of error.
The Interlocked.Exchange method atomically exchanges the values of the specified variables. The Interlocked.CompareExchange method combines two operations: comparing two values and storing a third value in one of the variables, based on the outcome of the comparison. The compare and exchange operations are performed as an atomic operation.
CompareExchange | Overloaded:CompareExchange(ref int location1, int value, int comparand) Compares two 32-bit signed integers for equality and, if they are equal, replaces one of the values. |
CompareExchange | Overloaded:CompareExchange(ref object location1, object value, object comparand) Compares two objects for equality and, if they are equal, replaces one of the objects. |
CompareExchange | Overloaded:CompareExchange(ref float location1, float value, float comparand) Compares two single-precision floating point numbers for equality and, if they are equal, replaces one of the values. |
CompareExchangePointer | |
Decrement | Overloaded:Decrement(ref int location) Decrements a specified variable and stores the result, as an atomic operation. |
Decrement | Overloaded:Decrement(ref long location) Decrements the specified variable and stores the result, as an atomic operation. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
Exchange | Overloaded:Exchange(ref int location1, int value) Sets a 32-bit signed integer to a specified value as an atomic operation, and returns the original value. |
Exchange | Overloaded:Exchange(ref object location1, object value) Sets an object to a specified value as an atomic operation, and returns a reference to the original object. |
Exchange | Overloaded:Exchange(ref float location1, float value) Sets a single-precision floating point number to a specified value as an atomic operation, and returns the original value. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Increment | Overloaded:Increment(ref int location) Increments a specified variable and stores the result, as an atomic operation. |
Increment | Overloaded:Increment(ref long location) Increments a specified variable and stores the result, as an atomic operation. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
location1
value
comparand
public static object CompareExchange( |
location1
value
comparand
Exception Type | Condition |
---|---|
ArgumentNullException | The address of location1 is null. |
location1
value
comparand
public static IntPtr CompareExchangePointer( |
location1
value
comparand
location
location
The 64-bit versions of Interlocked.Increment and Decrement 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.
location1
value
location1
value
Exception Type | Condition |
---|---|
ArgumentNullException | The address of location1 is null. |
location1
value
~Interlocked(); |
public virtual int GetHashCode(); |
public Type GetType(); |
location
location
The 64-bit versions of Increment and Interlocked.Decrement 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.
protected object MemberwiseClone(); |
public virtual string ToString(); |