Interlocked.CompareExchange Interlocked.CompareExchange Interlocked.CompareExchange Interlocked.CompareExchange Method

정의

두 값이 같은지 비교하여 같으면 첫 번째 값을 바꿉니다.Compares two values for equality and, if they are equal, replaces the first value.

오버로드

CompareExchange(Double, Double, Double) CompareExchange(Double, Double, Double) CompareExchange(Double, Double, Double) CompareExchange(Double, Double, Double)

두 배 정밀도 부동 소수점 숫자가 같은지 비교하여 같으면 두 값 중 하나를 바꿉니다.Compares two double-precision floating point numbers for equality and, if they are equal, replaces the first value.

CompareExchange(Int32, Int32, Int32) CompareExchange(Int32, Int32, Int32) CompareExchange(Int32, Int32, Int32) CompareExchange(Int32, Int32, Int32)

두 개의 부호 있는 32비트 정수가 같은지 비교하여 같으면 첫 번째 값을 바꿉니다.Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.

CompareExchange(Int64, Int64, Int64) CompareExchange(Int64, Int64, Int64) CompareExchange(Int64, Int64, Int64) CompareExchange(Int64, Int64, Int64)

두 개의 부호 있는 64비트 정수가 같은지 비교하여 같으면 첫 번째 값을 바꿉니다.Compares two 64-bit signed integers for equality and, if they are equal, replaces the first value.

CompareExchange(IntPtr, IntPtr, IntPtr) CompareExchange(IntPtr, IntPtr, IntPtr) CompareExchange(IntPtr, IntPtr, IntPtr) CompareExchange(IntPtr, IntPtr, IntPtr)

두 플랫폼별 핸들이나 포인터가 같은지 비교하고, 같으면 첫 번째 값을 바꿉니다.Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one.

CompareExchange(Object, Object, Object) CompareExchange(Object, Object, Object) CompareExchange(Object, Object, Object) CompareExchange(Object, Object, Object)

두 개체의 참조가 같은지 비교하고, 같으면 첫 번째 개체를 바꿉니다.Compares two objects for reference equality and, if they are equal, replaces the first object.

CompareExchange(Single, Single, Single) CompareExchange(Single, Single, Single) CompareExchange(Single, Single, Single) CompareExchange(Single, Single, Single)

두 단정밀도 부동 소수점 숫자가 같은지 비교하여 같으면 첫 번째 값을 바꿉니다.Compares two single-precision floating point numbers for equality and, if they are equal, replaces the first value.

CompareExchange<T>(T, T, T) CompareExchange<T>(T, T, T) CompareExchange<T>(T, T, T) CompareExchange<T>(T, T, T)

지정된 참조 형식 T의 두 인스턴스에 대한 참조가 같은지 비교하고, 같으면 첫 번째 값을 바꿉니다.Compares two instances of the specified reference type T for reference equality and, if they are equal, replaces the first one.

CompareExchange(Double, Double, Double) CompareExchange(Double, Double, Double) CompareExchange(Double, Double, Double) CompareExchange(Double, Double, Double)

두 배 정밀도 부동 소수점 숫자가 같은지 비교하여 같으면 두 값 중 하나를 바꿉니다.Compares two double-precision floating point numbers for equality and, if they are equal, replaces the first value.

public:
 static double CompareExchange(double % location1, double value, double comparand);
public static double CompareExchange (ref double location1, double value, double comparand);
static member CompareExchange : double * double * double -> double
Public Shared Function CompareExchange (ByRef location1 As Double, value As Double, comparand As Double) As Double

매개 변수

location1
Double Double Double Double

comparand와 비교하여 바뀔 수 있는 값을 가진 대상입니다.The destination, whose value is compared with comparand and possibly replaced.

value
Double Double Double Double

비교 결과가 같은 경우 대상 값을 바꿀 값입니다.The value that replaces the destination value if the comparison results in equality.

comparand
Double Double Double Double

location1의 값과 비교할 값입니다.The value that is compared to the value at location1.

반환

location1의 원래 값입니다.The original value in location1.

예외

location1의 주소는 null 포인터입니다.The address of location1 is a null pointer.

예제

다음 코드 예제를 실행 중인 총 누적 스레드로부터 안전한 메서드를 보여 줍니다. Double 값입니다.The following code example demonstrates a thread-safe method that accumulates a running total of Double values. 두 개의 스레드를 일련의 추가 Double 일반적인 더하기를 확인 하 고 스레드로부터 안전한 메서드를 사용 하 여 값의 합계를 비교 하는 스레드가 완료 합니다.Two threads add a series of Double values using the thread-safe method and ordinary addition, and when the threads complete the totals are compared. 이중 프로세서 컴퓨터에서는 합계에는 큰 차이가 있습니다.On a dual-processor computer, there is a significant difference in the totals.

