This documentation is archived and is not being maintained.

Interlocked.Exchange Method (Int32%, Int32)

Updated: August 2009

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 static int Exchange(
	ref int location1,
	int value


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.

using System;
using System.Threading;

namespace InterlockedExchange_Example
    class MyInterlockedExchangeExampleClass
        //0 for false, 1 for true. 
        private static int usingResource = 0;

        private const int numThreadIterations = 5;
        private const int numThreads = 10;

        static void Main()
            Thread myThread;
            Random rnd = new Random();

            for(int i = 0; i < numThreads; i++)
                myThread = new Thread(new ThreadStart(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));

        private static void MyThreadProc()
            for(int i = 0; i < numThreadIterations; i++)

                //Wait 1 second before next attempt.

        //A simple method that denies reentrancy. 
        static bool UseResource()
            //0 indicates that the method is not in use. 
            if(0 == Interlocked.Exchange(ref usingResource, 1))
                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(ref usingResource, 0);
                return true;
                Console.WriteLine("   {0} was denied the lock", Thread.CurrentThread.Name);
                return false;


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0




August 2009

Removed unused variables from the example.

Customer feedback.