开发者

Where to use volatile? [duplicate]

开发者 https://www.devze.com 2023-01-14 18:48 出处:网络
This question already has answers here: Why is volatile needed in C? 开发者_如何学JAVA(18 answers)
This question already has answers here: Why is volatile needed in C? 开发者_如何学JAVA (18 answers) Closed 8 years ago.

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

When the memory (variable) is getting updated and process is not aware of that?

In what cases should drivers use volatile variables?


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.


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.


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.


This might be helpful to you

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


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.


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.

0

精彩评论

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