• <small id='Fc8uf'></small><noframes id='Fc8uf'>

  • <tfoot id='Fc8uf'></tfoot>
    <legend id='Fc8uf'><style id='Fc8uf'><dir id='Fc8uf'><q id='Fc8uf'></q></dir></style></legend>

    1. <i id='Fc8uf'><tr id='Fc8uf'><dt id='Fc8uf'><q id='Fc8uf'><span id='Fc8uf'><b id='Fc8uf'><form id='Fc8uf'><ins id='Fc8uf'></ins><ul id='Fc8uf'></ul><sub id='Fc8uf'></sub></form><legend id='Fc8uf'></legend><bdo id='Fc8uf'><pre id='Fc8uf'><center id='Fc8uf'></center></pre></bdo></b><th id='Fc8uf'></th></span></q></dt></tr></i><div id='Fc8uf'><tfoot id='Fc8uf'></tfoot><dl id='Fc8uf'><fieldset id='Fc8uf'></fieldset></dl></div>

          <bdo id='Fc8uf'></bdo><ul id='Fc8uf'></ul>
      1. 'volatile' 在 C++ 中阻止了哪些优化?

        时间:2023-09-16

        1. <tfoot id='F52SJ'></tfoot>
          • <bdo id='F52SJ'></bdo><ul id='F52SJ'></ul>
          • <legend id='F52SJ'><style id='F52SJ'><dir id='F52SJ'><q id='F52SJ'></q></dir></style></legend>
                <tbody id='F52SJ'></tbody>

                <small id='F52SJ'></small><noframes id='F52SJ'>

                • <i id='F52SJ'><tr id='F52SJ'><dt id='F52SJ'><q id='F52SJ'><span id='F52SJ'><b id='F52SJ'><form id='F52SJ'><ins id='F52SJ'></ins><ul id='F52SJ'></ul><sub id='F52SJ'></sub></form><legend id='F52SJ'></legend><bdo id='F52SJ'><pre id='F52SJ'><center id='F52SJ'></center></pre></bdo></b><th id='F52SJ'></th></span></q></dt></tr></i><div id='F52SJ'><tfoot id='F52SJ'></tfoot><dl id='F52SJ'><fieldset id='F52SJ'></fieldset></dl></div>
                  本文介绍了'volatile' 在 C++ 中阻止了哪些优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在查找关键字 volatile 及其用途,得到的答案几乎是:

                  I was looking up the keyword volatile and what it's for, and the answer I got was pretty much:

                  它用于防止编译器优化掉代码.

                  It's used to prevent the compiler from optimizing away code.

                  有一些例子,例如在轮询内存映射硬件时:如果没有 volatile,轮询循环将被删除,因为编译器可能会识别出条件值永远不会改变.但是由于只有一个或两个示例,这让我开始思考:是否还有其他情况需要使用 volatile 来避免不必要的优化?条件变量是唯一需要 volatile 的地方吗?

                  There were some examples, such as when polling memory-mapped hardware: without volatile the polling loop would be removed as the compiler might recognize that the condition value is never changed. But since there only were one example or maybe two, it got me thinking: Are there other situations where we need to use volatile in terms of avoiding unwanted optimization? Are condition variables the only place where volatile is needed?

                  我认为优化是特定于编译器的,因此未在 C++ 规范中指定.这是否意味着我们必须凭直觉行事,说 嗯,如果我不将该变量声明为 volatile 或在那里,我怀疑我的编译器会取消这个有什么明确的规则要遵守吗?

                  I imagine that optimization is compiler-specific and therefore is not specified in the C++ specification. Does that mean we have to go by gut feeling, saying Hm, I suspect my compiler will do away with this if I don't declare that variable as volatile or are there any clear rules to go by?

                  推荐答案

                  基本上,volatile 声明一个值可能会在你的程序背后改变.这可以防止编译器缓存该值(在 CPU 寄存器中)并在从程序的 POV 看来不需要时优化对该值的访问.

                  Basically, volatile announces that a value might change behind your program's back. That prevents compilers from caching the value (in a CPU register) and from optimizing away accesses to that value when they seem unnecessary from the POV of your program.

                  什么应该触发使用 volatile 是当一个值改变时,尽管你的程序没有写入它,并且当没有其他内存障碍(如用于多线程程序的互斥锁)) 存在.

                  What should trigger usage of volatile is when a value changes despite the fact that your program hasn't written to it, and when no other memory barriers (like mutexes as used for multi-threaded programs) are present.

                  这篇关于'volatile' 在 C++ 中阻止了哪些优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:什么是复制省略以及它如何优化复制和交换习语 下一篇:是否在清除后释放 std::vector 内存?

                  相关文章

                  最新文章

                  1. <legend id='tkkZF'><style id='tkkZF'><dir id='tkkZF'><q id='tkkZF'></q></dir></style></legend>

                    <small id='tkkZF'></small><noframes id='tkkZF'>

                      <bdo id='tkkZF'></bdo><ul id='tkkZF'></ul>

                      <tfoot id='tkkZF'></tfoot>
                      <i id='tkkZF'><tr id='tkkZF'><dt id='tkkZF'><q id='tkkZF'><span id='tkkZF'><b id='tkkZF'><form id='tkkZF'><ins id='tkkZF'></ins><ul id='tkkZF'></ul><sub id='tkkZF'></sub></form><legend id='tkkZF'></legend><bdo id='tkkZF'><pre id='tkkZF'><center id='tkkZF'></center></pre></bdo></b><th id='tkkZF'></th></span></q></dt></tr></i><div id='tkkZF'><tfoot id='tkkZF'></tfoot><dl id='tkkZF'><fieldset id='tkkZF'></fieldset></dl></div>