The Usage of the volatile Keyword

原创 2012年03月25日 15:36:55

Following are my notes and understanding from the two referenced documents listed at the end of the article.

0. The volatile keyword tells compiler to turn off optimization for code accessing the modified variable/object, and always
write it to memory/read it from memory, rather than simply writing it to cpu register/reading it from cpu register.

1. A variable/object should be defined volatile if it can be modified by other instructions which are not executed by the current thread of execution,
and at the same time it can be read/written by current thread of execution. Since a thread of execution is run by a single cpu,
thus on multiple cpu systems, variables/objects shared by multiple threads of execution must be volatile, this including global variables
shared by multiple threads of a process, and structures/objects/variables placed in shared system memory/file mapping shared by multiple processes;
and even on a single cpu system, if a variable/object can be modified by a device (device driver) (i.e. not the cpu), it should be volatile.

2. volatile T *ptrT; where T is a structure. In this definition, whichever member ptrT dereferences, that member is written to/read
from memory rather than to/from CPU register. In other words, we tell the compiler that all members of T are volatile, thus always
access memory when accessing T's members through ptrT. type T* is not consistent with type "volatile T*".

And if we define all members in T with volatile, we get identical promise/behavior as above, and we don't have to use 'volatile'
outside when defining pointers/objects of (base) type T. Thus doing so can avoid spreading "volatile" everywhere in the code base, but
also means accesses to objectects of T will never be optimized for faster access.

If no volatile is used inside/outside T, data inconsistency can happen, imagine
two cpus executing two threads and they both are modifying the same object of type T, the final state of the object is unknown.

If T is a primitive type, the behavior is the same --- it takes a volatile keyword to guarantee the variable be accessed consistently if its shared by
multiple threads of execution.

3. The volatile modifier is quite like const modifier in terms of pointer definition. volatile T*ptrT or T volatile *ptrT means
the object(i.e. all members of the object)/variable ptrT points to is volatile; T *volatile ptrT means the pointer ptrT itself is volatile.

4. at thread/process context switch, i.e. thread/process A is about to be replaced by B, the registers that are used to cache variables
should be flushed to memory then cleared to maintain data consistency; and then during B's execution such registers are loaded with new values from memory.

new values from memory.


Jakob Jenkov多线程系列一一Java Volatile Keyword

Java Volatile Keyword Java中的volatitle关键字的作用是使一个Java变量"被放置在内存中",说得更确切一点是:每一个volatile变量只能从内存中被读到,而不是从C...
  • qq_22770457
  • qq_22770457
  • 2016年08月25日 23:10
  • 860

the usage of keyword const

1. define the const  constant  for example:  const int a  2. define the const parameter of function...
  • cherrygarden
  • cherrygarden
  • 2011年11月22日 21:46
  • 504

C语言再学习 -- 关键字volatile

  • qq_29350001
  • qq_29350001
  • 2017年01月04日 20:42
  • 3827

volatile keyword in C

  • tlaff
  • tlaff
  • 2014年02月27日 14:33
  • 754

The volatile keyword in Java

用在多线程,同步变量。 线程为了提高效率,将某成员变量(如A)拷贝了一份(如B),线程中对A的访问其实访问的是B。只在某些动作时才进行A和B的同步。因此存在A和B不一致的情况。volatile就是用来...
  • Meggie_love
  • Meggie_love
  • 2014年11月27日 15:47
  • 487

Java Volatile Keyword

转自: The Java volatile keyword is used ...
  • zhouhuakang
  • zhouhuakang
  • 2016年02月02日 19:22
  • 319

Introduction to the Volatile Keyword

Introduction to the Volatile Keyword The use of volatile is poorly understood by many programmers. T...
  • zhangao0086
  • zhangao0086
  • 2015年03月02日 11:49
  • 2842

Volatile C++ [keyword] [轉]

讲讲volatile的作用 volatile的本意是“易变的” 由于访问寄存器的速度要快过RAM,所以编译器一般都会作减少存取外部RAM的优化。比如: static int i=0; int main...
  • yunque100
  • yunque100
  • 2010年07月26日 14:39
  • 223

usage of volatile modifier

1. Managing Threads and Custom Services2. Java Volatile KeywordWhen is volatile Enough?As I have men...
  • u012319317
  • u012319317
  • 2016年02月18日 16:21
  • 293

[翻译]Introduction to the Volatile Keyword(认识关键字volatile)

出处: Introduction to the Volatile Keyword 认识关键字Volatile ...
  • wuhenyouyuyouyu
  • wuhenyouyuyouyu
  • 2016年06月08日 11:08
  • 434
您举报文章:The Usage of the volatile Keyword