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
    关于C#问号(?)的三个用处
    MVC上传图片示例
    在html中控制自动换行
    mvc中viewdata 和viewbag的区别
    eclipse 下找不到或无法加载主类的解决办法
    js实现checkbox全选与反选
    web组件新学--layer
    获取注册表最高权限
    pstools使用教程
  • 原文地址:https://www.cnblogs.com/Jessy/p/2144744.html
Copyright © 2011-2022 走看看