Fetching contributors…
Cannot retrieve contributors at this time
63 lines (43 sloc) 5.64 KB
title dev_langs helpviewer_keywords ms.assetid author
Interlocked Operations
Interlocked class, about Interlocked class
threading [.NET Framework], Interlocked class

Interlocked operations

The xref:System.Threading.Interlocked?displayProperty=nameWithType class provides methods that synchronize access to a variable that is shared by multiple threads. The threads of different processes can use this mechanism if the variable is in shared memory. Interlocked operations are atomic — that is, the entire operation is a unit that cannot be interrupted by another operation on the same variable. This is important in operating systems with preemptive multithreading, where a thread can be suspended after loading a value from a memory address, but before having the chance to alter it and store it.

The xref:System.Threading.Interlocked class provides the following operations:

  • The xref:System.Threading.Interlocked.Add%2A?displayProperty=nameWithType method adds an integer value to a variable and returns the new value of the variable.

  • The xref:System.Threading.Interlocked.Read%2A?displayProperty=nameWithType method reads a 64-bit integer value as an atomic operation. This is useful on 32-bit operating systems, where reading a 64-bit integer is not ordinarily an atomic operation.

  • The xref:System.Threading.Interlocked.Increment%2A?displayProperty=nameWithType and xref:System.Threading.Interlocked.Decrement%2A?displayProperty=nameWithType methods increment or decrement a variable and return the resulting value.

  • The xref:System.Threading.Interlocked.Exchange%2A?displayProperty=nameWithType method performs an atomic exchange of the value in a specified variable, returning that value and replacing it with a new value. Use a generic xref:System.Threading.Interlocked.Exchange%60%601%28%60%600%40%2C%60%600%29 overload of this method to perform the exchange on a variable of any reference type.

  • The xref:System.Threading.Interlocked.CompareExchange%2A?displayProperty=nameWithType method also exchanges two values, but contingent on the result of a comparison. Use a generic xref:System.Threading.Interlocked.CompareExchange%60%601%28%60%600%40%2C%60%600%2C%60%600%29 overload of this method to perform the exchange on a variable of any reference type.

On modern processors, the methods of the xref:System.Threading.Interlocked class can often be implemented by a single instruction. Thus, they provide very high-performance synchronization and can be used to build higher-level synchronization mechanisms, like spin locks.

For an example that uses the xref:System.Threading.Monitor and xref:System.Threading.Interlocked classes in combination, see xref:System.Threading.Monitor.

CompareExchange example

The xref:System.Threading.Interlocked.CompareExchange%2A method can be used to protect computations that are more complicated than simple increment and decrement. The following example demonstrates a thread-safe method that adds to a running total stored as a floating point number. (For integers, the xref:System.Threading.Interlocked.Add%2A method is a simpler solution.) For complete code examples, see the overloads of xref:System.Threading.Interlocked.CompareExchange%2A that take single-precision and double-precision floating-point arguments (xref:System.Threading.Interlocked.CompareExchange%28System.Single%40%2CSystem.Single%2CSystem.Single%29 and xref:System.Threading.Interlocked.CompareExchange%28System.Double%40%2CSystem.Double%2CSystem.Double%29).

[!code-cppConceptual.Interlocked#1] [!code-csharpConceptual.Interlocked#1] [!code-vbConceptual.Interlocked#1]

Untyped overloads of Exchange and CompareExchange

The xref:System.Threading.Interlocked.Exchange%2A and xref:System.Threading.Interlocked.CompareExchange%2A methods have overloads that take arguments of type xref:System.Object. The first argument of each of these overloads is ref Object (ByRef … As Object in Visual Basic), and type safety requires the variable passed to this argument to be typed strictly as xref:System.Object; you cannot simply cast the first argument to type xref:System.Object when calling these methods.

[!NOTE] In the .NET Framework version 2.0 and later, use the generic overloads of the xref:System.Threading.Interlocked.Exchange%2A and xref:System.Threading.Interlocked.CompareExchange%2A methods to exchange strongly typed variables.

The following code example shows a property of type ClassA that can be set only once, as it might be implemented in the .NET Framework version 1.0 or 1.1.

[!code-cppConceptual.Interlocked#2] [!code-csharpConceptual.Interlocked#2] [!code-vbConceptual.Interlocked#2]

See also