Interlocked Class
Provides atomic operations for variables that are shared by multiple threads.
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() ![]() ![]() ![]() | Add(Int32%, Int32) | Adds two 32-bit integers and replaces the first integer with the sum, as an atomic operation. |
![]() ![]() ![]() ![]() | Add(Int64%, Int64) | Adds two 64-bit integers and replaces the first integer with the sum, as an atomic operation. |
![]() ![]() ![]() ![]() | CompareExchange(Int32%, Int32, Int32) | Compares two 32-bit signed integers for equality and, if they are equal, replaces one of the values. |
![]() ![]() ![]() ![]() | CompareExchange(Int64%, Int64, Int64) | Compares two 64-bit signed integers for equality and, if they are equal, replaces one of the values. |
![]() ![]() ![]() | CompareExchange(Object%, Object, Object) | Compares two objects for reference equality and, if they are equal, replaces one of the objects. |
![]() ![]() ![]() ![]() | CompareExchange<T>(T%, T, T) | Compares two instances of the specified reference type T for equality and, if they are equal, replaces one of them. |
![]() ![]() ![]() ![]() | Decrement(Int32%) | Decrements a specified 32-bit signed integer variable and stores the result, as an atomic operation. |
![]() ![]() ![]() ![]() | Decrement(Int64%) | Decrements the specified 64-bit signed integer variable and stores the result, as an atomic operation. |
![]() ![]() ![]() ![]() | Exchange(Int32%, Int32) | Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation. |
![]() ![]() ![]() ![]() | Exchange(Int64%, Int64) | Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation. |
![]() ![]() ![]() ![]() | Exchange<T>(T%, T) | Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation. |
![]() ![]() ![]() ![]() | Increment(Int32%) | Increments a specified 32-bit signed variable and stores the result, as an atomic operation. |
![]() ![]() ![]() ![]() | Increment(Int64%) | Increments a specified 64-bit signed integer variable and stores the result, as an atomic operation. |
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:
Load a value from an instance variable into a register.
Increment or decrement the value.
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
Silverlight for Windows Phone
The following example shows a thread-safe resource locking mechanism.
Note: |
|---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

