Aracılığıyla paylaş


volatile (C# Başvurusu)

volatile Anahtar belirtir bir alan aynı anda çalışan birden çok iş parçacığı tarafından değiştirilebilir.Bildirilen alanları volatile değil, tek bir iş parçacığı tarafından erişim varsayalım derleyici iyileştirmeler tabi olan.Bu, en güncel değeri her zaman alanında mevcut olmasını sağlar.

volatile Değiştirici kullanmadan birden çok iş parçacığı tarafından erişilen bir alan için kullanılan genellikle Kilit erişim seri hale getirmek için deyimi.

volatile Anahtar sözcüğünü bu türdeki alanları uygulanabilir:

  • Başvuru tipleri.

  • İşaretçi türleri (güvenli olmayan bir içerik).Not işaretçi geçici olabilir, ancak işaret nesnesi olamaz.Başka bir deyişle, bir "işaretçi geçici." bildiremezsiniz.

  • Gibi sbyte, byte, short, ushort, int, uint, char, float ve bool türleri.

  • Aşağıdaki temel türlerinden biriyle enum türü: byte, sbyte, short, ushort, int veya uint.

  • Başvuru tipleri olduğu bilinen genel tür parametreleri.

  • IntPtr ve UIntPtr.

Geçici anahtar sözcüğü yalnızca bir sınıf veya yapı alanlarına uygulanabilir.Yerel değişken bildirilemez volatile.

Örnek

Aşağıdaki örnek nasıl bir ortak alanı değişken olarak bildirildiğini gösterir volatile.

class VolatileTest
    {
        public volatile int i;

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

Aşağıdaki örnek, nasıl yardımcı veya alt iş parçacığı oluşturulabilir ve birincil iş parçacığı ile paralel işlem gerçekleştirmek için kullanılan gösterir.Arka plan bilgileri için şunun hakkında bkz: Yönetilen İş Parçacığı Oluşturma ve İş Parçacığı Oluşturma (C# ve 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# dil belirtiminin

Daha fazla bilgi edinmek için, bkz. C# Dil Belirtimi. Dil belirtimi, C# sözdizimi ve kullanımı için kesin bir kaynaktır.

Ayrıca bkz.

Başvuru

C# Anahtar Sözcükleri

Değiştiriciler (C# Başvurusu)

Kavramlar

C# Programlama Kılavuzu

Diğer Kaynaklar

C# Başvurusu