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
Without volatile
With volatile
#include <stdio.h>
int
main(
)
{
int
a =
10,
b =
100,
c =
0,
d =
0
;
printf
(
"%d"
,
a +
b)
;
a =
b;
c =
b;
d =
b;
printf
(
"%d"
,
c +
d)
;
return
0
;
}
|
#include <stdio.h>
int
main(
)
{
volatile
int
a =
10,
b =
100,
c =
0,
d =
0
;
printf
(
"%d"
,
a +
b)
;
a =
b;
c =
b;
d =
b;
printf
(
"%d"
,
c +
d)
;
return
0
;
}
|
gcc -O3 -S without.c -o without.s
gcc -S with.c -o with.s
.
file "without.c"
.
section .
rodata.
str1.
1,
"aMS"
,
@progbits,
1
.
LC0:
.
string "%d"
.
text .
p2align 4,,
15 .
globl main .
type
main,
@function
main:
leal 4(
%
esp
)
,
%
ecx
andl $-
16,
%
esp
pushl -
4(
%
ecx
)
pushl %
ebp
movl %
esp
,
%
ebp
pushl %
ecx
subl $20,
%
esp
movl $110,
4(
%
esp
)
movl $.
LC0,
(
%
esp
)
call
printf
movl $200,
4(
%
esp
)
movl $.
LC0,
(
%
esp
)
call
printf
addl $20,
%
esp
xorl %
eax
,
%
eax
popl %
ecx
popl %
ebp
leal -
4(
%
ecx
)
,
%
esp
ret
.
size
main,
.-
main .
ident "GCC: (GNU) 4.2.1 20070719 [FreeBSD]"
|
.
file "with.c"
.
section .
rodata.
str1.
1,
"aMS"
,
@progbits,
1
.
LC0:
.
string "%d"
.
text .
p2align 4,,
15 .
globl main .
type
main,
@function
main:
leal 4(
%
esp
)
,
%
ecx
andl $-
16,
%
esp
pushl -
4(
%
ecx
)
pushl %
ebp
movl %
esp
,
%
ebp
pushl %
ecx
subl $36,
%
esp
movl $10,
-
8(
%
ebp
)
movl $100,
-
12(
%
ebp
)
movl $0,
-
16(
%
ebp
)
movl $0,
-
20(
%
ebp
)
movl -
8(
%
ebp
)
,
%
edx
movl -
12(
%
ebp
)
,
%
eax
movl $.
LC0,
(
%
esp
)
addl %
edx
,
%
eax
movl %
eax
,
4(
%
esp
)
call
printf
movl -
12(
%
ebp
)
,
%
eax
movl %
eax
,
-
8(
%
ebp
)
movl -
12(
%
ebp
)
,
%
eax
movl %
eax
,
-
16(
%
ebp
)
movl -
12(
%
ebp
)
,
%
eax
movl %
eax
,
-
20(
%
ebp
)
movl -
16(
%
ebp
)
,
%
edx
movl -
20(
%
ebp
)
,
%
eax
movl $.
LC0,
(
%
esp
)
addl %
edx
,
%
eax
movl %
eax
,
4(
%
esp
)
call
printf
addl $36,
%
esp
xorl %
eax
,
%
eax
popl %
ecx
popl %
ebp
leal -
4(
%
ecx
)
,
%
esp
ret
.
size
main,
.-
main .
ident "GCC: (GNU) 4.2.1 20070719 [FreeBSD]"
|
|
分享到:
相关推荐
一、了解Variable 顾名思义,Variable就是 变量 的意思。实质上也就是可以变化的量,区别于int变量,它是一种可以变化的变量,这正好就符合了反向传播,参数更新的属性。 具体来说,在pytorch中的Variable就是一个...
每个变量都有两个标志:requires_grad和volatile。它们都允许从梯度计算中精细地排除子图,并可以提高效率。 requires_grad 如果有一个单一的输入操作需要梯度,它的输出也需要梯度。相反,只有所有输入都不需要梯度...
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,...
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 ...
Scala_Java_Override_Variable_Parameter Scala_Map_List_Array Security_Camera_Invade SpringBoot-RabbitMQ SpringBoot-Scala SpringBoot-Swagger SpringBoot SpringBoot_ApplicationContext SpringBoot_DevTools ...
4.5. sig_atomic_t类型与volatile限定符 4.6. 竞态条件与sigsuspend函数 4.7. 关于SIGCHLD信号 34. 终端、作业控制与守护进程 1. 终端 1.1. 终端的基本概念 1.2. 终端登录过程 1.3. 网络登录过程 2. 作业控制 2.1. ...
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...
(1) super.variable //用来访问父类被隐藏的成员变量 (2) super.Method([paramlist]) //用来调用父类中被重载的方法 (3) super.([paramlist]) //调用父类中的构造函数 在类方法中(static),不能使用this或super...
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. ...
volatile (关键字) 不稳定的['vɒlәtail] while (关键字) 循环语句。 当...的时候 [hwail] ORACLE_SID=oral10g\ --变局部变量 export ORACLE_SID --变全局变量 unset ORACLE_SID --卸载环境变量 ORACLE_HOME=...
objectReference.variable 其中 objectReference 是对象的一个引用,它可以是一个已生成的对象,也可以是能够生成对 象引用的表达式。 例如:我们用 Point p=newPoint();生成了类 Point 的对象 p 后,可以用 p.x,p.y...
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 ...
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试题(100道) —————————————————————————————————————— 题目1: 下面不属于基本类型的是:c (选择1项) A) boolean B) long C) String D) byte ...(1)public class Char...