在C / C ++ / Java中使用volatile说明符 [英] Usage of volatile specifier in C/C++/Java

查看:87
本文介绍了在C / C ++ / Java中使用volatile说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在讨论多线程编程的许多资源时,通常会引用volatile说明符。
很明显,使用这个关键字不是在C / C ++和Java(1.4版本和更早版本)中至少在多线程之间实现同步的可靠方法。
这里是wikipedia列出的(不解释如何)典型的使用这个说明: -

While going through many resources on multithreaded programming, reference to volatile specifier usually comes up. It is clear that usage of this keyword is not a reliable way to achieve synchronization between multiple threads atleast in C/C++ and Java (versions 1.4 and earlier). Here is what wikipedia lists (without explaining how) as typical usages this specifier:-


  1. 允许访问内存映射设备

  2. 允许在setjmp和longjmp之间使用变量

  3. 允许在信号处理程序中使用变量

  4. / li>
  1. allow access to memory mapped devices
  2. allow uses of variables between setjmp and longjmp
  3. allow uses of variables in signal handlers
  4. busy waiting

我可以开始看到这个说明符在上面列出的用法中的作用,但是由于我还没有完全理解每个

I can begin to see the role of this specifier in the above listed usages but since I don't yet have a complete understanding of each of these areas, I can't figure out how exactly this specifier behaves in each of these usages.

有人可以解释一下吗?

推荐答案

因为你对这些用例感兴趣,我会解释第一个。注意,这适用于从ac / c + +的角度,不知道它如何扮演到java,虽然我怀疑在c / c + +和java通常volatile用于完全不同的情况。

Since you're interested in those usage cases, I'll explain the first one. Note that this applies from a c/c++ perspective, not sure how it plays into java, although I suspect in general volatile in c/c++ and java are used for completely different cases.

存储器映射设备是处理器以与存储器相同的方式而不是通过特殊总线进行通信的外设。

Memory mapped devices are peripherals which the processor communicates with in the same manner as the memory rather than through a special bus.

假设您有一个定时器即存储器映射。你通过写1到它的内存地址&其内部定时器倒计时5秒,关闭灯并将存储器位置重置为0.现在您正在开发一个c程序,需要在某些事件后打开该灯,有时在计数器到期之前将其关闭。如果使用常规变量(倾向于作为这种类型的应用程序的指针或引用)写入其内存位置,有许多事情可能会由于编译器优化而出错。

Suppose you have a little light with a timer that is memory mapped. You turn on the light by writing 1 to its memory address & its internal timer counts down for 5 seconds & turns the light off and resets the memory location to 0. Now you are developing a c program that needs to turn that light on after certain events, and sometimes turn it off before the counter expires. If you use a regular variable (tends to be a pointer or a reference for this type of application) to write to its memory location, there are a number of things that might go wrong due to compiler optimizations.

如果你不使用那么多的变量,你打开它,然后不久,关闭它,没有任何其他变量使用该值 - 将完全摆脱第一分配,或者在其它情况下,它将简单地维持处理器寄存器和寄存器中的值。从不写入内存。在这两种情况下,光woudl从来没有打开,因为它的记忆从未改变。

If you aren't working with that many variables and you are turning the light on and shortly there after turning it off without any other variables using that value - sometimes the compiler will altogether get rid of the first assignment, or in other cases it will simply maintain the value in the processor registers & never write to memory. In both these cases, the light woudl never turn on since it's memory was never changed.

现在考虑另一种情况,你检查的状态灯&它开着。这里,从设备的存储器和存储器中提取该值。保存在处理器寄存器中。现在,几秒钟后,灯自己关闭。不久之后,您尝试再次打开灯,然而,由于您读取的内存地址&没有改变它,因为,编译器假设值仍然是一个&因此从不改变它,虽然它实际上是0现在。

Now think of another situation where you check the state of the light & it is on. Here, the value is extracted from the device's memory & kept in a processor register. Now, after a few seconds, the light turns off by itself. Shortly thereafter you try to turn the light on again, however since you read that memory address & haven't changed it since, the compiler assumes the value is still one & therefore never changes it, although it is actually 0 now.

通过使用volatile关键字,防止编译器在将代码转换为机器时做出任何假设代码&确保所有这些特定操作严格按照程序员所写的来执行。这对于存储器映射设备是必要的,这主要是因为存储器位置不被处理器严格改变。出于这些原因,具有共享内存的多处理器系统在共同的存储器空间上操作时通常需要类似的做法。

By using the volatile key word, you prevent the compiler from making any of those assumptions when converting your code into machine code & ensures all those specific operations are performed strictly as written by the programmer. This is essential for memory mapped devices mostly because the memory location is not changed strictly by the processor. For these same reasons, multiprocessor systems with shared memory often require similar practices when operating on a common memory space.

这篇关于在C / C ++ / Java中使用volatile说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