Interlockedとは? わかりやすく解説

Interlocked クラス

複数のスレッドで共有される変数に分割不可能な操作を提供します。

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

このクラスのメソッドは、他のスレッドからアクセスできる変数をスレッドが更新しているとき、または 2 つのスレッドが別々のプロセッサで同時に実行されているときに、スケジューラがコンテキストの切り替えを行うと発生するエラーを防ぎます。このクラスのメンバは、例外をスローしません。

Increment メソッドと Decrement メソッドは、1 回の操作で変数をインクリメントまたはデクリメントして、結果値を格納します。変数のインクリメントは、ほとんどのコンピュータでは、分割不可能な操作ではなく、次のような手順が必要となります。

  1. インスタンス変数の値をレジスタに読み込みます。

  2. 値をインクリメントまたはデクリメントします。

  3. 値をインスタンス変数に格納します。

Increment と Decrement を使用しない場合は、最初の 2 つの手順を実行した後で、別のスレッドが優先処理される可能性があります。また、別のスレッドが 3 つのすべての手順を先行して完了させることもあります。最初のスレッドが実行を再開したときにインスタンス変数の値が上書きされるため、2 番目のスレッドが実行したインクリメントまたはデクリメントの結果は失われます。

Exchange メソッドは、指定した変数の値を分割不可能な方法で交換します。CompareExchange メソッドは、2 つの操作を組み合わせます。ここでは、2 つの値を比較し、その比較結果に基づいてその変数のいずれかに 3 つ目の値を格納します。比較操作および交換操作は、分割不可能な操作として実行されます。

使用例使用例

スレッド セーフなリソース ロック機構の例を次に示します。

Imports System
Imports System.Threading

Namespace InterlockedExchange_Example
    Class MyInterlockedExchangeExampleClass
        '0 for false, 1 for true.
        Private Shared usingResource As
 Integer = 0

        Private Shared currentMso As
 [Object]
        Private Shared globalMso As
 New [Object]()
        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 'Main

        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 'MyThreadProc

        '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 'UseResource
    End Class 'MyInterlockedExchangeExampleClass
 
End Namespace 'InterlockedExchange_Example
using System;
using System.Threading;

namespace InterlockedExchange_Example
{
    class MyInterlockedExchangeExampleClass
    {
        //0 for false, 1 for true.
        private static int
 usingResource = 0;

        private static Object currentMso;
        private static Object globalMso = new
 Object();
        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;
            }
        }

    }
}  
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;
   static Object^ globalMso = gcnew Object;
};

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();

   }
}

package InterlockedExchange_Example ; 

import System .* ;
import System.Threading .* ;
import System.Threading.Thread;

class MyInterlockedExchangeExampleClass
{
    //0 for false, 1 for true.
    private static int usingResource
 = 0;
    private static Object currentMso;
    private static Object globalMso = new
 Object();
    private static int numThreadIterations
 = 5;
    private static int numThreads
 = 10;

    public static void main(String[]
 args)
    {
        Thread myThread;
        Random rnd = new Random();

        for (int i = 0; i < numThreads;
 i++) {
            myThread = new Thread(new ThreadStart(MyThreadProc));
            myThread.set_Name(String.Format("Thread{0}",
                String.valueOf(i + 1)));

            //Wait a random amount of time before starting next thread.
            Thread.Sleep(rnd.Next(0, 1000));
            myThread.Start();
        }
    } //main

    private static void
 MyThreadProc()
    {
        for (int i = 0; i < numThreadIterations;
 i++) {
            UseResource();

            //Wait 1 second before next attempt.
            Thread.Sleep(1000);
        }
    } //MyThreadProc

    //A simple method that denies reentrancy.
    static boolean UseResource()
    {
        //0 indicates that the method is not in use.
        if (0 == Interlocked.Exchange(usingResource, 1)) {
            Console.WriteLine("{0} acquired the lock",
                Thread.get_CurrentThread().get_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.get_CurrentThread().get_Name());

            //Release the lock
            Interlocked.Exchange(usingResource, 0);
            return true;
        }
        else {
            Console.WriteLine("   {0} was denied the lock", 
                Thread.get_CurrentThread().get_Name());
            return false;
        }
    } //UseResource
} //MyInterlockedExchangeExampleClass
継承階層継承階層
System.Object
  System.Threading.Interlocked
スレッド セーフスレッド セーフ

この型は、スレッド セーフです。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Interlocked メソッド


パブリック メソッドパブリック メソッド

  名前 説明
パブリック メソッド Add オーバーロードされます。 分割不可能な操作として、2 つの整数を加算し、最初の整数を合計で置き換えます。
パブリック メソッド CompareExchange オーバーロードされます。 2 つの値が等しいかどうかを比較します。等しい場合は、それらの値のいずれかを置き換えます。
パブリック メソッド Decrement オーバーロードされます。 分割不可能な操作として、指定した変数をデクリメントし、結果を格納します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド Exchange オーバーロードされます。 分割不可能な操作として指定した値を変数として設定します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド Increment オーバーロードされます。 分割不可能な操作として、指定した変数をインクリメントし、結果を格納します。
パブリック メソッド Read 分割不可能な操作として 64 ビット値を読み込んで返します。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
参照参照

関連項目

Interlocked クラス
System.Threading 名前空間

その他の技術情報

マネージ スレッド処理
インタロックされた操作

Interlocked メンバ

複数のスレッドで共有される変数に分割不可能な操作を提供します。

Interlocked データ型で公開されるメンバを以下の表に示します。


パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド Add オーバーロードされます。 分割不可能な操作として、2 つの整数を加算し、最初の整数を合計で置き換えます。
パブリック メソッド CompareExchange オーバーロードされます。 2 つの値が等しいかどうかを比較します。等しい場合は、それらの値のいずれかを置き換えます。
パブリック メソッド Decrement オーバーロードされます。 分割不可能な操作として、指定した変数をデクリメントし、結果を格納します。
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド Exchange オーバーロードされます。 分割不可能な操作として指定した値を変数として設定します。
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド Increment オーバーロードされます。 分割不可能な操作として、指定した変数をインクリメントし、結果を格納します。
パブリック メソッド Read 分割不可能な操作として 64 ビット値を読み込んで返します。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
参照参照

関連項目

Interlocked クラス
System.Threading 名前空間

その他の技術情報

マネージ スレッド処理
インタロックされた操作



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Interlocked」の関連用語

Interlockedのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Interlockedのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS