Interlocked.Exchange Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Define uma variável para um valor especificado como operação atómica.
Sobrecargas
| Name | Description |
|---|---|
| Exchange(UInt16, UInt16) |
Define um inteiro assinado de 16 bits para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(UInt32, UInt32) |
Define um inteiro sem sinal de 32 bits para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(Single, Single) |
Define um número de ponto flutuante de precisão simples para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(UIntPtr, UIntPtr) |
Define um número inteiro não assinado de tamanho nativo para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(UInt64, UInt64) |
Define um inteiro não assinado de 64 bits para um valor especificado e devolve o valor original como uma operação atómica. |
| Exchange(SByte, SByte) |
Define um inteiro assinado de 8 bits para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(Byte, Byte) |
Define um inteiro sem sinal de 8 bits para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(IntPtr, IntPtr) |
Define um inteiro com assinatura de tamanho nativo a um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(Int64, Int64) |
Define um inteiro assinado de 64 bits para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(Int32, Int32) |
Define um inteiro assinado de 32 bits para um valor especificado e devolve o valor original como uma operação atómica. |
| Exchange(Int16, Int16) |
Define um inteiro não assinado de 16 bits para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(Double, Double) |
Define um número de ponto flutuante de dupla precisão para um valor especificado e devolve o valor original, como uma operação atómica. |
| Exchange(Object, Object) |
Define um objeto para um valor especificado e devolve uma referência ao objeto original, como uma operação atómica. |
| Exchange<T>(T, T) |
Define uma variável do tipo |
Exchange(UInt16, UInt16)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Importante
Esta API não está em conformidade com CLS.
Define um inteiro assinado de 16 bits para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- UInt16
A variável a definir para o valor especificado.
- value
- UInt16
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
- Atributos
Exceções
O endereço de localização1 é um null apontador.
Aplica-se a
Exchange(UInt32, UInt32)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Importante
Esta API não está em conformidade com CLS.
Define um inteiro sem sinal de 32 bits para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- UInt32
A variável a definir para o valor especificado.
- value
- UInt32
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
- Atributos
Exceções
O endereço de location1 é um null apontador.
Aplica-se a
Exchange(Single, Single)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Define um número de ponto flutuante de precisão simples para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- Single
A variável a definir para o valor especificado.
- value
- Single
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
Exceções
O endereço de location1 é um null apontador.
Ver também
- de threading gerenciado
- Visão geral das primitivas de sincronização
Aplica-se a
Exchange(UIntPtr, UIntPtr)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Importante
Esta API não está em conformidade com CLS.
Define um número inteiro não assinado de tamanho nativo para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
-
UIntPtr
unativeint
A variável a definir para o valor especificado.
- value
-
UIntPtr
unativeint
O valor ao qual o location1 parâmetro está definido.
Devoluções
unativeint
O valor original de location1.
- Atributos
Exceções
O endereço de location1 é um null apontador.
Aplica-se a
Exchange(UInt64, UInt64)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Importante
Esta API não está em conformidade com CLS.
Define um inteiro não assinado de 64 bits para um valor especificado e devolve o valor original como uma operação atómica.
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
Parâmetros
- location1
- UInt64
A variável a definir para o valor especificado.
- value
- UInt64
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
- Atributos
Exceções
O endereço de location1 é um null apontador.
Aplica-se a
Exchange(SByte, SByte)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Importante
Esta API não está em conformidade com CLS.
Define um inteiro assinado de 8 bits para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- SByte
A variável a definir para o valor especificado.
- value
- SByte
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
- Atributos
Exceções
O endereço de localização1 é um null apontador.
Aplica-se a
Exchange(Byte, Byte)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Define um inteiro sem sinal de 8 bits para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- Byte
A variável a definir para o valor especificado.
- value
- Byte
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
Exceções
O endereço de localização1 é um null apontador.
Aplica-se a
Exchange(IntPtr, IntPtr)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Define um inteiro com assinatura de tamanho nativo a um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
-
IntPtr
nativeint
A variável a definir para o valor especificado.
- value
-
IntPtr
nativeint
O valor ao qual o location1 parâmetro está definido.
Devoluções
nativeint
O valor original de location1.
Exceções
O endereço de location1 é um null apontador.
Ver também
- de threading gerenciado
- Visão geral das primitivas de sincronização
Aplica-se a
Exchange(Int64, Int64)
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.CoreCLR.cs
Define um inteiro assinado de 64 bits para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- Int64
A variável a definir para o valor especificado.
- value
- Int64
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
Exceções
O endereço de location1 é um null apontador.
Ver também
- de threading gerenciado
- Visão geral das primitivas de sincronização
Aplica-se a
Exchange(Int32, Int32)
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.CoreCLR.cs
Define um inteiro assinado de 32 bits para um valor especificado e devolve o valor original como uma operação atómica.
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
Parâmetros
- location1
- Int32
A variável a definir para o valor especificado.
- value
- Int32
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
Exceções
O endereço de location1 é um null apontador.
O endereço de location1 é um null apontador.
Exemplos
O seguinte exemplo de código mostra um mecanismo de bloqueio de recursos seguro para threads.
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
Ver também
- de threading gerenciado
- Visão geral das primitivas de sincronização
Aplica-se a
Exchange(Int16, Int16)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Define um inteiro não assinado de 16 bits para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- Int16
A variável a definir para o valor especificado.
- value
- Int16
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
Exceções
O endereço de localização1 é um null apontador.
Aplica-se a
Exchange(Double, Double)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
Define um número de ponto flutuante de dupla precisão para um valor especificado e devolve o valor original, como uma operação atómica.
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
Parâmetros
- location1
- Double
A variável a definir para o valor especificado.
- value
- Double
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
Exceções
O endereço de location1 é um null apontador.
Ver também
- de threading gerenciado
- Visão geral das primitivas de sincronização
Aplica-se a
Exchange(Object, Object)
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.CoreCLR.cs
Define um objeto para um valor especificado e devolve uma referência ao objeto original, como uma operação atómica.
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
Parâmetros
- location1
- Object
A variável a definir para o valor especificado.
- value
- Object
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
Exceções
O endereço de location1 é um null apontador.
O endereço de location1 é um null apontador.
Observações
Importante
A Exchange<T>(T, T) sobrecarga de métodos fornece uma alternativa genérica que pode ser usada para tipos de referência concretos.
Ver também
- de threading gerenciado
- Visão geral das primitivas de sincronização
Aplica-se a
Exchange<T>(T, T)
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.cs
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.CoreCLR.cs
- Origem:
- Interlocked.cs
Define uma variável do tipo T especificado para um valor especificado e devolve o valor original, como uma operação atómica.
public:
generic <typename T>
where T : class static T Exchange(T % location1, T value);
public:
generic <typename T>
static T Exchange(T % location1, T value);
public static T Exchange<T>(ref T location1, T value) where T : class;
public static T Exchange<T>(ref 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)
static member Exchange : 'T * 'T -> 'T
[<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
Public Shared Function Exchange(Of T) (ByRef location1 As T, value As T) As T
Parâmetros de Tipo Genérico
- T
O tipo a ser usado para location1 e value.
Parâmetros
- location1
- T
A variável a definir para o valor especificado. Este é um parâmetro de referência (ref em C#, ByRef em Visual Basic).
- value
- T
O valor ao qual o location1 parâmetro está definido.
Devoluções
O valor original de location1.
- Atributos
Exceções
O endereço de location1 é um null apontador.
É especificado um não suportado T . No .NET 8 e versões anteriores, T deve ser um tipo de referência. Em versões 9 e posteriores .NET, T deve ser um tipo de referência, primitivo ou enum.