`
gaofen100
  • 浏览: 1188689 次
文章分类
社区版块
存档分类
最新评论

volatile varible

 
阅读更多

From: http://en.wikipedia.org/wiki/Volatile_variable#In_C_and_C.2B.2B

In C and C++

In C, and consequently C++, the volatile keyword was intended to[ 1]

  • allow access to memory mapped devices
  • allow uses of variables between setjmp and longjmp
  • allow uses of sig_atomic_t variables in signal handlers

Operations on volatile variables are not atomic , nor do they establish a proper happens-before relationship for threading. This is according to the relevant standards (C, C++, POSIX, WIN32), and this is the matter of fact for the vast majority of current implementations. The volatile keyword is basically worthless as a portable threading construct.[ 2] [ 3] [ 4] [ 5] [ 6]

Example of MMIO In C

In this example, the code sets the value stored in foo to 0 . It then starts to poll that value repeatedly until it changes to 255 :

static
 int
 foo;


void bar( void ) {
foo = 0 ;

while ( foo != 255)
;
}

An optimizing compiler will notice that no other code can possibly change the value stored in foo , and will assume that it will remain equal to 0 at all times. The compiler will therefore replace the function body with an infinite loop similar to this:

void
 bar_optimized(
void
)
 {

foo = 0 ;

while ( true )
;
}

However, foo might represent a location that can be changed by other elements of the computer system at any time, such as a hardware register of a device connected to the CPU . The above code would never detect such a change; without the volatile keyword, the compiler assumes that the current program is the only part of the system that could change the value (which is by far the most common situation).

To prevent the compiler from optimizing code as above, the volatile keyword is used:

static
 volatile
 int
 foo;


void bar ( void ) {
foo = 0 ;

while ( foo != 255)
;
}

With this modification the loop condition will not be optimized away, and the system will detect the change when it occurs.

Optimization comparison in C

The following C programs, and accompanying disassemblies, demonstrate how the volatile keyword affects the compiler's output. The compiler in this case was GCC .

[hide ] Disassembly comparison
分享到:
评论

相关推荐

    PyTorch中的Variable变量详解

    一、了解Variable 顾名思义,Variable就是 变量 的意思。实质上也就是可以变化的量,区别于int变量,它是一种可以变化的变量,这正好就符合了反向传播,参数更新的属性。 具体来说,在pytorch中的Variable就是一个...

    关于PyTorch 自动求导机制详解

    每个变量都有两个标志:requires_grad和volatile。它们都允许从梯度计算中精细地排除子图,并可以提高效率。 requires_grad 如果有一个单一的输入操作需要梯度,它的输出也需要梯度。相反,只有所有输入都不需要梯度...

    来自UCI机器学习库的葡萄酒样品质量分析

    Variable Description QUALITY (integer in [1 - 10]) FIXED ACIDITY (numeric, g/dm^3) VOLATILE ACIDITY (numeric, g/dm^3) CITRIC ACID (numeric, g/dm^3) RESIDUAL SUGAR (numeric, g/dm^3) CHLORIDES (numeric,...

    基于51单片机的英文翻译(3000)

    In contrast to general-purpose CPUs, microcontrollers do not have an address bus or a data bus, because they integrate all the RAM and non-volatile memory on the same chip as the CPU. Because they ...

    Note_scalad.tar.gz

    Scala_Java_Override_Variable_Parameter Scala_Map_List_Array Security_Camera_Invade SpringBoot-RabbitMQ SpringBoot-Scala SpringBoot-Swagger SpringBoot SpringBoot_ApplicationContext SpringBoot_DevTools ...

    Linux C 一站式学习

    4.5. sig_atomic_t类型与volatile限定符 4.6. 竞态条件与sigsuspend函数 4.7. 关于SIGCHLD信号 34. 终端、作业控制与守护进程 1. 终端 1.1. 终端的基本概念 1.2. 终端登录过程 1.3. 网络登录过程 2. 作业控制 2.1. ...

    C# 语言规格说明(English Edition第五版)

    5.5 Atomicity of variable references 107 6. Conversions 109 6.1 Implicit conversions 109 6.1.1 Identity conversion 109 6.1.2 Implicit numeric conversions 110 6.1.3 Implicit enumeration conversions 110...

    java面试800题

    (1) super.variable //用来访问父类被隐藏的成员变量 (2) super.Method([paramlist]) //用来调用父类中被重载的方法 (3) super.([paramlist]) //调用父类中的构造函数 在类方法中(static),不能使用this或super...

    宋劲彬的嵌入式C语言一站式编程

    3.2. Condition Variable 3.3. Semaphore 3.4. 其它线程间同步机制 4. 编程练习 36. TCP/IP协议基础 1. TCP/IP协议栈与数据包封装 2. 以太网(RFC 894)帧格式 3. ARP数据报格式 4. IP数据报格式 5. IP地址与路由 6. ...

    整理后java开发全套达内学习笔记(含练习)

    volatile (关键字) 不稳定的['vɒlәtail] while (关键字) 循环语句。 当...的时候 [hwail] ORACLE_SID=oral10g\ --变局部变量 export ORACLE_SID --变全局变量 unset ORACLE_SID --卸载环境变量 ORACLE_HOME=...

    Java 语言基础 —— 非常符合中国人习惯的Java基础教程手册

    objectReference.variable 其中 objectReference 是对象的一个引用,它可以是一个已生成的对象,也可以是能够生成对 象引用的表达式。 例如:我们用 Point p=newPoint();生成了类 Point 的对象 p 后,可以用 p.x,p.y...

    C++ 标准 ISO 14882-2011

    Contents Contents iii List of Tables xi List of Figures xv 1 General 1 1.1 Scope . . . . ....1.2 Normative references ....1.3 Terms and definitions ....1.4 Implementation compliance ....1.5 Structure of this ...

    Google C++ International Standard.pdf

    Contents Contents ii List of Tables x List of Figures xiv 1 Scope 1 2 Normative references 2 3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . ....4.2 Structure of this ...

    java经典面试2010集锦100题(不看你后悔)

    JAVA试题(100道) —————————————————————————————————————— 题目1: 下面不属于基本类型的是:c (选择1项) A) boolean B) long C) String D) byte ...(1)public class Char...

Global site tag (gtag.js) - Google Analytics