Interlocked.CompareExchange
5 stars based on
50 reviews
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. Exchange method atomically exchanges the values of the specified variables.
CompareExchange method combines two operations: The compare and exchange operations are performed as an atomic operation. The destination value that will be compared with the value of the comparand parameter and will possibly be replaced.
The interlocked compareexchange 64bit 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 interlocked compareexchange 64bit respect to other means of accessing the data. The interlocked compareexchange 64bit versions of Increment and Interlocked.
Decrement are truly atomic only on systems where a IntPtr is 64 bits long. CompareExchange ref int location1, int value, int comparand Compares two bit signed integers for equality and, if they are interlocked compareexchange 64bit, replaces one of the values. CompareExchange ref object location1, object value, object comparand Compares two objects for equality and, if they are equal, replaces one of the objects.
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. Decrement ref int location Decrements a specified variable and stores the result, as an atomic operation. Decrement ref long location Decrements the specified variable and stores the result, as an atomic operation. See base class member description: Equals Derived from System.
Object, the primary base class for all objects. Exchange ref interlocked compareexchange 64bit location1, int value Sets a bit signed integer to a specified value as an atomic operation, and returns the interlocked compareexchange 64bit value. 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 ref float location1, interlocked compareexchange 64bit value Sets a single-precision floating point number to a specified value as an atomic operation, and returns the original value.
GetHashCode Derived from System. GetType Derived from System. Increment ref int location Increments a specified variable and stores the result, as an interlocked compareexchange 64bit operation. Increment ref long location Increments a specified variable and stores the result, as an atomic operation. ToString Derived from System. Finalize Derived from System. MemberwiseClone Derived from System.