스레드로부터 안전한 메서드에 누계의 초기 값이 저장 고를 CompareExchange 메서드 누계 새로 계산 된 총 exchange를 사용 합니다.In the thread-safe method, the initial value of the running total is saved, and then the CompareExchange method is used to exchange the newly computed total with the old total. 반환 값의 누계 저장 된 값과 같으면 없는 경우 다음 다른 스레드가 업데이트 총 그동안.If the return value is not equal to the saved value of the running total, then another thread has updated the total in the meantime. 이 경우 누계를 업데이트 하려는 시도 반복 되어야 합니다.In that case, the attempt to update the running total must be repeated.

// This example demonstrates a thread-safe method that adds to a
// running total.  
using System;
using System.Threading;

public class ThreadSafe
{
    // Field totalValue contains a running total that can be updated
    // by multiple threads. It must be protected from unsynchronized 
    // access.
    private double totalValue = 0.0;

    // The Total property returns the running total.
    public double Total { get { return totalValue; }}

    // AddToTotal safely adds a value to the running total.
    public double AddToTotal(double addend)
    {
        double initialValue, computedValue;
        do
        {
            // Save the current running total in a local variable.
            initialValue = totalValue;

            // Add the new value to the running total.
            computedValue = initialValue + addend;

            // CompareExchange compares totalValue to initialValue. If
            // they are not equal, then another thread has updated the
            // running total since this loop started. CompareExchange
            // does not update totalValue. CompareExchange returns the
            // contents of totalValue, which do not equal initialValue,
            // so the loop executes again.
        }
        while (initialValue != Interlocked.CompareExchange(ref totalValue, 
            computedValue, initialValue));
        // If no other thread updated the running total, then 
        // totalValue and initialValue are equal when CompareExchange
        // compares them, and computedValue is stored in totalValue.
        // CompareExchange returns the value that was in totalValue
        // before the update, which is equal to initialValue, so the 
        // loop ends.

        // The function returns computedValue, not totalValue, because
        // totalValue could be changed by another thread between
        // the time the loop ends and the function returns.
        return computedValue;
    }
}

public class Test
{
    // Create an instance of the ThreadSafe class to test.
    private static ThreadSafe ts = new ThreadSafe();
    private static double control;

    private static Random r = new Random();
    private static ManualResetEvent mre = new ManualResetEvent(false);

    public static void Main()
    {
        // Create two threads, name them, and start them. The
        // thread will block on mre.
        Thread t1 = new Thread(TestThread);
        t1.Name = "Thread 1";
        t1.Start();
        Thread t2 = new Thread(TestThread);
        t2.Name = "Thread 2";
        t2.Start();

        // Now let the threads begin adding random numbers to 
        // the total.
        mre.Set();
        
        // Wait until all the threads are done.
        t1.Join();
        t2.Join();

        Console.WriteLine("Thread safe: {0}  Ordinary Double: {1}", 
            ts.Total, control);
    }

    private static void TestThread()
    {
        // Wait until the signal.
        mre.WaitOne();

        for(int i = 1; i <= 1000000; i++)
        {
            // Add to the running total in the ThreadSafe instance, and
            // to an ordinary double.
            //
            double testValue = r.NextDouble();
            control += testValue;
            ts.AddToTotal(testValue);
        }
    }
}

/* On a dual-processor computer, this code example produces output 
   similar to the following:

Thread safe: 998068.049623744  Ordinary Double: 759775.417190589
 */
' This example demonstrates a thread-safe method that adds to a
' running total.  
Imports System.Threading

Public Class ThreadSafe
    ' Field totalValue contains a running total that can be updated
    ' by multiple threads. It must be protected from unsynchronized 
    ' access.
    Private totalValue As Double = 0.0

    ' The Total property returns the running total.
    Public ReadOnly Property Total As Double
        Get
            Return totalValue
        End Get
    End Property

    ' AddToTotal safely adds a value to the running total.
    Public Function AddToTotal(ByVal addend As Double) As Double
        Dim initialValue, computedValue As Double
        Do
            ' Save the current running total in a local variable.
            initialValue = totalValue

            ' Add the new value to the running total.
            computedValue = initialValue + addend

            ' CompareExchange compares totalValue to initialValue. If
            ' they are not equal, then another thread has updated the
            ' running total since this loop started. CompareExchange
            ' does not update totalValue. CompareExchange returns the
            ' contents of totalValue, which do not equal initialValue,
            ' so the loop executes again.
        Loop While initialValue <> Interlocked.CompareExchange( _
            totalValue, computedValue, initialValue)
        ' If no other thread updated the running total, then 
        ' totalValue and initialValue are equal when CompareExchange
        ' compares them, and computedValue is stored in totalValue.
        ' CompareExchange returns the value that was in totalValue
        ' before the update, which is equal to initialValue, so the 
        ' loop ends.

        ' The function returns computedValue, not totalValue, because
        ' totalValue could be changed by another thread between
        ' the time the loop ends and the function returns.
        Return computedValue
    End Function
