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

    volatile(C# 参考)

     

    若要了解有关 Visual Studio 2017 RC 的最新文档,请参阅 Visual Studio 2017 RC 文档

    volatile 关键字指示一个字段可以由多个同时执行的线程修改。 声明为 volatile 的字段不受编译器优化(假定由单个线程访问)的限制。 这样可以确保该字段在任何时间呈现的都是最新的值。

    volatile 修饰符通常用于由多个线程访问但不使用 lock 语句对访问进行序列化的字段。

    volatile 关键字可应用于以下类型的字段:

    • 引用类型。

    • 指针类型(在不安全的上下文中)。 请注意,虽然指针本身可以是可变的,但是它指向的对象不能是可变的。 换句话说,您无法声明“指向可变对象的指针”。

    • 类型,如 sbyte、byte、short、ushort、int、uint、char、float 和 bool。

    • 具有以下基类型之一的枚举类型:byte、sbyte、short、ushort、int 或 uint。

    • 已知为引用类型的泛型类型参数。

    • IntPtrUIntPtr

    可变关键字仅可应用于类或结构字段。 不能将局部变量声明为 volatile

    示例
     

     

    下面的示例说明如何将公共字段变量声明为 volatile

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

     

    下面的示例演示如何创建辅助线程,并用它与主线程并行执行处理。 有关多线程处理的背景信息,请参见Threading线程

    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.
    }

     
  • 相关阅读:
    Linux ->> VirtualBox Linux虚拟机与Windows主机共享目录
    Linux ->> CentOS 7 执行service network restart 报错
    借助企业微信实现“调接口”给个人微信发消息
    idea提交代码到github教程
    Content type ‘multipart/form-data;boundary=--------------------------9107
    org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;bounda
    org.hibernate.LazyInitializationException: could not initialize proxy 解决方案(JPA)
    GitLab代码回滚到特定版本
    js 导入excel文件
    GoLand 2021.1.3安装之后需要激活得步骤
  • 原文地址:https://www.cnblogs.com/Herzog3/p/6203251.html
Copyright © 2011-2022 走看看