次の方法で共有


Interlocked.CompareExchange メソッド

2 つの値が等しいかどうかを比較します。等しい場合は、それらの値のいずれかを置き換えます。

オーバーロードの一覧

2 つの 32 ビット符号付き整数が等しいかどうかを比較します。等しい場合は、それらの値のいずれかを置き換えます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function CompareExchange(ByRef Integer, Integer, Integer) As Integer

[C#] public static int CompareExchange(ref int, int, int);

[C++] public: static int CompareExchange(int*, int*, int*);

[JScript] public static function CompareExchange(int, int, int) : int;

2 つのオブジェクトが等しいかどうかを比較します。等しい場合は、オブジェクトのいずれかを置き換えます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function CompareExchange(ByRef Object, Object, Object) As Object

[C#] public static object CompareExchange(ref object, object, object);

[C++] public: static Object* CompareExchange(Object**, Object**, Object**);

[JScript] public static function CompareExchange(Object, Object, Object) : Object;

2 つの単精度浮動小数点数が等しいかどうかを比較します。等しい場合は、それらの値のいずれかを置き換えます。

[Visual Basic] Overloads Public Shared Function CompareExchange(ByRef Single, Single, Single) As Single

[C#] public static float CompareExchange(ref float, float, float);

[C++] public: static float CompareExchange(float*, float*, float*);

[JScript] public static function CompareExchange(float, float, float) : float;

使用例

[Visual Basic, C#, C++] メモ   ここでは、CompareExchange のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
' 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

[C#] 
// 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;
    }
}

[C++] 
// 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 <mscorlib.dll>
#using <system.dll>

using namespace System::Threading;

public __gc class ThreadSafe 
{
    // totalValue contains a running total that can be updated
    // by multiple threads. It must be protected from unsynchronized 
    // access.
private:
    int totalValue;

    // The Total property returns the running total.
public:
    __property int get_Total()
    {
        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(&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;
    }
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Interlocked クラス | Interlocked メンバ | System.Threading 名前空間