End Class

Public Class Test
    ' Create an instance of the ThreadSafe class to test.
    Private Shared ts As New ThreadSafe()
    Private Shared control As Double

    Private Shared r As New Random()
    Private Shared mre As New ManualResetEvent(false)

    <MTAThread> _
    Public Shared Sub Main()
        ' Create two threads, name them, and start them. The
        ' threads will block on mre.
        Dim t1 As New Thread(AddressOf TestThread)
        t1.Name = "Thread 1"
        t1.Start()
        Dim t2 As New Thread(AddressOf TestThread)
        t2.Name = "Thread 2"
        t2.Start()

        ' Now let the threads begin adding random numbers to 
        ' the total.
        mre.Set()
        
        ' Wait until all the threads are done.
        t1.Join()
        t2.Join()

        Console.WriteLine("Thread safe: {0}  Ordinary Double: {1}", ts.Total, control)
    End Sub

    Private Shared Sub TestThread()
        ' Wait until the signal.
        mre.WaitOne()

        For i As Integer = 1 to 1000000
            ' Add to the running total in the ThreadSafe instance, and
            ' to an ordinary double.
            '
            Dim testValue As Double = r.NextDouble
            control += testValue
            ts.AddToTotal(testValue)
        Next
    End Sub
End Class

' On a dual-processor computer, this code example produces output 
' similar to the following:
'
'Thread safe: 998068.049623744  Ordinary Double: 759775.417190589

설명

하는 경우 comparand 에 값 location1 가 같으면 value 에 저장 된 location1합니다.If comparand and the value in location1 are equal, then value is stored in location1. 그렇지 않으면 작업이 수행되지 않습니다.Otherwise, no operation is performed. 비교 및 교환 작업을 원자성 작업으로 수행 됩니다.The compare and exchange operations are performed as an atomic operation. 반환 값 CompareExchange 에서 원래 값인 location1교환이 이루어지는 여부.The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

추가 정보

CompareExchange(Int32, Int32, Int32) CompareExchange(Int32, Int32, Int32) CompareExchange(Int32, Int32, Int32) CompareExchange(Int32, Int32, Int32)

두 개의 부호 있는 32비트 정수가 같은지 비교하여 같으면 첫 번째 값을 바꿉니다.Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.

public:
 static int CompareExchange(int % location1, int value, int comparand);
public static int CompareExchange (ref int location1, int value, int comparand);
static member CompareExchange : int * int * int -> int
Public Shared Function CompareExchange (ByRef location1 As Integer, value As Integer, comparand As Integer) As Integer

매개 변수

location1
Int32 Int32 Int32 Int32

comparand와 비교하여 바뀔 수 있는 값을 가진 대상입니다.The destination, whose value is compared with comparand and possibly replaced.

value
Int32 Int32 Int32 Int32

비교 결과가 같은 경우 대상 값을 바꿀 값입니다.The value that replaces the destination value if the comparison results in equality.

comparand
Int32 Int32 Int32 Int32

location1의 값과 비교할 값입니다.The value that is compared to the value at location1.

반환

location1의 원래 값입니다.The original value in location1.

예외

location1의 주소는 null 포인터입니다.The address of location1 is a null pointer.

예제

다음 코드 예제에서는 누계를 누적 하는 스레드로부터 안전한 메서드를 보여 줍니다.The following code example demonstrates a thread-safe method that accumulates a running total. 누계의 초기 값이 저장 차례로 CompareExchange 메서드 누계 새로 계산 된 총 exchange를 사용 합니다.The initial value of the running total is saved, and then the CompareExchange method is used to exchange the newly computed total with the old total. 반환 값의 누계 저장 된 값과 같으면 없는 경우 다음 다른 스레드가 업데이트 총 그동안.If the return value is not equal to the saved value of the running total, then another thread has updated the total in the meantime. 이 경우 누계를 업데이트 하려는 시도 반복 되어야 합니다.In that case, the attempt to update the running total must be repeated.

참고

Add 메서드를.NET Framework 버전 2.0에에서 도입 된 정수에 대 한 누계 스레드로부터 안전한 누적 하는 보다 편리한 방법을 제공 합니다.The Add method, introduced in version 2.0 of the .NET Framework, provides a more convenient way to accumulate thread-safe running totals for integers.

// This example demonstrates a thread-safe method that adds to a
// running total.  It cannot be run directly.  You can compile it
// as a library, or add the class to a project.
#using <system.dll>

