Interlocked.Exchange 메서드

정의

원자 단위 연산으로 변수를 지정된 값으로 설정합니다.

오버로드

Exchange(UInt16, UInt16)
Exchange(UInt32, UInt32)

원자성 연산으로 부호 없는 32비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(Single, Single)

원자 단위 연산으로 단정밀도 부동 소수점 숫자를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(UIntPtr, UIntPtr)

원자 단위 연산으로 플랫폼별 핸들 또는 포인터를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(UInt64, UInt64)

원자성 연산으로 부호 없는 64비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(SByte, SByte)
Exchange(Byte, Byte)
Exchange(IntPtr, IntPtr)

원자 단위 연산으로 플랫폼별 핸들 또는 포인터를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(Int64, Int64)

원자성 연산으로 부호 있는 64비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(Int32, Int32)

원자 단위 연산으로 부호 있는 32비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(Int16, Int16)
Exchange(Double, Double)

원자 단위 연산으로 배정밀도 부동 소수점 숫자를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(Object, Object)

원자 단위 연산으로 개체를 지정된 값으로 설정하고 참조를 원래 개체로 반환합니다.

Exchange<T>(T, T)

원자 단위 연산으로 지정된 형식 T의 변수를 지정된 값으로 설정하고 원래 값을 반환합니다.

Exchange(UInt16, UInt16)

중요

이 API는 CLS 규격이 아닙니다.

public:
 static System::UInt16 Exchange(System::UInt16 % location1, System::UInt16 value);
[System.CLSCompliant(false)]
public static ushort Exchange (ref ushort location1, ushort value);
[<System.CLSCompliant(false)>]
static member Exchange : uint16 * uint16 -> uint16
Public Shared Function Exchange (ByRef location1 As UShort, value As UShort) As UShort

매개 변수

location1
UInt16
value
UInt16

반환

특성

적용 대상

Exchange(UInt32, UInt32)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

중요

이 API는 CLS 규격이 아닙니다.

원자성 연산으로 부호 없는 32비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

public:
 static System::UInt32 Exchange(System::UInt32 % location1, System::UInt32 value);
[System.CLSCompliant(false)]
public static uint Exchange (ref uint location1, uint value);
[<System.CLSCompliant(false)>]
static member Exchange : uint32 * uint32 -> uint32
Public Shared Function Exchange (ByRef location1 As UInteger, value As UInteger) As UInteger

매개 변수

location1
UInt32

지정된 값으로 설정할 변수입니다.

value
UInt32

location1 매개 변수의 설정 값입니다.

반환

location1의 원래 값입니다.

특성

예외

location1의 주소는 null 포인터입니다.

적용 대상

Exchange(Single, Single)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

원자 단위 연산으로 단정밀도 부동 소수점 숫자를 지정된 값으로 설정하고 원래 값을 반환합니다.

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

매개 변수

location1
Single

지정된 값으로 설정할 변수입니다.

value
Single

location1 매개 변수의 설정 값입니다.

반환

location1의 원래 값입니다.

예외

location1의 주소는 null 포인터입니다.

추가 정보

적용 대상

Exchange(UIntPtr, UIntPtr)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

중요

이 API는 CLS 규격이 아닙니다.

원자 단위 연산으로 플랫폼별 핸들 또는 포인터를 지정된 값으로 설정하고 원래 값을 반환합니다.

public:
 static UIntPtr Exchange(UIntPtr % location1, UIntPtr value);
[System.CLSCompliant(false)]
public static UIntPtr Exchange (ref UIntPtr location1, UIntPtr value);
[<System.CLSCompliant(false)>]
static member Exchange : unativeint * unativeint -> unativeint
Public Shared Function Exchange (ByRef location1 As UIntPtr, value As UIntPtr) As UIntPtr

매개 변수

location1
UIntPtr

unativeint

지정된 값으로 설정할 변수입니다.

value
UIntPtr

unativeint

location1 매개 변수의 설정 값입니다.

