Interlocked Class

Interlocked Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Provides atomic operations for variables that are shared by multiple threads.

System::Object
  System.Threading::Interlocked

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

No code example is currently available or this language may not be supported.

The Interlocked type exposes the following members.

  NameDescription
Public methodStatic memberAdd(Int32%, Int32)Adds two 32-bit integers and replaces the first integer with the sum, as an atomic operation.
Public methodStatic memberAdd(Int64%, Int64)Adds two 64-bit integers and replaces the first integer with the sum, as an atomic operation.
Public methodStatic memberCompareExchange(Double%, Double, Double)Compares two double-precision floating point numbers for equality and, if they are equal, replaces one of the values.
Public methodStatic memberCompareExchange(Int32%, Int32, Int32)Compares two 32-bit signed integers for equality and, if they are equal, replaces one of the values.
Public methodStatic memberCompareExchange(Int64%, Int64, Int64)Compares two 64-bit signed integers for equality and, if they are equal, replaces one of the values.
Public methodStatic memberCompareExchange(IntPtr%, IntPtr, IntPtr)Compares two platform-specific handles or pointers for equality and, if they are equal, replaces one of them.
Public methodStatic memberCompareExchange(Object%, Object, Object)Compares two objects for reference equality and, if they are equal, replaces one of the objects.
Public methodStatic memberCompareExchange(Single%, Single, Single)Compares two single-precision floating point numbers for equality and, if they are equal, replaces one of the values.
Public methodStatic memberCompareExchange<T>(T%, T, T)Compares two instances of the specified reference type T for equality and, if they are equal, replaces one of them.
Public methodStatic memberDecrement(Int32%)Decrements a specified 32-bit signed integer variable and stores the result, as an atomic operation.
Public methodStatic memberDecrement(Int64%)Decrements the specified 64-bit signed integer variable and stores the result, as an atomic operation.
Public methodStatic memberExchange(Double%, Double)Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.
Public methodStatic memberExchange(Int32%, Int32)Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.
Public methodStatic memberExchange(Int64%, Int64)Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation.
Public methodStatic memberExchange(IntPtr%, IntPtr)Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.
Public methodStatic memberExchange(Object%, Object)Sets an object to a specified value and returns a reference to the original object, as an atomic operation.
Public methodStatic memberExchange(Single%, Single)Sets a single-precision floating point number to a specified value and returns the original value, as an atomic operation.
Public methodStatic memberExchange<T>(T%, T)Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation.
Public methodStatic memberIncrement(Int32%)Increments a specified 32-bit signed variable and stores the result, as an atomic operation.
Public methodStatic memberIncrement(Int64%)Increments a specified 64-bit signed integer variable and stores the result, as an atomic operation.
Public methodStatic memberMemoryBarrierSynchronizes memory access as follows: The processor that executes the current thread cannot reorder instructions in such a way that memory accesses before the call to MemoryBarrier execute after memory accesses that follow the call to MemoryBarrier.
Public methodStatic memberReadReturns a 64-bit value, loaded as an atomic operation.
Top

The methods of this class help 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, or when two threads are executing concurrently on separate processors. The members of this class do not throw exceptions.

The Increment and Decrement methods increment or decrement a variable and store the resulting value in a single operation. On most computers, incrementing a variable is not an atomic operation. It requires the following steps:

  1. Load a value from an instance variable into a register.

  2. Increment or decrement the value.

  3. Store the value in the instance variable.

If you do not use Increment and Decrement, a thread can be preempted after executing the first two steps. Another thread can then execute all three steps. When the first thread resumes execution, it overwrites the value in the instance variable, and the effect of the increment or decrement performed by the second thread is lost.

The Exchange method atomically exchanges the values of the specified variables. The CompareExchange method combines two operations: It compares two values and stores a third value in one of the variables, based on the outcome of the comparison. The comparison and the exchange are performed as an atomic operation.

Version Notes

Windows Phone

 64-bit members of the Interlocked class are present but not supported.

The following example shows a thread-safe resource locking mechanism.

No code example is currently available or this language may not be supported.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

This type is thread safe.

Show:
© 2017 Microsoft