using namespace System::Threading;
public ref class ThreadSafe
{
private:

   // totalValue contains a running total that can be updated
   // by multiple threads. It must be protected from unsynchronized 
   // access.
   int totalValue;

public:

   property int Total 
   {

      // The Total property returns the running total.
      int get()
      {
         return totalValue;
      }

   }

   // AddToTotal safely adds a value to the running total.
   int AddToTotal( int addend )
   {
      int initialValue;
      int computedValue;
      do
      {
         
         // Save the current running total in a local variable.
         initialValue = totalValue;
         
         // Add the new value to the running total.
         computedValue = initialValue + addend;
         
         // CompareExchange compares totalValue to initialValue. If
         // they are not equal, then another thread has updated the
         // running total since this loop started. CompareExchange
         // does not update totalValue. CompareExchange returns the
         // contents of totalValue, which do not equal initialValue,
         // so the loop executes again.
      }
      while ( initialValue != Interlocked::CompareExchange( totalValue, computedValue, initialValue ) );

      
      // If no other thread updated the running total, then 
      // totalValue and initialValue are equal when CompareExchange
      // compares them, and computedValue is stored in totalValue.
      // CompareExchange returns the value that was in totalValue
      // before the update, which is equal to initialValue, so the 
      // loop ends.
      // The function returns computedValue, not totalValue, because
      // totalValue could be changed by another thread between
      // the time the loop ends and the function returns.
      return computedValue;
   }

};

// This example demonstrates a thread-safe method that adds to a
// running total.  It cannot be run directly.  You can compile it
// as a library, or add the class to a project.
using System.Threading;

public class ThreadSafe {
    // totalValue contains a running total that can be updated
    // by multiple threads. It must be protected from unsynchronized 
    // access.
    private int totalValue = 0;

    // The Total property returns the running total.
    public int Total {
        get { return totalValue; }
    }

    // AddToTotal safely adds a value to the running total.
    public int AddToTotal(int addend) {
        int initialValue, computedValue;
        do {
            // Save the current running total in a local variable.
            initialValue = totalValue;

            // Add the new value to the running total.
            computedValue = initialValue + addend;

            // CompareExchange compares totalValue to initialValue. If
            // they are not equal, then another thread has updated the
            // running total since this loop started. CompareExchange
            // does not update totalValue. CompareExchange returns the
            // contents of totalValue, which do not equal initialValue,
            // so the loop executes again.
        } while (initialValue != Interlocked.CompareExchange(
            ref totalValue, computedValue, initialValue));
        // If no other thread updated the running total, then 
        // totalValue and initialValue are equal when CompareExchange
        // compares them, and computedValue is stored in totalValue.
        // CompareExchange returns the value that was in totalValue
        // before the update, which is equal to initialValue, so the 
        // loop ends.

        // The function returns computedValue, not totalValue, because
        // totalValue could be changed by another thread between
        // the time the loop ends and the function returns.
        return computedValue;
    }
}
' This example demonstrates a thread-safe method that adds to a
' running total.  It cannot be run directly.  You can compile it
' as a library, or add the class to a project.
Imports System.Threading

Public Class ThreadSafe
    ' Field totalValue contains a running total that can be updated
    ' by multiple threads. It must be protected from unsynchronized 
    ' access.
    Private totalValue As Integer = 0

    ' The Total property returns the running total.
    Public ReadOnly Property Total As Integer
        Get
            Return totalValue
        End Get
    End Property

    ' AddToTotal safely adds a value to the running total.
    Public Function AddToTotal(ByVal addend As Integer) As Integer
        Dim initialValue, computedValue As Integer
        Do
            ' Save the current running total in a local variable.
            initialValue = totalValue

            ' Add the new value to the running total.
            computedValue = initialValue + addend

            ' CompareExchange compares totalValue to initialValue. If
            ' they are not equal, then another thread has updated the
            ' running total since this loop started. CompareExchange
            ' does not update totalValue. CompareExchange returns the
            ' contents of totalValue, which do not equal initialValue,
            ' so the loop executes again.
        Loop While initialValue <> Interlocked.CompareExchange( _
            totalValue, computedValue, initialValue)
        ' If no other thread updated the running total, then 
        ' totalValue and initialValue are equal when CompareExchange
        ' compares them, and computedValue is stored in totalValue.
        ' CompareExchange returns the value that was in totalValue
        ' before the update, which is equal to initialValue, so the 
        ' loop ends.

        ' The function returns computedValue, not totalValue, because
        ' totalValue could be changed by another thread between
        ' the time the loop ends and the function returns.
        Return computedValue
    End Function
End Class

설명

하는 경우 comparand 에 값 location1 가 같으면 value 에 저장 된 location1합니다.If comparand and the value in location1 are equal, then value is stored in location1. 그렇지 않으면 작업이 수행되지 않습니다.Otherwise, no operation is performed. 비교 및 교환 작업을 원자성 작업으로 수행 됩니다.The compare and exchange operations are performed as an atomic operation. 반환 값 CompareExchange 에서 원래 값인 location1교환이 이루어지는 여부.The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

