本文档已存档,并且将不进行维护。

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

volatile(C# 参考)的更多相关文章

  1. volatile的本质

    1. 编译器的优化 在本次线程内, 当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后,再取变量值时,就直接从寄存器中取值:当变量值在本线程里改变时,会同时把变量的新 ...

  2. 温故知新-多线程-深入刨析volatile关键词

    文章目录 摘要 volatile的作用 volatile如何解决线程可见? CPU Cache CPU Cache & 主内存 缓存一致性协议 volatile如何解决指令重排序? volat ...

  3. Java多线程 5 多线程其他知识简要介绍

    一.线程组 /** * A thread group represents a set of threads. In addition, a thread * group can also inclu ...

  4. Java中Unsafe类详解

    http://www.cnblogs.com/mickole/articles/3757278.html Java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操 ...

  5. 面试连环炮系列(十二):说说Atomiclnteger的使用场景

    说说Atomiclnteger的使用场景 AtomicInteger提供原子操作来进行Integer的使用,适合并发情况下的使用,比如两个线程对同一个整数累加. 为什么Atomiclnteger是线程 ...

  6. android 面试汇总<二>

    Animation Q:Android中有哪几种类型的动画? 技术点:动画类型 参考回答: 常见三类动画 View动画(View Animation)/补间动画(Tween animation):对V ...

  7. Java面试总结-基础篇1

    java多线程-- 自旋锁,偏向锁 好处:可以举Servlet和CGI的对比用户线程和守护线程的区别:用户线程结束后JVM会退出,然后守护线程才会终止(比如垃圾回收线程),如何在java中创建守护线程 ...

  8. Java并发编程:volatile关键字解析

    Java并发编程:volatile关键字解析 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 ...

  9. 多线程中的volatile和伪共享

      伪共享 false sharing,顾名思义,“伪共享”就是“其实不是共享”.那什么是“共享”?多CPU同时访问同一块内存区域就是“共享”,就会产生冲突,需要控制协议来协调访问.会引起“共享”的最 ...

随机推荐

  1. 微软Visual Studio "14" CTP 2 发布

    微软Visual Studio "14" CTP 2 发布 对于在微软阵营下进行工作的团队来说,拥有最新版本的Visual Studio是提高效率最佳的选择,没有之一. 在本文中, ...

  2. linux内核数据结构之链表

    linux内核数据结构之链表 1.前言 最近写代码需用到链表结构,正好公共库有关于链表的.第一眼看时,觉得有点新鲜,和我之前见到的链表结构不一样,只有前驱和后继指针,而没有数据域.后来看代码注释发现该 ...

  3. UVA 10392 (13.07.28)

    Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...

  4. C++指针数组和指向指针的指针

    指针数组 定义: 如果一个 数组,其元素均为指针型数据,该数组为指针数组,也就是说,指针数组中的每一个元素相当于一个指针变量,它的值都是地址. 形式: 一维指针数组的定义形式为: int[类型名] * ...

  5. Orchard网上商店模块

    从头开始编写一个Orchard网上商店模块(1) - 介绍 原文地址:http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-w ...

  6. 序列化和反序列化的几种方式(DataContractSerializer)

    序列化和反序列化的几种方式(DataContractSerializer) DataContractSerializer 类 使用提供的数据协定,将类型实例序列化和反序列化为 XML 流或文档. 无法 ...

  7. js漫谈

    不久前在武汉这边找到了自己的第一份软件方面工作,当时还很欢喜,可惜最后因为家里的事情,公司的情况等等,最近又成了待业状态,正好安静的考虑一下以后怎么走,是考研还是继续找工作,非计算机专业学生自学找软件 ...

  8. 依赖注入(IOC)

    依赖注入(IOC) 背景介绍 在设计模式中,尤其是结构型模式很多时候解决的就是对象间的依赖关系,变依赖具体为依赖抽象.平时开发中如果发现客户程序依赖某个或某类对象,我们常常会对他们进行一次抽象,形成抽 ...

  9. grunt 上手

    grunt 上手 开始上手 Grunt 和 grunt 插件都是通过 npm 安装, Node.js 包管理器管理的. Grunt 0.4.x 版本需要Node.js 版本号不低于0.8.0. 一.安 ...

  10. Arduino 各种模块篇 粉尘传感器 dust sensor 空气质量检测

    Testing a sensor from here. http://www.seeedstudio.com/wiki/Grove_-_Dust_Sensor It's a dust sensor. ...