我应该在哪里使用“易失性”?

发布于 2024-09-17 07:10:42 字数 208 浏览 10 评论 0原文

我读到了有关 易失性 关键字的信息,但我不知道在什么情况下我会使用应该使用它。

是在内存(变量)被更新而进程不知道的时候吗?

在什么情况下驱动程序应该使用“易失性”变量?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

混浊又暗下来 2024-09-24 07:10:42

在我的世界中,最常见的情况是当您对使用内存映射 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.

毁我热情 2024-09-24 07:10:42

由于您已使用 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 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.

过去的过去 2024-09-24 07:10:42

除了其他人所说的之外, volatile 关键字通常是为了防止编译器进行优化。
在某些寄存器值不断变化的存储器映射寄存器中(例如RTC时钟寄存器),使用易失性关键字。
看一下这个例子:

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

//Do something

}

//rest of the code 

如果我们不在 TIME 之前附加 volatile 关键字,这段代码将像这样 _currentTime - _time = 0 并且编译器不会考虑它下面的 while 循环。:

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

为了防止这种情况,我们必须使用带有 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 :

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.

审判长 2024-09-24 07:10:42

易失性变量是可以在程序不知道的情况下随时更改的变量。

我想不出 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.

姜生凉生 2024-09-24 07:10:42

据我所知,在 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文