추가 정보

CompareExchange(Int64, Int64, Int64) CompareExchange(Int64, Int64, Int64) CompareExchange(Int64, Int64, Int64) CompareExchange(Int64, Int64, Int64)

두 개의 부호 있는 64비트 정수가 같은지 비교하여 같으면 첫 번째 값을 바꿉니다.Compares two 64-bit signed integers for equality and, if they are equal, replaces the first value.

public:
 static long CompareExchange(long % location1, long value, long comparand);
public static long CompareExchange (ref long location1, long value, long comparand);
static member CompareExchange : int64 * int64 * int64 -> int64
Public Shared Function CompareExchange (ByRef location1 As Long, value As Long, comparand As Long) As Long

매개 변수

location1
Int64 Int64 Int64 Int64

comparand와 비교하여 바뀔 수 있는 값을 가진 대상입니다.The destination, whose value is compared with comparand and possibly replaced.

value
Int64 Int64 Int64 Int64

비교 결과가 같은 경우 대상 값을 바꿀 값입니다.The value that replaces the destination value if the comparison results in equality.

comparand
Int64 Int64 Int64 Int64

location1의 값과 비교할 값입니다.The value that is compared to the value at location1.

반환

location1의 원래 값입니다.The original value in location1.

예외

location1의 주소는 null 포인터입니다.The address of location1 is a null pointer.

설명

하는 경우 comparand 에 값 location1 가 같으면 value 에 저장 된 location1합니다.If comparand and the value in location1 are equal, then value is stored in location1. 그렇지 않으면 작업이 수행되지 않습니다.Otherwise, no operation is performed. 비교 및 교환 작업을 원자성 작업으로 수행 됩니다.The compare and exchange operations are performed as an atomic operation. 반환 값 CompareExchange 에서 원래 값인 location1교환이 이루어지는 여부.The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

추가 정보

CompareExchange(IntPtr, IntPtr, IntPtr) CompareExchange(IntPtr, IntPtr, IntPtr) CompareExchange(IntPtr, IntPtr, IntPtr) CompareExchange(IntPtr, IntPtr, IntPtr)

두 플랫폼별 핸들이나 포인터가 같은지 비교하고, 같으면 첫 번째 값을 바꿉니다.Compares two platform-specific handles or pointers for equality and, if they are equal, replaces the first one.

public:
 static IntPtr CompareExchange(IntPtr % location1, IntPtr value, IntPtr comparand);
public static IntPtr CompareExchange (ref IntPtr location1, IntPtr value, IntPtr comparand);
static member CompareExchange : nativeint * nativeint * nativeint -> nativeint
Public Shared Function CompareExchange (ByRef location1 As IntPtr, value As IntPtr, comparand As IntPtr) As IntPtr

매개 변수

location1
IntPtr IntPtr IntPtr IntPtr

comparand 값과 비교되어 value로 바뀔 수 있는 값을 가진 대상 IntPtr입니다.The destination IntPtr, whose value is compared with the value of comparand and possibly replaced by value.

value
IntPtr IntPtr IntPtr IntPtr

비교 결과가 같은 경우 대상 값을 바꿀 IntPtr입니다.The IntPtr that replaces the destination value if the comparison results in equality.

comparand
IntPtr IntPtr IntPtr IntPtr

location1의 값과 비교할 IntPtr입니다.The IntPtr that is compared to the value at location1.

반환

location1의 원래 값입니다.The original value in location1.

예외

location1의 주소는 null 포인터입니다.The address of location1 is a null pointer.

설명

하는 경우 comparand 에 값 location1 가 같으면 value 에 저장 된 location1합니다.If comparand and the value in location1 are equal, then value is stored in location1. 그렇지 않으면 작업이 수행되지 않습니다.Otherwise, no operation is performed. 비교 및 교환 작업을 원자성 작업으로 수행 됩니다.The compare and exchange operations are performed as an atomic operation. 이 메서드의 반환 값은 원래 값에 location1교환이 이루어지는 여부.The return value of this method is the original value in location1, whether or not the exchange takes place.

참고

IntPtr 플랫폼별 형식이입니다.IntPtr is a platform-specific type.

추가 정보

CompareExchange(Object, Object, Object) CompareExchange(Object, Object, Object) CompareExchange(Object, Object, Object) CompareExchange(Object, Object, Object)

두 개체의 참조가 같은지 비교하고, 같으면 첫 번째 개체를 바꿉니다.Compares two objects for reference equality and, if they are equal, replaces the first object.

public:
 static System::Object ^ CompareExchange(System::Object ^ % location1, System::Object ^ value, System::Object ^ comparand);
