Interlocked.Exchange Interlocked.Exchange Interlocked.Exchange Interlocked.Exchange Method

Definizione

Imposta una variabile su un valore specificato come operazione atomica.Sets a variable to a specified value as an atomic operation.

Overload

Exchange(Single, Single) Exchange(Single, Single) Exchange(Single, Single) Exchange(Single, Single)

Imposta un numero in virgola mobile a precisione singola su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a single-precision floating point number to a specified value and returns the original value, as an atomic operation.

Exchange(Object, Object) Exchange(Object, Object) Exchange(Object, Object) Exchange(Object, Object)

Imposta un oggetto su un valore specificato e restituisce un riferimento all'oggetto originale, come operazione atomica.Sets an object to a specified value and returns a reference to the original object, as an atomic operation.

Exchange(IntPtr, IntPtr) Exchange(IntPtr, IntPtr) Exchange(IntPtr, IntPtr) Exchange(IntPtr, IntPtr)

Imposta un puntatore o un handle specifico della piattaforma su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.

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

Imposta un numero in virgola mobile a precisione doppia su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.

Exchange(Int32, Int32) Exchange(Int32, Int32) Exchange(Int32, Int32) Exchange(Int32, Int32)

Imposta un intero con segno a 32 bit su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.

Exchange(Int64, Int64) Exchange(Int64, Int64) Exchange(Int64, Int64) Exchange(Int64, Int64)

Imposta un intero con segno a 64 bit su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation.

Exchange<T>(T, T) Exchange<T>(T, T) Exchange<T>(T, T) Exchange<T>(T, T)

Imposta una variabile del tipo T indicato sul valore specificato e restituisce il valore originale, come operazione atomica.Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation.

Exchange(Single, Single) Exchange(Single, Single) Exchange(Single, Single) Exchange(Single, Single)

Imposta un numero in virgola mobile a precisione singola su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a single-precision floating point number to a specified value and returns the original value, as an atomic operation.

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

Parametri

location1
Single Single Single Single

Variabile su cui impostare il valore specificato.The variable to set to the specified value.

value
Single Single Single Single

Valore su cui è impostato il parametro location1.The value to which the location1 parameter is set.

Restituisce

Valore originale di location1.The original value of location1.

Eccezioni

L'indirizzo di location1 è un puntatore Null.The address of location1 is a null pointer.

Vedi anche

Exchange(Object, Object) Exchange(Object, Object) Exchange(Object, Object) Exchange(Object, Object)

Imposta un oggetto su un valore specificato e restituisce un riferimento all'oggetto originale, come operazione atomica.Sets an object to a specified value and returns a reference to the original object, as an atomic operation.