반환

UIntPtr

unativeint

location1의 원래 값입니다.

특성

예외

location1의 주소는 null 포인터입니다.

적용 대상

Exchange(UInt64, UInt64)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

중요

이 API는 CLS 규격이 아닙니다.

원자성 연산으로 부호 없는 64비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

public:
 static System::UInt64 Exchange(System::UInt64 % location1, System::UInt64 value);
[System.CLSCompliant(false)]
public static ulong Exchange (ref ulong location1, ulong value);
[<System.CLSCompliant(false)>]
static member Exchange : uint64 * uint64 -> uint64
Public Shared Function Exchange (ByRef location1 As ULong, value As ULong) As ULong

매개 변수

location1
UInt64

지정된 값으로 설정할 변수입니다.

value
UInt64

location1 매개 변수의 설정 값입니다.

반환

location1의 원래 값입니다.

특성

예외

location1의 주소는 null 포인터입니다.

적용 대상

Exchange(SByte, SByte)

중요

이 API는 CLS 규격이 아닙니다.

public:
 static System::SByte Exchange(System::SByte % location1, System::SByte value);
[System.CLSCompliant(false)]
public static sbyte Exchange (ref sbyte location1, sbyte value);
[<System.CLSCompliant(false)>]
static member Exchange : sbyte * sbyte -> sbyte
Public Shared Function Exchange (ByRef location1 As SByte, value As SByte) As SByte

매개 변수

location1
SByte
value
SByte

반환

특성

적용 대상

Exchange(Byte, Byte)

public:
 static System::Byte Exchange(System::Byte % location1, System::Byte value);
public static byte Exchange (ref byte location1, byte value);
static member Exchange : byte * byte -> byte
Public Shared Function Exchange (ByRef location1 As Byte, value As Byte) As Byte

매개 변수

location1
Byte
value
Byte

반환

적용 대상

Exchange(IntPtr, IntPtr)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

원자 단위 연산으로 플랫폼별 핸들 또는 포인터를 지정된 값으로 설정하고 원래 값을 반환합니다.

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

매개 변수

location1
IntPtr

nativeint

지정된 값으로 설정할 변수입니다.

value
IntPtr

nativeint

location1 매개 변수의 설정 값입니다.

반환

IntPtr

nativeint

location1의 원래 값입니다.

예외

location1의 주소는 null 포인터입니다.

추가 정보

적용 대상

Exchange(Int64, Int64)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.CoreCLR.cs

원자성 연산으로 부호 있는 64비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

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

매개 변수

location1
Int64

지정된 값으로 설정할 변수입니다.

value
Int64

location1 매개 변수의 설정 값입니다.

반환

location1의 원래 값입니다.

예외

location1의 주소는 null 포인터입니다.

추가 정보

적용 대상

Exchange(Int32, Int32)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.CoreCLR.cs

원자 단위 연산으로 부호 있는 32비트 정수를 지정된 값으로 설정하고 원래 값을 반환합니다.

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

매개 변수

location1
Int32

지정된 값으로 설정할 변수입니다.

value
Int32

location1 매개 변수의 설정 값입니다.

반환

location1의 원래 값입니다.

예외

location1의 주소는 null 포인터입니다.

location1의 주소는 null 포인터입니다.

예제

다음 코드 예제에서는 스레드로부터 안전한 리소스 잠금 메커니즘을 보여줍니다.

using namespace System;
using namespace System::Threading;

const int numThreads = 10;
const int numThreadIterations = 5;
ref class MyInterlockedExchangeExampleClass
{
public:
   static void MyThreadProc()
   {
      for ( int i = 0; i < numThreadIterations; i++ )
      {
         UseResource();
         
         //Wait 1 second before next attempt.
         Thread::Sleep( 1000 );

      }
   }


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


   //0 for false, 1 for true.
   static int usingResource;
};