public static object CompareExchange (ref object location1, object value, object comparand);
static member CompareExchange : obj * obj * obj -> obj
Public Shared Function CompareExchange (ByRef location1 As Object, value As Object, comparand As Object) As Object

매개 변수

location1
Object Object Object Object

참조에 의해 comparand와 비교되어 바뀔 수 있는 대상 개체입니다.The destination object that is compared by reference with comparand and possibly replaced.

value
Object Object Object Object

참조 비교 결과가 같은 경우 대상 개체를 바꾸는 개체입니다.The object that replaces the destination object if the reference comparison results in equality.

comparand
Object Object Object Object

참조에 의해 location1에서 개체와 비교되는 개체입니다.The object that is compared by reference to the object at location1.

반환

location1의 원래 값입니다.The original value in location1.

예외

location1의 주소는 null 포인터입니다.The address of location1 is a null pointer.

설명

중요

.NET Framework 2.0 부터 CompareExchange<T>(T, T, T) 메서드 오버로드에서는 참조 형식에 대해 형식이 안전한 대안을 제공합니다.Beginning with .NET Framework 2.0, the CompareExchange<T>(T, T, T) method overload provides a type-safe alternative for reference types. 이 오버 로드 대신 호출 하는 것이 좋습니다.We recommend that you call it instead of this overload.

하는 경우 comparand 의 개체와 location1 같은지 참조 한 다음 value 에 저장 됩니다 location1합니다.If comparand and the object in location1 are equal by reference, then value is stored in location1. 그렇지 않으면 작업이 수행되지 않습니다.Otherwise, no operation is performed. 비교 및 교환 작업을 원자성 작업으로 수행 됩니다.The compare and exchange operations are performed as an atomic operation. 반환 값 CompareExchange 에서 원래 값인 location1교환이 이루어지는 여부.The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

참고

값 같음이 아니라 참조 같음에 대 한 개체 비교 됩니다.The objects are compared for reference equality rather than value equality. 결과적으로, 동일한 두 boxed 인스턴스 값 형식 (예: 정수 3) 항상 같지 않은 것으로 표시 되 고 아무 작업도 수행 합니다.As a result, two boxed instances of the same value type (for example, the integer 3) always appear to be unequal and no operation is performed. 값 형식과이 오버 로드를 사용 하지 마세요.Do not use this overload with value types.

추가 정보

CompareExchange(Single, Single, Single) CompareExchange(Single, Single, Single) CompareExchange(Single, Single, Single) CompareExchange(Single, Single, Single)

두 단정밀도 부동 소수점 숫자가 같은지 비교하여 같으면 첫 번째 값을 바꿉니다.Compares two single-precision floating point numbers for equality and, if they are equal, replaces the first value.

public:
 static float CompareExchange(float % location1, float value, float comparand);
public static float CompareExchange (ref float location1, float value, float comparand);
static member CompareExchange : single * single * single -> single
Public Shared Function CompareExchange (ByRef location1 As Single, value As Single, comparand As Single) As Single

매개 변수

location1
Single Single Single Single

comparand와 비교하여 바뀔 수 있는 값을 가진 대상입니다.The destination, whose value is compared with comparand and possibly replaced.

value
Single Single Single Single

비교 결과가 같은 경우 대상 값을 바꿀 값입니다.The value that replaces the destination value if the comparison results in equality.

comparand
Single Single Single Single

location1의 값과 비교할 값입니다.The value that is compared to the value at location1.

반환

location1의 원래 값입니다.The original value in location1.

예외

location1의 주소는 null 포인터입니다.The address of location1 is a null pointer.

예제

다음 코드 예제를 실행 중인 총 누적 스레드로부터 안전한 메서드를 보여 줍니다. Single 값입니다.The following code example demonstrates a thread-safe method that accumulates a running total of Single values. 두 개의 스레드를 일련의 추가 Single 일반적인 더하기를 확인 하 고 스레드로부터 안전한 메서드를 사용 하 여 값의 합계를 비교 하는 스레드가 완료 합니다.Two threads add a series of Single values using the thread-safe method and ordinary addition, and when the threads complete the totals are compared. 이중 프로세서 컴퓨터에서는 합계에는 큰 차이가 있습니다.On a dual-processor computer, there is a significant difference in the totals.

스레드로부터 안전한 메서드에 누계의 초기 값이 저장 고를 CompareExchange 메서드 누계 새로 계산 된 총 exchange를 사용 합니다.In the thread-safe method, the initial value of the running total is saved, and then the CompareExchange method is used to exchange the newly computed total with the old total. 반환 값의 누계 저장 된 값과 같으면 없는 경우 다음 다른 스레드가 업데이트 총 그동안.If the return value is not equal to the saved value of the running total, then another thread has updated the total in the meantime. 이 경우 누계를 업데이트 하려는 시도 반복 되어야 합니다.In that case, the attempt to update the running total must be repeated.

