0

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

Community
  • 1
  • 1
Dew
  • 2,993
  • 5
  • 17
  • 7
  • 1
    Please, before you post a question, spend 5 seconds looking over the preview, to see if it looks readable. You can format code simply by selecting the text, and pressing the `{ }` button. By not doing this, your question comes across as if you don't *care* about it, and don't want to make the least effort to make it easy for others to read (and answer). None of us are getting paid to answer your questions, so please, don't give us more work than necessary by posting badly formatted or hard-to-read questions. – jalf Apr 11 '11 at 10:58
  • @jalf i got 2 answer and 2 comments other than your comment for this question. Do you still think it is not readable? – Dew Apr 11 '11 at 11:12
  • no, I think it is **more** readable after I fixed it up. Do you deny this? Take a step back and *think*. *you* are the one who needs help. Either *you* spend 5 seconds more cleaning up your question before you ask it, or those you need help *from* spend 5 seconds more *reading* the question. Are you saying you prefer the latter? That you expect others to not just read your question, but do the extra work that you were too **lazy** to do? – jalf Apr 11 '11 at 11:17

1 Answers1

2

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.

Axel Gneiting
  • 5,293
  • 25
  • 30