Interlocked.Exchange Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bir değişkeni, atomik işlem olarak belirtilen bir değere ayarlar.
Aşırı Yüklemeler
| Name | Description |
|---|---|
| Exchange(Single, Single) |
Tek duyarlıklı kayan nokta sayısını belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür. |
| Exchange(Object, Object) |
Bir nesneyi belirtilen bir değere ayarlar ve atomik işlem olarak özgün nesneye başvuru döndürür. |
| Exchange(IntPtr, IntPtr) |
Yerel boyutlu imzalı bir tamsayıyı belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür. |
| Exchange(Double, Double) |
Çift duyarlıklı kayan nokta sayısını belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür. |
| Exchange(Int32, Int32) |
32 bit imzalı bir tamsayıyı belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür. |
| Exchange(Int64, Int64) |
64 bit imzalı tamsayıyı belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür. |
| Exchange<T>(T, T) |
Belirtilen türde |
Exchange(Single, Single)
Tek duyarlıklı kayan nokta sayısını belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür.
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
Parametreler
- location1
- Single
Belirtilen değere ayarlanacağı değişken.
- value
- Single
location1 parametresinin ayarlandığı değer.
Döndürülenler
location1özgün değeri.
Özel durumlar
adresi location1 bir null işaretçidir.
Ayrıca bkz.
Şunlara uygulanır
Exchange(Object, Object)
Bir nesneyi belirtilen bir değere ayarlar ve atomik işlem olarak özgün nesneye başvuru döndürür.
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
Parametreler
- location1
- Object
Belirtilen değere ayarlanacağı değişken.
- value
- Object
location1 parametresinin ayarlandığı değer.
Döndürülenler
location1özgün değeri.
Özel durumlar
adresi location1 bir null işaretçidir.
adresi location1 bir null işaretçidir.
Açıklamalar
Important
Yöntem Exchange<T>(T, T) aşırı yüklemesi, somut başvuru türleri için kullanılabilecek genel bir alternatif sağlar.
Ayrıca bkz.
Şunlara uygulanır
Exchange(IntPtr, IntPtr)
Yerel boyutlu imzalı bir tamsayıyı belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür.
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
Parametreler
- location1
-
IntPtr
nativeint
Belirtilen değere ayarlanacağı değişken.
- value
-
IntPtr
nativeint
location1 parametresinin ayarlandığı değer.
Döndürülenler
nativeint
location1özgün değeri.
Özel durumlar
adresi location1 bir null işaretçidir.
Ayrıca bkz.
Şunlara uygulanır
Exchange(Double, Double)
Çift duyarlıklı kayan nokta sayısını belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür.
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
Parametreler
- location1
- Double
Belirtilen değere ayarlanacağı değişken.
- value
- Double
location1 parametresinin ayarlandığı değer.
Döndürülenler
location1özgün değeri.
Özel durumlar
adresi location1 bir null işaretçidir.
Ayrıca bkz.
Şunlara uygulanır
Exchange(Int32, Int32)
32 bit imzalı bir tamsayıyı belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür.
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
Parametreler
- location1
- Int32
Belirtilen değere ayarlanacağı değişken.
- value
- Int32
location1 parametresinin ayarlandığı değer.
Döndürülenler
location1özgün değeri.
Özel durumlar
adresi location1 bir null işaretçidir.
adresi location1 bir null işaretçidir.
Örnekler
Aşağıdaki kod örneğinde iş parçacığı güvenli bir kaynak kilitleme mekanizması gösterilmektedir.
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
Ayrıca bkz.
Şunlara uygulanır
Exchange(Int64, Int64)
64 bit imzalı tamsayıyı belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür.
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
Parametreler
- location1
- Int64
Belirtilen değere ayarlanacağı değişken.
- value
- Int64
location1 parametresinin ayarlandığı değer.
Döndürülenler
location1özgün değeri.
Özel durumlar
adresi location1 bir null işaretçidir.
Ayrıca bkz.
Şunlara uygulanır
Exchange<T>(T, T)
Belirtilen türde T bir değişkeni belirtilen bir değere ayarlar ve özgün değeri atomik işlem olarak döndürür.
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ür Parametreleri
- T
location1 ve valueiçin kullanılacak tür.
Parametreler
- location1
- T
Belirtilen değere ayarlanacağı değişken. Bu bir başvuru parametresidir (C# dilinde ref, Visual Basic'de ByRef).
- value
- T
location1 parametresinin ayarlandığı değer.
Döndürülenler
location1özgün değeri.
- Öznitelikler
Özel durumlar
adresi location1 bir null işaretçidir.
Desteklenmeyen T bir belirtildi. .NET 8 ve önceki sürümlerde, T bir başvuru türü olmalıdır. .NET 9 ve sonraki sürümlerde T bir başvuru, ilkel veya sabit listesi türünde olmalıdır.