// This example demonstrates a thread-safe method that adds to a
// running total.  
using System;
using System.Threading;

public class ThreadSafe
{
    // Field totalValue contains a running total that can be updated
    // by multiple threads. It must be protected from unsynchronized 
    // access.
    private float totalValue = 0.0F;

    // The Total property returns the running total.
    public float Total { get { return totalValue; }}

    // AddToTotal safely adds a value to the running total.
    public float AddToTotal(float addend)
    {
        float initialValue, computedValue;
        do
        {
            // Save the current running total in a local variable.
            initialValue = totalValue;

            // Add the new value to the running total.
            computedValue = initialValue + addend;

            // CompareExchange compares totalValue to initialValue. If
            // they are not equal, then another thread has updated the
            // running total since this loop started. CompareExchange
            // does not update totalValue. CompareExchange returns the
            // contents of totalValue, which do not equal initialValue,
            // so the loop executes again.
        }
        while (initialValue != Interlocked.CompareExchange(ref totalValue, 
            computedValue, initialValue));
        // If no other thread updated the running total, then 
        // totalValue and initialValue are equal when CompareExchange
        // compares them, and computedValue is stored in totalValue.
        // CompareExchange returns the value that was in totalValue
        // before the update, which is equal to initialValue, so the 
        // loop ends.

        // The function returns computedValue, not totalValue, because
        // totalValue could be changed by another thread between
        // the time the loop ends and the function returns.
        return computedValue;
    }
}

public class Test
{
    // Create an instance of the ThreadSafe class to test.
    private static ThreadSafe ts = new ThreadSafe();
    private static float control;

    private static Random r = new Random();
    private static ManualResetEvent mre = new ManualResetEvent(false);

    public static void Main()
    {
        // Create two threads, name them, and start them. The
        // thread will block on mre.
        Thread t1 = new Thread(TestThread);
        t1.Name = "Thread 1";
        t1.Start();
        Thread t2 = new Thread(TestThread);
        t2.Name = "Thread 2";
        t2.Start();

        // Now let the threads begin adding random numbers to 
        // the total.
        mre.Set();
        
        // Wait until all the threads are done.
        t1.Join();
        t2.Join();

        Console.WriteLine("Thread safe: {0}  Ordinary float: {1}", 
            ts.Total, control);
    }

    private static void TestThread()
    {
        // Wait until the signal.
        mre.WaitOne();

        for(int i = 1; i <= 1000000; i++)
        {
            // Add to the running total in the ThreadSafe instance, and
            // to an ordinary float.
            //
            float testValue = (float) r.NextDouble();
            control += testValue;
            ts.AddToTotal(testValue);
        }
    }
}

/* On a dual-processor computer, this code example produces output 
   similar to the following:

Thread safe: 17039.57  Ordinary float: 15706.44
 */
' This example demonstrates a thread-safe method that adds to a
' running total.  
Imports System.Threading

Public Class ThreadSafe
    ' Field totalValue contains a running total that can be updated
    ' by multiple threads. It must be protected from unsynchronized 
    ' access.
    Private totalValue As Single = 0.0

    ' The Total property returns the running total.
    Public ReadOnly Property Total As Single
        Get
            Return totalValue
        End Get
    End Property

    ' AddToTotal safely adds a value to the running total.
    Public Function AddToTotal(ByVal addend As Single) As Single
        Dim initialValue, computedValue As Single
        Do
            ' Save the current running total in a local variable.
            initialValue = totalValue

            ' Add the new value to the running total.
            computedValue = initialValue + addend

            ' CompareExchange compares totalValue to initialValue. If
            ' they are not equal, then another thread has updated the
            ' running total since this loop started. CompareExchange
            ' does not update totalValue. CompareExchange returns the
            ' contents of totalValue, which do not equal initialValue,
            ' so the loop executes again.
        Loop While initialValue <> Interlocked.CompareExchange( _
            totalValue, computedValue, initialValue)
        ' If no other thread updated the running total, then 
        ' totalValue and initialValue are equal when CompareExchange
        ' compares them, and computedValue is stored in totalValue.
        ' CompareExchange returns the value that was in totalValue
        ' before the update, which is equal to initialValue, so the 
        ' loop ends.

        ' The function returns computedValue, not totalValue, because
        ' totalValue could be changed by another thread between
        ' the time the loop ends and the function returns.
        Return computedValue
    End Function
End Class

