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.


Reference:
http://www.cognitus.net/html/tutorial/usingVolatile.html
http://www.megasolutions.net/cplus/What-is-the-proper-use-of-volatile_-21698.aspx
new values from memory.

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

the usage of keyword const

1. define the const  constant  for example:  const int a  2. define the const parameter of function...

Java多线程-2 基本线程间通信 && volatile keyword

这里是通过在主线程中终止一个用户线程的操作的例子。但在本内容之前需要首先了解Java内存模型。 1. java内存模型:指定了虚拟机和Java内存RAM是如何运作的这里是关于java内存模型的详细资料...

The volatile keyword in Java

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

[Keyword]static/const/volatile/intterupt的使用及注意

问题描述:对于static 和constant两个关键字,     static:声明为static的变量,在编译时刻开始永远存在,即编译后是会生成这个变量的符号的,其所在的位置,是在内存的中的....

How to Use C's volatile Keyword

BY Nigel Jones 原帖: http://www.barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword The prope...

SOPS: A System for Efficient Processing of Spatial-Keyword Publish/Subscribe(作者:Lisi Chen) 读后感

 最近读完chen 写的一篇文章关于publish/subscribe system中的空间文本内容匹配的文章。以下是对读完之后的一个总结。  随着大量的空间文本内容的通过移动设备产生...

Deep discussion on the usage of MACRO

see the code directly: /* macro.c by vinco at 2011-09-15 * i386-Red Hat-gcc-4.1.2 * http://b...

【DataStructure】Another usage of List: Polynomial

Statements: This blog was written by me, but most of content  is quoted from book【Data Structure wit...

Memory usage of Java objects: general guide

On this page, we take a general look at how to calculate the memory usage of a Java object, or at le...

【DataStructure】One of queue usage: Simulation System

Statements: This blog was written by me, but most of content  is quoted from book【Data Structure wit...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The Usage of the volatile Keyword
举报原因:
原因补充:

(最多只允许输入30个字)