开发者

Volatile keyword [duplicate]

开发者 https://www.devze.com 2023-02-23 22:48 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What is the use of volatile keyword?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What is the use of volatile keyword?

Why to use volatile keyword in c++? what is 开发者_运维技巧the proper use of it?

As per definition

The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.

and in examples it decalred for e.g.

volatile bool Isrunning;
Isrunning=true;

and people use it for e.g.

if(Isrunning)
{
//some code here....
}

and at the end

Isrunning=false;

So my question is how it is diferent from bool Isrunning;

Thanks in advance. Dew


It's only valid usage is if you absolutely need to make sure that the value of a variable will never be held in a register, but written out and read from memory immediately. Also reordering the reads and writes is forbidden.

This is primarily necessary for memory mapped hardware I/O.

Note that volatile in C and C++ does not enforce atomic read/writes semantics for multiple threads. Your definition is wrong on that subject.

0

精彩评论

暂无评论...
验证码 换一张
取 消