Export (0) Print
Expand All

Interlocked.Exchange Method (Int32, Int32)

Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.

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

Public Shared Function Exchange ( _
	ByRef location1 As Integer, _
	value As Integer _
) As Integer


Type: System.Int32

The variable to set to the specified value.

Type: System.Int32

The value to which the location1 parameter is set.

Return Value

Type: System.Int32
The original value of location1.


The address of location1 is a null pointer.

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

Imports System
Imports System.Threading

Namespace InterlockedExchange_Example
    Class MyInterlockedExchangeExampleClass
        '0 for false, 1 for true. 
        Private Shared usingResource As Integer = 0

        Private Const numThreadIterations As Integer = 5
        Private Const numThreads As Integer = 10

        <MTAThread> _
        Shared Sub Main()
            Dim myThread As Thread
            Dim rnd As New Random()

            Dim i As Integer 
            For i = 0 To numThreads - 1
                myThread = New Thread(AddressOf MyThreadProc)
                myThread.Name = String.Format("Thread{0}", i + 1)

                'Wait a random amount of time before starting next thread.
                Thread.Sleep(rnd.Next(0, 1000))
            Next i
        End Sub 'Main

        Private Shared Sub MyThreadProc()
            Dim i As Integer 
            For i = 0 To numThreadIterations - 1

                'Wait 1 second before next attempt.
            Next i
        End Sub  

        'A simple method that denies reentrancy. 
        Shared Function UseResource() As Boolean 
            '0 indicates that the method is not in use. 
            If 0 = Interlocked.Exchange(usingResource, 1) Then
                Console.WriteLine("{0} acquired the lock", Thread.CurrentThread.Name)

                'Code to access a resource that is not thread safe would go here. 
                'Simulate some work

                Console.WriteLine("{0} exiting lock", Thread.CurrentThread.Name)

                'Release the lock
                Interlocked.Exchange(usingResource, 0)
                Return True 
                Console.WriteLine("   {0} was denied the lock", Thread.CurrentThread.Name)
                Return False 
            End If 
        End Function  
    End Class  
End Namespace

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft