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

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

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

  • 引用类型。

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

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

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

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

  • IntPtr 和 UIntPtr

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

示例

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

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

示例

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

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(); // 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#中的volatile关键字的更多相关文章

  1. zz剖析为什么在多核多线程程序中要慎用volatile关键字?

    [摘要]编译器保证volatile自己的读写有序,但由于optimization和多线程可以和非volatile读写interleave,也就是不原子,也就是没有用.C++11 supposed会支持 ...

  2. java中的volatile关键字

    java中的volatile关键字 一个变量被声明为volatile类型,表示这个变量可能随时被其他线程改变,所以不能把它cache到线程内存(如寄存器)中. 一般情况下volatile不能代替syn ...

  3. 单例模式中的volatile关键字

    在之前学习了单例模式在多线程下的设计,疑惑为何要加volatile关键字.加与不加有什么区别呢?这里我们就来研究一下.单例模式的设计可以参考个人总结的这篇文章   背景:在早期的JVM中,synchr ...

  4. Java中的volatile关键字的功能

    Java中的volatile关键字的功能 volatile是java中的一个类型修饰符.它是被设计用来修饰被不同线程访问和修改的变量.如果不加入volatile,基本上会导致这样的结果:要么无法编写多 ...

  5. 深入理解Java中的volatile关键字

    在再有人问你Java内存模型是什么,就把这篇文章发给他中我们曾经介绍过,Java语言为了解决并发编程中存在的原子性.可见性和有序性问题,提供了一系列和并发处理相关的关键字,比如synchronized ...

  6. java中的Volatile关键字使用

    文章目录 什么时候使用volatile Happens-Before java中的Volatile关键字使用 在本文中,我们会介绍java中的一个关键字volatile. volatile的中文意思是 ...

  7. C/C++中的volatile关键字

    volatile提醒编译器它后面所定义的变量随时都有可能改变,因此编译后的程序每次需要存储或读取这个变量的时候,都会直接从变量地址中读取数据. 如果没有volatile关键字,则编译器可能优化读取和存 ...

  8. 详解C中的volatile关键字【转】

    本文转载自:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html volatile提醒编译器它后面所定义的变量随时都有 ...

  9. Java中的volatile关键字为什么不是不具有原子性

    Java中long赋值不是原子操作,因为先写32位,再写后32位,分两步操作,而AtomicLong赋值是原子操作,为什么?为什么volatile能替代简单的锁,却不能保证原子性?这里面涉及volat ...

  10. C中的volatile关键字

    volatile提醒编译器它后面所定义的变量随时都有可能改变,因此编译后的程序每次需要存储或读取这个变量的时候,都会直接从变量地址中读取数据.如果没有volatile关键字,则编译器可能优化读取和存储 ...

随机推荐

  1. Activity 变成对话框,然后再隐藏?

    由于继续需要做一些无感操控的工作,之前也记录了下利用悬浮窗的方法.今天突然发现原来activity可以直接嗯嗯嗯啊. 首先我在AndroidManifest里给activity添加了如下一行: and ...

  2. echarts legend 的单选模式以及轮播技巧

    1.设置 legend 属性: selectedMode: 'single' 2.使用 myCharts.dispatchAction 来设置legend的聚焦 broadcast (v) { // ...

  3. 转 Kubernetes 入门 概念理解

    你闺女也能看懂的插画版Kubernetes指南 原创  2016-06-30 作者 周小璐 译 编者按:Matt Butcher是Deis的平台架构师,热爱哲学,咖啡和精雕细琢的代码.有一天女儿走进书 ...

  4. 使用OkHttpClient处理json请求处理的方式

    今天遇到一个问题,重构老系统时,前端传递的参数是一个json,controller层可以用@ResponseBody来接收. 因为新系统用的是spring cloud这一套,调用其他服务使用的是fei ...

  5. C# delegate 委托

    http://www.runoob.com/csharp/csharp-delegate.html

  6. ansible 批量推送公钥

    这里我们使用ansible的playbook 的功能来推送秘钥 使用方法参见:http://blog.csdn.net/magedu_linux/article/details/48529645 这里 ...

  7. 安装python 2.7条件下的pip环境

    wget https://pypi.python.org/packages/ff/d4/209f4939c49e31f5524fa0027bf1c8ec3107abaf7c61fdaad704a648 ...

  8. java、oracle对CLOB处理

    oracle CLOB字段转换位VARCHAR 1.实际上处理CLOB字段的时候,直接TO_CHAR,当长度超过4000的时候,会报错,提示列被截取: CLOB转varchar2:select to_ ...

  9. [na]数据链路层&网络层协议小结截图版

    ip层:分组选路 tcp:端到端的通信 中间系统没必要有应用程序,分组选路即可 应用程序中隐藏所有的物理细节. 语音肯定用udp linux主版本 次版本号 修订号 次版本为偶数说明是稳定版.奇数是开 ...

  10. tiny-cnn开源库的使用(MNIST)

    tiny-cnn是一个基于CNN的开源库,它的License是BSD 3-Clause.作者也一直在维护更新,对进一步掌握CNN非常有帮助,因此以下介绍下tiny-cnn在windows7 64bit ...