int main()
{
   Thread^ myThread;
   Random^ rnd = gcnew Random;
   for ( int i = 0; i < numThreads; i++ )
   {
      myThread = gcnew Thread( gcnew ThreadStart( MyInterlockedExchangeExampleClass::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 ) );
      myThread->Start();

   }
}
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));
                myThread.Start();
            }
        }

        private static void MyThreadProc()
        {
            for(int i = 0; i < numThreadIterations; i++)
            {
                UseResource();
            
                //Wait 1 second before next attempt.
                Thread.Sleep(1000);
            }
        }

        //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
                Thread.Sleep(500);

                Console.WriteLine("{0} exiting lock", Thread.CurrentThread.Name);
            
                //Release the lock
                Interlocked.Exchange(ref usingResource, 0);
                return true;
            }
            else
            {
                Console.WriteLine("   {0} was denied the lock", Thread.CurrentThread.Name);
                return false;
            }
        }
    }
}
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))
                myThread.Start()
            Next i
        End Sub

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

                'Wait 1 second before next attempt.
                Thread.Sleep(1000)
            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
                Thread.Sleep(500)

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

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

추가 정보

적용 대상

Exchange(Int16, Int16)

public:
 static short Exchange(short % location1, short value);
public static short Exchange (ref short location1, short value);
static member Exchange : int16 * int16 -> int16
Public Shared Function Exchange (ByRef location1 As Short, value As Short) As Short

매개 변수

location1
Int16
value
Int16

반환

적용 대상

Exchange(Double, Double)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.cs

원자 단위 연산으로 배정밀도 부동 소수점 숫자를 지정된 값으로 설정하고 원래 값을 반환합니다.

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

매개 변수

location1
Double

지정된 값으로 설정할 변수입니다.

value
Double

location1 매개 변수의 설정 값입니다.

반환

location1의 원래 값입니다.

예외

location1의 주소는 null 포인터입니다.

추가 정보

적용 대상

Exchange(Object, Object)

Source:
Interlocked.cs
Source:
Interlocked.cs
Source:
Interlocked.CoreCLR.cs

원자 단위 연산으로 개체를 지정된 값으로 설정하고 참조를 원래 개체로 반환합니다.

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

매개 변수

location1
Object

지정된 값으로 설정할 변수입니다.

value
Object

location1 매개 변수의 설정 값입니다.

반환

location1의 원래 값입니다.

예외

location1의 주소는 null 포인터입니다.

location1의 주소는 null 포인터입니다.

설명

중요

.NET Framework 2.0 부터 Exchange<T>(T, T) 메서드 오버로드에서는 참조 형식에 대해 형식이 안전한 대안을 제공합니다. 이 오버로드 대신 호출하는 것이 좋습니다.

추가 정보

적용 대상

Exchange<T>(T, T)

Source:
Interlocked.CoreCLR.cs
Source:
Interlocked.CoreCLR.cs
Source:
Interlocked.CoreCLR.cs

원자 단위 연산으로 지정된 형식 T의 변수를 지정된 값으로 설정하고 원래 값을 반환합니다.

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

형식 매개 변수

T

location1value에 사용될 형식입니다. 이 형식은 참조 형식이어야 합니다.

매개 변수

location1
T

지정된 값으로 설정할 변수입니다. 이것은 참조 매개 변수입니다. C#에서는 ref이고, Visual Basic에서는 ByRef입니다.

value
T

location1 매개 변수의 설정 값입니다.

반환

T

location1의 원래 값입니다.

특성

예외

location1의 주소는 null 포인터입니다.

설명

이 메서드는 참조 형식만 지원합니다. , , , Int64IntPtrSingleDouble 값 형식에 대한 Int32메서드의 Exchange 오버로드가 있지만 다른 값 형식은 지원되지 않습니다.

참고

이 메서드 오버로드는 메서드 오버로드보다 Exchange(Object, Object) 바람직합니다. 후자는 대상 개체 에 대한 늦은 바인딩된 액세스가 필요하기 때문입니다.

적용 대상