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.
    }
    
    做个快乐的自己。
  • 相关阅读:
    各系统终端快速清屏方式
    mvc使用Chsword.Excel2Object导出和导入数据
    winfrom读写txt文件值(短信猫)
    WebDatagrid中添加打开新tab的超链接列
    WebDatagrid前台后台选中行
    WebDatagrid三种获取值得方法javascript
    WebDataMenu做工具栏程序代码
    WebDatagrid-checkbox行如何用js控制其是否可用
    WebDatagrid-左边的checkbox决定右边的文本是否进入编辑状态
    Cell Editing (WebDataGrid) 单元格编辑
  • 原文地址:https://www.cnblogs.com/Jessy/p/2144744.html
Copyright © 2011-2022 走看看