8

I read about the volatile keyword, but I don't know in what situations I should use it.

Is it when the memory (variable) is getting updated and the process is not aware of that?

In what cases should drivers use 'volatile' variables?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pointer
  • 627
  • 2
  • 8
  • 19
  • See: http://stackoverflow.com/questions/2535148/volatile-qualifier-and-compiler-reorderings (Though the other question has both C++ and C tags remember that C++ got this keyword from C.) – dirkgently Sep 06 '10 at 06:18
  • From Dr Dobbs - http://www.drdobbs.com/184403766;jsessionid=4HI2GRADWCN51QE1GHRSKHWATMY32JVN – pankajt Sep 06 '10 at 06:23
  • 1
    See [ Why is volatile needed in c? ](http://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c). – Matthew Flaschen Sep 06 '10 at 06:23

6 Answers6

13

The most common case in my world is when you are programming microcontrollers that use memory-mapped I/O. The value in a register could change due to external digital inputs, but if you don't declare a variable as volatile, the compiler might optimize the code out completely and you'll be wondering why nothing works.

Matt suggested that I embellish on the statement regarding code getting "optimized out". Memory mapped I/O is accessed in code via pointers. When you want to check the state of a button, you will typically bitwise AND the value of the register with the bitmask for the button. If you don't specify volatile, the compiler will say, "hey, your code never actually changes the value of that pointer, so I'm going to just remove that statement where you've bitwise ANDed it, because the value is always the same!".

Hopefully this clears my statement up a bit. Thanks for the suggestion, Matt.

Dave
  • 14,618
  • 13
  • 91
  • 145
  • 1
    You should go into what you mean by "optimize it out", while it's familiar to some of us what this means, it might not be to everyone. – Matt Joiner Sep 06 '10 at 06:29
5

As you've marked this with the linux-device-driver tag, some specific advice for coding within the Linux kernel is possibly in order.

In general, you shouldn't need to write volatile in your Linux kernel code. In the cases where volatile might be required, its use is wrapped in core kernel functions that you should call instead. For example, if you're doing memory-mapped I/O, then you should be using ioremap(), writel(), readl() etc.

caf
  • 233,326
  • 40
  • 323
  • 462
4

Apart form what others have said, volatile keyword is generally to prevent the compiler form doing the optimization. In certain memory mapped registers where the value of the registers keep on changing (e.g. an RTC clock register) volatile key word is used. Take a look at this example :

RTC_CLOCK _time;
TIME _currentTime = _time ;
while(_currentTime - _time >= 100)
{

//Do something

}

//rest of the code 

If we do not append the volatile keyword before TIME this code will be like this as _currentTime - _time = 0 and the compiler will not consider the while loop below it.:

RTC_CLOCK _time;
TIME _currentTime = _time ;
//rest of the code

to prevent this we have to use the volatile keyword with the TIME.

sagivd
  • 120
  • 2
  • 11
Raulp
  • 7,758
  • 20
  • 93
  • 155
  • excellent concrete, easy to understand answer. and NOT full of false crap about how it helps concurrency. – v.oddou Sep 13 '13 at 03:07
0

This might be helpful to you

http://www.kcomputing.com/volatile.html

Rupeshit
  • 1,476
  • 1
  • 14
  • 23
-1

Volatile variables are variables that can be changed at any point, without the program knowing about it.

I can't think of any use for the volatile keyword in everyday programming, but it may spring up.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
  • he didnt ask for a definition, he asked for the use. – RPM1984 Sep 06 '10 at 06:29
  • Alexander is correct. He does not deserve a mark down. There examples above that illustrate this. One example in everyday use is if you use a pointer to the variable. The compiler can't tell that something has changed the variable via the pointer. – Eddy Feb 09 '17 at 21:23
-3

From my knowledge, in C the volatile keyword should be used where concurrent unsynchronized operations are performed on a variable from more than one source (process). If the variable is declared volatile, then all processes will always directly access the variable from its memory location, as opposed to copying the variable in the microprocessor's cache and accessing it from there.
Note that this will significantly decrease performance for that particular variable. The access time for in-memory variables is in the order of milliseconds, while for 1'st level or 2'nd level cache variables it is somewhere around tenths of nanoseconds, so use them only when all other options have been considered.

scripni
  • 2,144
  • 2
  • 19
  • 25
  • is it possible to make concurrent unsynchronized operations on a variable from more than one source (process) ? – Pointer Sep 06 '10 at 08:13
  • 1
    @Yogesh: yes. The simplest example is a variable that's written from a signal handler and read in the main loop of a program (for example, a "clean up and terminate asap" flag). Others include various forms of shared memory, such as a shared memory-mapped file. – R.. GitHub STOP HELPING ICE Sep 06 '10 at 12:26