我应该在哪里使用“易失性”?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在我的世界中,最常见的情况是当您对使用内存映射 I/O 的微控制器进行编程时。寄存器中的值可能会因外部数字输入而发生变化,但如果您不将变量声明为
易失性
,编译器可能会完全优化代码,您会想知道为什么没有任何效果。马特建议我对有关代码“优化”的声明进行修饰。内存映射 I/O 在代码中通过指针进行访问。当您想要检查按钮的状态时,通常会将寄存器的值与按钮的位掩码按位与。如果您不指定 volatile,编译器会说:“嘿,您的代码实际上从未更改该指针的值,因此我将删除您已对其进行按位与运算的语句,因为该值始终是相同!”。
希望这能让我的陈述更清楚一些。谢谢你的建议,马特。
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.
由于您已使用
linux-device-driver
标记对其进行了标记,因此可能需要一些在 Linux 内核中进行编码的具体建议。一般来说,您不需要在 Linux 内核代码中编写
易失性
。在可能需要易失性
的情况下,它的使用被包装在您应该调用的核心内核函数中。例如,如果您正在执行内存映射 I/O,那么您应该使用ioremap()
、writel()
、readl()等等。
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 wherevolatile
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 usingioremap()
,writel()
,readl()
etc.除了其他人所说的之外, volatile 关键字通常是为了防止编译器进行优化。
在某些寄存器值不断变化的存储器映射寄存器中(例如RTC时钟寄存器),使用易失性关键字。
看一下这个例子:
如果我们不在 TIME 之前附加 volatile 关键字,这段代码将像这样 _currentTime - _time = 0 并且编译器不会考虑它下面的 while 循环。:
为了防止这种情况,我们必须使用带有 TIME 的 volatile 关键字。
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 :
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.:
to prevent this we have to use the volatile keyword with the TIME.
这可能对您有帮助
http://www.kcomputing.com/volatile.html
This might be helpful to you
http://www.kcomputing.com/volatile.html
易失性变量是可以在程序不知道的情况下随时更改的变量。
我想不出 volatile 关键字在日常编程中有什么用处,但它可能会出现。
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.
据我所知,在 C 语言中,当对来自多个源(进程)的变量执行并发非同步操作时,应使用 volatile 关键字。如果变量被声明为
易失性
,那么所有进程将始终直接从其内存位置访问该变量,而不是在微处理器的缓存中复制该变量并从那里访问它。请注意,这将显着降低该特定变量的性能。内存中变量的访问时间约为毫秒,而对于一级或二级缓存变量,其访问时间约为十分之一纳秒,因此仅在考虑了所有其他选项后才使用它们。
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 declaredvolatile
, 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.