Provides atomic operations for variables that are shared by multiple threads.
For a list of all members of this type, see Interlocked Members.
System.Object
System.Threading.Interlocked
[Visual Basic] NotInheritable Public Class Interlocked [C#] public sealed class Interlocked [C++] public __gc __sealed class Interlocked [JScript] public class Interlocked
This type is safe for multithreaded operations.
The methods of this class 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, requiring the following steps:
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: 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.
Namespace: System.Threading
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET
Assembly: Mscorlib (in Mscorlib.dll)
Interlocked Members | System.Threading Namespace |