Interlocked.CompareExchange メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
2 つの値が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。
オーバーロード
CompareExchange(UInt64, UInt64, UInt64) |
2 つの 64 ビット符号なし整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。 |
CompareExchange(UInt32, UInt32, UInt32) |
2 つの 32 ビット符号なし整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。 |
CompareExchange(Single, Single, Single) |
2 つの単精度浮動小数点数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。 |
CompareExchange(Object, Object, Object) |
2 つのオブジェクトの参照が等値であるかどうかを比較します。等しい場合は、最初のオブジェクトを置き換えます。 |
CompareExchange(Int64, Int64, Int64) |
2 つの 64 ビット符号付き整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。 |
CompareExchange(Int32, Int32, Int32) |
2 つの 32 ビット符号付き整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。 |
CompareExchange(Double, Double, Double) |
2 つの倍精度浮動小数点数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。 |
CompareExchange(IntPtr, IntPtr, IntPtr) |
2 つのプラットフォーム固有のハンドルまたはポインターが等しいかどうかを比較します。等しい場合は、最初の 1 つを置き換えます。 |
CompareExchange<T>(T, T, T) |
指定した参照型 |
CompareExchange(UInt64, UInt64, UInt64)
重要
この API は CLS 準拠ではありません。
2 つの 64 ビット符号なし整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。
public:
static System::UInt64 CompareExchange(System::UInt64 % location1, System::UInt64 value, System::UInt64 comparand);
[System.CLSCompliant(false)]
public static ulong CompareExchange (ref ulong location1, ulong value, ulong comparand);
[<System.CLSCompliant(false)>]
static member CompareExchange : uint64 * uint64 * uint64 -> uint64
Public Shared Function CompareExchange (ByRef location1 As ULong, value As ULong, comparand As ULong) As ULong
パラメーター
- location1
- UInt64
値を comparand
と比較し、場合によっては置き換える比較先。
- value
- UInt64
比較した結果が等しい場合に比較先の値を置き換える値。
- comparand
- UInt64
location1
にある値と比較する値。
戻り値
location1
の元の値。
- 属性
例外
location1
のアドレスは null
ポインターです。
適用対象
CompareExchange(UInt32, UInt32, UInt32)
重要
この API は CLS 準拠ではありません。
2 つの 32 ビット符号なし整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。
public:
static System::UInt32 CompareExchange(System::UInt32 % location1, System::UInt32 value, System::UInt32 comparand);
[System.CLSCompliant(false)]
public static uint CompareExchange (ref uint location1, uint value, uint comparand);
[<System.CLSCompliant(false)>]
static member CompareExchange : uint32 * uint32 * uint32 -> uint32
Public Shared Function CompareExchange (ByRef location1 As UInteger, value As UInteger, comparand As UInteger) As UInteger
パラメーター
- location1
- UInt32
値を comparand
と比較し、場合によっては置き換える比較先。
- value
- UInt32
比較した結果が等しい場合に比較先の値を置き換える値。
- comparand
- UInt32
location1
にある値と比較する値。
戻り値
location1
の元の値。
- 属性
例外
location1
のアドレスは null
ポインターです。
適用対象
CompareExchange(Single, Single, Single)
2 つの単精度浮動小数点数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。
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
値を comparand
と比較し、場合によっては置き換える比較先。
- value
- Single
比較した結果が等しい場合に比較先の値を置き換える値。
- comparand
- Single
location1
にある値と比較する値。
戻り値
location1
の元の値。
例外
location1
のアドレスは null ポインターです。
例
次のコード例は、実行中の合計値を累積するスレッド セーフメソッドを Single 示しています。 2 つのスレッドは、スレッド セーフメソッドと通常の追加を使用して一連の Single 値を追加し、スレッドが完了すると合計が比較されます。 デュアル プロセッサ コンピューターでは、合計に大きな違いがあります。
スレッド セーフ メソッドでは、実行中の合計の初期値が保存され、新 CompareExchange しく計算された合計を古い合計と交換するためにメソッドが使用されます。 戻り値が実行中の合計の保存された値と等しくない場合、別のスレッドはその間に合計を更新しました。 その場合、実行中の合計を更新する試行を繰り返す必要があります。
// 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
注釈
値がlocation1
等しい場合comparand
は、 .location1``value
それ以外の場合は演算が実行されません。 比較操作と交換操作は、アトミック操作として実行されます。 の戻り値 CompareExchange は、交換が行われるかどうかに location1
関係なく、元の値です。
こちらもご覧ください
適用対象
CompareExchange(Object, Object, Object)
2 つのオブジェクトの参照が等値であるかどうかを比較します。等しい場合は、最初のオブジェクトを置き換えます。
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);
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
参照によって comparand
と比較し、場合によっては置き換える比較先のオブジェクト。
- value
- Object
参照の比較の結果が等しい場合に比較先のオブジェクトを置き換えるオブジェクト。
- comparand
- Object
location1
にあるオブジェクトと、参照によって比較されるオブジェクト。
戻り値
location1
の元の値。
例外
location1
のアドレスは null
ポインターです。
注釈
重要
.NET Framework 2.0 より、CompareExchange<T>(T, T, T) メソッド オーバーロードから参照型に対してタイプ セーフの代替が提供されます。 このオーバーロードの代わりに呼び出することをお勧めします。
オブジェクトがlocation1
参照で等しい場合comparand
はlocation1
、value
. それ以外の場合は演算が実行されません。 比較操作と交換操作は、アトミック操作として実行されます。 の戻り値 CompareExchange は、交換が行われるかどうかに location1
関係なく、元の値です。
注意
オブジェクトは、値の等値ではなく、参照の等価性で比較されます。 その結果、同じ値型の 2 つのボックス化されたインスタンス (たとえば、整数 3) は常に等しくないように見え、操作は実行されません。 値型では、このオーバーロードを使用しないでください。
こちらもご覧ください
適用対象
CompareExchange(Int64, Int64, Int64)
2 つの 64 ビット符号付き整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。
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
値を comparand
と比較し、場合によっては置き換える比較先。
- value
- Int64
比較した結果が等しい場合に比較先の値を置き換える値。
- comparand
- Int64
location1
にある値と比較する値。
戻り値
location1
の元の値。
例外
location1
のアドレスは null ポインターです。
注釈
値がlocation1
等しい場合comparand
は、 .location1``value
それ以外の場合は演算が実行されません。 比較操作と交換操作は、アトミック操作として実行されます。 の戻り値 CompareExchange は、交換が行われるかどうかに location1
関係なく、元の値です。
こちらもご覧ください
適用対象
CompareExchange(Int32, Int32, Int32)
2 つの 32 ビット符号付き整数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。
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
値を comparand
と比較し、場合によっては置き換える比較先。
- value
- Int32
比較した結果が等しい場合に比較先の値を置き換える値。
- comparand
- Int32
location1
にある値と比較する値。
戻り値
location1
の元の値。
例外
location1
のアドレスは null ポインターです。
例
次のコード例は、実行中の合計を累積するスレッド セーフなメソッドを示しています。 実行中の合計の初期値が保存され、メソッドを CompareExchange 使用して、新しく計算された合計を古い合計と交換します。 戻り値が実行中の合計の保存された値と等しくない場合、別のスレッドはその間に合計を更新しました。 その場合、実行中の合計を更新する試行を繰り返す必要があります。
注意
.NET Frameworkのバージョン 2.0 で導入されたこのメソッドはAdd、整数のスレッド セーフな実行合計を蓄積する便利な方法を提供します。
// 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
注釈
値がlocation1
等しい場合comparand
は、 .location1``value
それ以外の場合は演算が実行されません。 比較操作と交換操作は、アトミック操作として実行されます。 の戻り値 CompareExchange は、交換が行われるかどうかに location1
関係なく、元の値です。
こちらもご覧ください
適用対象
CompareExchange(Double, Double, Double)
2 つの倍精度浮動小数点数が等しいかどうかを比較します。等しい場合は、最初の値を置き換えます。
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
値を comparand
と比較し、場合によっては置き換える比較先。
- value
- Double
比較した結果が等しい場合に比較先の値を置き換える値。
- comparand
- Double
location1
にある値と比較する値。
戻り値
location1
の元の値。
例外
location1
のアドレスは null ポインターです。
例
次のコード例は、実行中の合計値を累積するスレッド セーフメソッドを Double 示しています。 2 つのスレッドは、スレッド セーフメソッドと通常の追加を使用して一連の Double 値を追加し、スレッドが完了すると合計が比較されます。 デュアル プロセッサ コンピューターでは、合計に大きな違いがあります。
スレッド セーフ メソッドでは、実行中の合計の初期値が保存され、新 CompareExchange しく計算された合計を古い合計と交換するためにメソッドが使用されます。 戻り値が実行中の合計の保存された値と等しくない場合、別のスレッドはその間に合計を更新しました。 その場合、実行中の合計を更新する試行を繰り返す必要があります。
// 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
注釈
値がlocation1
等しい場合comparand
は、 .location1``value
それ以外の場合は演算が実行されません。 比較操作と交換操作は、アトミック操作として実行されます。 の戻り値 CompareExchange は、交換が行われるかどうかに location1
関係なく、元の値です。
こちらもご覧ください
適用対象
CompareExchange(IntPtr, IntPtr, IntPtr)
2 つのプラットフォーム固有のハンドルまたはポインターが等しいかどうかを比較します。等しい場合は、最初の 1 つを置き換えます。
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
パラメーター
戻り値
-
IntPtr
nativeint
location1
の元の値。
例外
location1
のアドレスは null ポインターです。
注釈
値がlocation1
等しい場合comparand
は、 .location1``value
それ以外の場合は演算が実行されません。 比較操作と交換操作は、アトミック操作として実行されます。 このメソッドの戻り値は、交換が行われるかどうかに location1
関係なく、元の値です。
注意
IntPtr はプラットフォーム固有の型です。
こちらもご覧ください
適用対象
CompareExchange<T>(T, T, T)
指定した参照型 T
の 2 つのインスタンスの参照が等しいかどうかを比較します。等しい場合は、最初の 1 つを置き換えます。
public:
generic <typename T>
where T : class static T CompareExchange(T % location1, T value, T comparand);
public static T CompareExchange<T> (ref T location1, T value, T comparand) where T : class;
[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)
[<System.Runtime.InteropServices.ComVisible(false)>]
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
、value
、および comparand
に使用する型。 この型は、参照型である必要があります。
パラメーター
- location1
- T
参照によって値を comparand
と比較し、場合によっては置き換える比較先。 これは参照パラメーターです (C# では ref
、Visual Basic では ByRef
)。
- value
- T
参照によって比較した結果が等しい場合に比較先の値を置き換える値。
- comparand
- T
location1
にある値と参照によって比較される値。
戻り値
- T
location1
の元の値。
- 属性
例外
location1
のアドレスは null ポインターです。
注釈
値がlocation1
参照で等しい場合comparand
はlocation1
、value
. それ以外の場合は演算が実行されません。 比較と交換はアトミック操作として実行されます。 このメソッドの戻り値は、交換が行われるかどうかに location1
関係なく、元の値です。
このメソッドは参照型のみをサポートします。 値型 、、、SingleIntPtrおよび Double、およびメソッドのCompareExchangeオーバーロードがありますが、他の値型はサポートされません。 Int64Int32
注意
このメソッド のオーバーロードは、メソッド の CompareExchange(Object, Object, Object) オーバーロードに適しています。後者では、宛先オブジェクトに遅延バインドへのアクセスが必要になるためです。