Public Class Test
    ' Create an instance of the ThreadSafe class to test.
    Private Shared ts As New ThreadSafe()
    Private Shared control As Single

    Private Shared r As New Random()
    Private Shared mre As New ManualResetEvent(false)

    <MTAThread> _
    Public Shared Sub Main()
        ' Create two threads, name them, and start them. The
        ' threads will block on mre.
        Dim t1 As New Thread(AddressOf TestThread)
        t1.Name = "Thread 1"
        t1.Start()
        Dim t2 As New Thread(AddressOf TestThread)
        t2.Name = "Thread 2"
        t2.Start()

        ' Now let the threads begin adding random numbers to 
        ' the total.
        mre.Set()
        
        ' Wait until all the threads are done.
        t1.Join()
        t2.Join()

        Console.WriteLine("Thread safe: {0}  Ordinary Single: {1}", ts.Total, control)
    End Sub

    Private Shared Sub TestThread()
        ' Wait until the signal.
        mre.WaitOne()

        For i As Integer = 1 to 1000000
            ' Add to the running total in the ThreadSafe instance, and
            ' to an ordinary Single.
            '
            Dim testValue As Single = r.NextDouble()
            control += testValue
            ts.AddToTotal(testValue)
        Next
    End Sub
End Class

' On a dual-processor computer, this code example produces output 
' similar to the following:
'
'Thread safe: 17039.57  Ordinary Single: 15706.44

설명

하는 경우 comparand 에 값 location1 가 같으면 value 에 저장 된 location1합니다.If comparand and the value in location1 are equal, then value is stored in location1. 그렇지 않으면 작업이 수행되지 않습니다.Otherwise, no operation is performed. 비교 및 교환 작업을 원자성 작업으로 수행 됩니다.The compare and exchange operations are performed as an atomic operation. 반환 값 CompareExchange 에서 원래 값인 location1교환이 이루어지는 여부.The return value of CompareExchange is the original value in location1, whether or not the exchange takes place.

추가 정보

CompareExchange<T>(T, T, T) CompareExchange<T>(T, T, T) CompareExchange<T>(T, T, T) CompareExchange<T>(T, T, T)

지정된 참조 형식 T의 두 인스턴스에 대한 참조가 같은지 비교하고, 같으면 첫 번째 값을 바꿉니다.Compares two instances of the specified reference type T for reference equality and, if they are equal, replaces the first one.

public:
generic <typename T>
 where T : class static T CompareExchange(T % location1, T value, T comparand);
[System.Runtime.InteropServices.ComVisible(false)]
public static T CompareExchange<T> (ref T location1, T value, T comparand) where T : class;
static member CompareExchange : 'T * 'T * 'T -> 'T (requires 'T : null)
Public Shared Function CompareExchange(Of T As Class) (ByRef location1 As T, value As T, comparand As T) As T

형식 매개 변수

T

location1, valuecomparand에 사용될 형식입니다.The type to be used for location1, value, and comparand. 이 형식은 참조 형식이어야 합니다.This type must be a reference type.

매개 변수

location1
T T T T

참조에 의해 comparand와 비교되어 바뀔 수 있는 값을 가진 대상입니다.The destination, whose value is compared by reference with comparand and possibly replaced. 이것은 참조 매개 변수입니다. C#에서는 ref이고, Visual Basic에서는 ByRef입니다.This is a reference parameter (ref in C#, ByRef in Visual Basic).

value
T T T T

참조에 의한 비교 결과가 같은 경우 대상 값을 바꿀 값입니다.The value that replaces the destination value if the comparison by reference results in equality.

comparand
T T T T

참조에 의해 location1에서 값과 비교되는 값입니다.The value that is compared by reference to the value at location1.

반환

T T T T

location1의 원래 값입니다.The original value in location1.

예외

location1의 주소는 null 포인터입니다.The address of location1 is a null pointer.

설명

하는 경우 comparand 에 값 location1 같은지 참조 한 다음 value 에 저장 됩니다 location1합니다.If comparand and the value in location1 are equal by reference, then value is stored in location1. 그렇지 않으면 작업이 수행되지 않습니다.Otherwise, no operation is performed. 비교 및 교환 원자 단위 연산으로 수행 됩니다.The comparison and the exchange are performed as an atomic operation. 이 메서드의 반환 값은 원래 값에 location1교환이 이루어지는 여부.The return value of this method is the original value in location1, whether or not the exchange takes place.

이 메서드는 참조 형식만 지원 합니다.This method only supports reference types. 오버 로드가 있습니다 합니다 CompareExchange 값 형식에 대 한 메서드 Int32Int64, IntPtrSingle, 및 Double, 이지만 다른 값 형식에 대해 지원 되지 않습니다.There are overloads of the CompareExchange method for the value types Int32, Int64, IntPtr, Single, and Double, but there is no support for other value types.

참고

이 메서드 오버 로드에 적합 합니다 CompareExchange(Object, Object, Object) 후자 런타임에 바인딩된 액세스는 대상 개체를 필요로 하기 때문에 메서드를 오버 로드 합니다.This method overload is preferable to the CompareExchange(Object, Object, Object) method overload, because the latter requires the destination object to be accessed late-bound.

적용 대상