public:
 static System::Object ^ Exchange(System::Object ^ % location1, System::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

Parametri

location1
Object Object Object Object

Variabile su cui impostare il valore specificato.The variable to set to the specified value.

value
Object Object Object Object

Valore su cui è impostato il parametro location1.The value to which the location1 parameter is set.

Restituisce

Valore originale di location1.The original value of location1.

Eccezioni

L'indirizzo di location1 è un puntatore Null.The address of location1 is a null pointer.

Commenti

Importante

A partire da .NET Framework 2.0, l'overload del metodo Exchange<T>(T, T) offre un'alternativa indipendente dai tipi per i tipo di riferimento.Beginning with .NET Framework 2.0, the Exchange<T>(T, T) method overload provides a type-safe alternative for reference types. È consigliabile chiamarlo invece di questo overload.We recommend that you call it instead of this overload.

Vedi anche

Exchange(IntPtr, IntPtr) Exchange(IntPtr, IntPtr) Exchange(IntPtr, IntPtr) Exchange(IntPtr, IntPtr)

Imposta un puntatore o un handle specifico della piattaforma su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a platform-specific handle or pointer to a specified value and returns the original value, as an atomic operation.

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

Parametri

location1
IntPtr IntPtr IntPtr IntPtr

Variabile su cui impostare il valore specificato.The variable to set to the specified value.

value
IntPtr IntPtr IntPtr IntPtr

Valore su cui è impostato il parametro location1.The value to which the location1 parameter is set.

Restituisce

Valore originale di location1.The original value of location1.

Eccezioni

L'indirizzo di location1 è un puntatore Null.The address of location1 is a null pointer.

Vedi anche

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

Imposta un numero in virgola mobile a precisione doppia su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a double-precision floating point number to a specified value and returns the original value, as an atomic operation.

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

Parametri

location1
Double Double Double Double

Variabile su cui impostare il valore specificato.The variable to set to the specified value.

value
Double Double Double Double

Valore su cui è impostato il parametro location1.The value to which the location1 parameter is set.

Restituisce

Valore originale di location1.The original value of location1.

Eccezioni

L'indirizzo di location1 è un puntatore Null.The address of location1 is a null pointer.

Vedi anche

Exchange(Int32, Int32) Exchange(Int32, Int32) Exchange(Int32, Int32) Exchange(Int32, Int32)

Imposta un intero con segno a 32 bit su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.

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

Parametri

location1
Int32 Int32 Int32 Int32

Variabile su cui impostare il valore specificato.The variable to set to the specified value.

value
Int32 Int32 Int32 Int32

Valore su cui è impostato il parametro location1.The value to which the location1 parameter is set.

Restituisce

Valore originale di location1.The original value of location1.

Eccezioni

L'indirizzo di location1 è un puntatore Null.The address of location1 is a null pointer.

Esempi

Nell'esempio di codice seguente viene illustrato un meccanismo di blocco delle risorse thread-safe.The following code example shows a thread-safe resource locking mechanism.

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 
Vedi anche

Exchange(Int64, Int64) Exchange(Int64, Int64) Exchange(Int64, Int64) Exchange(Int64, Int64)

Imposta un intero con segno a 64 bit su un valore specificato e restituisce il valore originale, come operazione atomica.Sets a 64-bit signed integer to a specified value and returns the original value, as an atomic operation.

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

Parametri

location1
Int64 Int64 Int64 Int64

Variabile su cui impostare il valore specificato.The variable to set to the specified value.

value
Int64 Int64 Int64 Int64

Valore su cui è impostato il parametro location1.The value to which the location1 parameter is set.

Restituisce

Valore originale di location1.The original value of location1.

Eccezioni

L'indirizzo di location1 è un puntatore Null.The address of location1 is a null pointer.

Vedi anche

Exchange<T>(T, T) Exchange<T>(T, T) Exchange<T>(T, T) Exchange<T>(T, T)

Imposta una variabile del tipo T indicato sul valore specificato e restituisce il valore originale, come operazione atomica.Sets a variable of the specified type T to a specified value and returns the original value, as an atomic operation.

public:
generic <typename T>
 where T : class static T Exchange(T % location1, T value);
[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)
Public Shared Function Exchange(Of T As Class) (ByRef location1 As T, value As T) As T

Parametri di tipo

T

Tipo da utilizzare per location1 e value.The type to be used for location1 and value. Questo tipo deve essere un tipo di riferimento.This type must be a reference type.

Parametri

location1
T T T T

Variabile su cui impostare il valore specificato.The variable to set to the specified value. Questo rappresenta un parametro di riferimento (ref in C#, ByRef in Visual Basic).This is a reference parameter (ref in C#, ByRef in Visual Basic).

value
T T T T

Valore su cui è impostato il parametro location1.The value to which the location1 parameter is set.

Restituisce

T T T T

Valore originale di location1.The original value of location1.

Eccezioni

L'indirizzo di location1 è un puntatore Null.The address of location1 is a null pointer.

Commenti

Questo metodo supporta solo i tipi di riferimento.This method only supports reference types. Sono disponibili Exchange overload del metodo per i Int32tipi di valore, Int64, IntPtr, Singlee Double , ma non è disponibile alcun supporto per altri tipi di valore.There are overloads of the Exchange method for the Int32, Int64, IntPtr, Single, and Double value types, but there is no support for other value types.

Nota

Questo overload del metodo è preferibile Exchange(Object, Object) all'overload del metodo, perché quest'ultimo richiede l'accesso ad associazione tardiva all'oggetto di destinazione.This method overload is preferable to the Exchange(Object, Object) method overload, because the latter requires late-bound access to the destination object .

Si applica a