Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Interlocked.Exchange Method

Sets a variable to a specified value as an atomic operation.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Exchange(ByRef Integer, Integer) As Integer
[C#] public static int Exchange(ref int, int);
[C++] public: static int Exchange(int*, int*);
[JScript] public static function Exchange(int, int) : int;

Sets an object to a specified value as an atomic operation, and returns a reference to the original object.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Exchange(ByRef Object, Object) As Object
[C#] public static object Exchange(ref object, object);
[C++] public: static Object* Exchange(Object**, Object**);
[JScript] public static function Exchange(Object, Object) : Object;

Sets a single-precision floating point number to a specified value as an atomic operation, and returns the original value.

[Visual Basic] Overloads Public Shared Function Exchange(ByRef Single, Single) As Single
[C#] public static float Exchange(ref float, float);
[C++] public: static float Exchange(float*, float*);
[JScript] public static function Exchange(float, float) : float;

Example

[Visual Basic, C#, C++] The following code example shows the syntax for using Exchange with any reference type object.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Exchange. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Threading

Public Class AtomicTest

    Shared Sub Main()
        Dim atomicExchange As New AtomicExchange()
        Dim firstThread As New Thread(AddressOf atomicExchange.Switch)
        firstThread.Start()
    End Sub

End Class

Public Class AtomicExchange

    Public Class SomeType
    End Class

    ' To use Interlocked.Exchange, someType1 
    ' must be declared as type Object.
    Dim someType1 As Object   
    Dim someType2 As SomeType 

    Sub New() 
        someType1 = New SomeType()
        someType2 = New SomeType()
    End Sub

    Sub Switch()
        someType2 = CType(Interlocked.Exchange( _
            someType1, CType(someType2, Object)), SomeType)
    End Sub

End Class

[C#] 
using System;
using System.Threading;

class AtomicTest
{
    static void Main()
    {
        AtomicExchange atomicExchange = new AtomicExchange();
        Thread firstThread = 
            new Thread(new ThreadStart(atomicExchange.Switch));
        firstThread.Start();
    }
}

class AtomicExchange
{
    class SomeType{}

    // To use Interlocked.Exchange, someType1 
    // must be declared as type Object.
    object   someType1;
    SomeType someType2;

    public AtomicExchange() 
    {
        someType1 = new SomeType();
        someType2 = new SomeType();
    }

    public void Switch()
    {
        someType2 = (SomeType)Interlocked.Exchange(
            ref someType1, (object)someType2);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;


__gc class AtomicExchange
{
    __gc class SomeType{};

    // To use Interlocked::Exchange, someType1 
    // must be declared as type Object*.
    Object*   someType1;
    SomeType* someType2;

public:
    AtomicExchange() 
    {
        someType1 = new SomeType();
        someType2 = new SomeType();
    }

    void Switch()
    {
        someType2 = dynamic_cast<SomeType*>(Interlocked::Exchange(
            &someType1, dynamic_cast<Object*>(someType2)));
    }
};

void main()
{
    AtomicExchange* atomicExchange = new AtomicExchange();
    Thread* firstThread = new Thread(
        new ThreadStart(atomicExchange, AtomicExchange::Switch));
    firstThread->Start();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Interlocked Class | Interlocked Members | System.Threading Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.