Name

Interlocked

Synopsis

The shared members of this class provide thread safety for common built-in arithmetic operations, such as increasing and decreasing a variable by one, and exchanging variables.

If two threads increment the same variable, one thread could be interrupted after both have retrieved the initial value of the variable. If this happens, then both operations store the same value, meaning that the variable has been incremented once instead of twice. The Interlocked methods protect against this kind of error. Increment() and Decrement() increase and decrease a variable by one, respectively, and Exchange() switches two variables. CompareExchange() compares the first two variables and, if true, assigns the third value to the first variable.

Public NotInheritable Class Interlocked
                  ' Public Shared Methods
   Public Shared Function CompareExchange(
        ByRef location1 As Integer, ByVal value As Integer, 
        ByVal comparand As Integer) As Integer  
   Public Shared Function CompareExchange(
        ByRef location1 As Object, ByVal value As Object, 
        ByVal comparand As Object) As Object  
   Public Shared Function CompareExchange(
        ByRef location1 As Single, ByVal value As Single, 
        ByVal comparand As Single) As Single  
   Public Shared Function Decrement(
        ByRef location As Integer) As Integer  
   Public Shared Function Decrement(
        ByRef location As Long) As Long  
   Public Shared Function Exchange(ByRef location1 As Integer, 
        ByVal value As Integer) As Integer  
   Public Shared Function Exchange(ByRef location1 As Object, ...

Get VB.NET Core Classes in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.