Where to use volatiles?

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

When the memory (variable) is updated and the process does not know about it?

When should drivers use mutable variables?

+7
c compiler-construction volatile operating-system linux-device-driver
source share
6 answers

The most common case in my world is when you program microcontrollers that use memory I / O. The value in the register may change due to external digital inputs, but if you do not declare the variable as volatile , the compiler can fully optimize the code, and you will be wondering why nothing works.

Matt suggested that I embellish the expression that the code is "optimized." Access to the I / O memory card is through code through pointers. If you want to check the state of a button, you are usually a bitwise AND register value with a bitmask for the button. If you do not specify volatile, the compiler will say: "Hey, your code never changes the value of this pointer, so I just delete this statement, where you are bitwise ANDed, because the value is always the same!".

I hope this makes my statement a little easier. Thanks for the suggestion, Matt.

+12
source share

As you tagged this with the linux-device-driver tag, some specific guidelines for coding in the Linux kernel are probably fine.

In general, you do not need to write volatile in your Linux kernel code. In cases where volatile may be required, its use ends in the core functions of the kernel, which you should call instead. For example, if you are doing memory I / O, you should use ioremap() , writel() , readl() , etc.

+3
source share

Besides the form others have said, the volatile keyword usually prevents compiler form optimizations. In certain memory-displayed registers where the value of the registers continues to change (for example, the RTC clock register), a variable keyword 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 add the volatile keyword before TIME, this code will look like this: _currentTime - _time = 0, and the compiler will not consider the while loop under it.

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

To prevent this, we should use the volatile keyword over time.

+2
source share
0
source share

Volatile variables are variables that can be changed at any time if the program does not know about it.

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

-one
source share

As far as I know, in C, you should use the volatile keyword, where parallel unsynchronized operations on a variable from several sources (process) are performed. If the variable is declared volatile , then all processes will always directly access the variable from its location in memory, and not copy the variable to the microprocessor cache and access it from there.

Note that this will significantly decrease performance for this particular variable. The access time for variables in memory is on the order of milliseconds, and for level 1 or level 2 of the cache, variables are somewhere around tenths of a nanosecond, so use them only when all other options are considered.

-2
source share

All Articles