zoukankan      html  css  js  c++  java
  • volatile (C# Reference)

    The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.

    The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access.

    The volatile keyword can be applied to fields of these types:

    • Reference types.

    • Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile."

    • Types such as sbyte, byte, short, ushort, int, uint, char, float, and bool.

    • An enum type with one of the following base types: byte, sbyte, short, ushort, int, or uint.

    • Generic type parameters known to be reference types.

    • IntPtr and UIntPtr.

    The volatile keyword can only be applied to fields of a class or struct. Local variables cannot be declared volatile.



    The following example shows how to declare a public field variable as volatile.

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

    The following example demonstrates how an auxiliary or worker thread can be created and used to perform processing in parallel with that of the primary thread. For background information about multithreading, see Managed Threading and Threading (C# and 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.
    }
    
    做个快乐的自己。
  • 相关阅读:
    B题 hdu 1407 测试你是否和LTC水平一样高
    A题 hdu 1235 统计同成绩学生人数
    hdu 1869 六度分离(最短路floyd)
    hdu 2795 Billboard(线段树+单点更新)
    hdu 1754 I Hate It(线段树)
    hdu 1166敌兵布阵(线段树)
    hdu 1556(线段树之扫描线)
    Contest2073
    中南oj String and Arrays
    51nod 1459 迷宫游戏 (最短路径—Dijkstra算法)
  • 原文地址:https://www.cnblogs.com/Jessy/p/2144744.html
Copyright © 2011-2022 走看看