volatile (C# リファレンス)

volatile キーワードは、同時に実行中の複数のスレッドによってフィールドが変更される可能性があることを示します。volatile と宣言されているフィールドは、シングル スレッドによるアクセスを前提とする、コンパイラの最適化の対象にはなりません。このため、フィールドには常に最新の値が含まれます。

volatile 修飾子は、通常、アクセスをシリアル化する lock ステートメントが使用されない場合に、複数のスレッドによりアクセスされるフィールドに対して使用します。

volatile キーワードは次の型のフィールドに使用できます。

  • 参照型

  • ポインター型 (unsafe コンテキスト内)ポインター自体は volatile にすることができますが、ポインターが指しているオブジェクトは volatile にすることができません。つまり、"volatile を指すポインター" は宣言できません。

  • sbyte、byte、short、ushort、int、uint、char、float、bool などの型

  • 基本型 byte、sbyte、short、ushort、int、または uint のいずれかを持つ列挙型

  • 参照型であることが判明しているジェネリック型パラメーター

  • IntPtr 型および UIntPtr

volatile キーワードは、クラスまたは構造体のフィールドにのみ適用できます。ローカル変数を volatile で宣言することはできません。

使用例

次の例では、public のフィールド変数を volatile として宣言する方法を示します。

    class VolatileTest
    {
        public volatile int i;

        public void Test(int _i)
        {
            i = _i;
        }
    }

次の例では、補助スレッドつまりワーカー スレッドを作成および使用して、プライマリ スレッドとの並行処理を実行する方法を示します。マルチスレッド処理の背景情報については、「マネージ スレッド処理」および「スレッド処理 (C# および Visual Basic)」を参照してください。

using System;
using System.Threading;

public class Worker
{
    // This method is called when the thread is started.
    public void DoWork()
    {
        while (!_shouldStop)
        {
            Console.WriteLine("Worker thread: working...");
        }
        Console.WriteLine("Worker thread: terminating gracefully.");
    }
    public void RequestStop()
    {
        _shouldStop = true;
    }
    // Keyword volatile is used as a hint to the compiler that this data
    // member is accessed by multiple threads.
    private volatile bool _shouldStop;
}

public class WorkerThreadExample
{
    static void Main()
    {
        // Create the worker thread object. This does not start the thread.
        Worker workerObject = new Worker();
        Thread workerThread = new Thread(workerObject.DoWork);

        // Start the worker thread.
        workerThread.Start();
        Console.WriteLine("Main thread: starting worker thread...");

        // Loop until the worker thread activates.
        while (!workerThread.IsAlive) ;

        // Put the main thread to sleep for 1 millisecond to
        // allow the worker thread to do some work.
        Thread.Sleep(1);

        // Request that the worker thread stop itself.
        workerObject.RequestStop();

        // Use the Thread.Join method to block the current thread 
        // until the object's thread terminates.
        workerThread.Join();
        Console.WriteLine("Main thread: worker thread has terminated.");
    }
    // Sample output:
    // Main thread: starting worker thread...
    // Worker thread: working...
    // Worker thread: working...
    // Worker thread: working...
    // Worker thread: working...
    // Worker thread: working...
    // Worker thread: working...
    // Worker thread: terminating gracefully.
    // Main thread: worker thread has terminated.
}

C# 言語仕様

詳細については、「C# 言語仕様」を参照してください。言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。

参照

関連項目

C# のキーワード

修飾子 (C# リファレンス)

概念

C# プログラミング ガイド

その他の技術情報

C# リファレンス