<tfoot id='tyuqf'></tfoot>

    <legend id='tyuqf'><style id='tyuqf'><dir id='tyuqf'><q id='tyuqf'></q></dir></style></legend>

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

      • <bdo id='tyuqf'></bdo><ul id='tyuqf'></ul>

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

        在没有锁定的情况下读取同时被修改的整数变量是否安全?

        Is it safe to read an integer variable that#39;s being concurrently modified without locking?(在没有锁定的情况下读取同时被修改的整数变量是否安全?)

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

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

          <bdo id='MReOw'></bdo><ul id='MReOw'></ul>
        • <tfoot id='MReOw'></tfoot>

            • <legend id='MReOw'><style id='MReOw'><dir id='MReOw'><q id='MReOw'></q></dir></style></legend>
                <tbody id='MReOw'></tbody>

                  本文介绍了在没有锁定的情况下读取同时被修改的整数变量是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我在一个类中有一个整型变量,这个变量可能会被其他线程并发修改.写入受互斥锁保护.我也需要保护读取吗?我听说有一些硬件架构,如果一个线程修改一个变量,另一个线程读取它,那么读取结果将是垃圾;在这种情况下,我确实需要保护读取.不过,我从未见过这样的架构.

                  Suppose that I have an integer variable in a class, and this variable may be concurrently modified by other threads. Writes are protected by a mutex. Do I need to protect reads too? I've heard that there are some hardware architectures on which, if one thread modifies a variable, and another thread reads it, then the read result will be garbage; in this case I do need to protect reads. I've never seen such architectures though.

                  此问题假设单个事务仅包含更新单个整数变量,因此我不担心也可能涉及事务的任何其他变量的状态.

                  This question assumes that a single transaction only consists of updating a single integer variable so I'm not worried about the states of any other variables that might also be involved in a transaction.

                  推荐答案

                  原子读
                  如前所述,它依赖于平台.在 x86 上,该值必须在 4 字节边界上对齐.通常对于大多数平台,读取必须在单个 CPU 指令中执行.

                  atomic read
                  As said before, it's platform dependent. On x86, the value must be aligned on a 4 byte boundary. Generally for most platforms, the read must execute in a single CPU instruction.

                  优化器缓存
                  优化器不知道您正在读取由不同线程修改的值.声明值 volatile 有助于:优化器将为每次访问发出内存读/写,而不是尝试将值缓存在寄存器中.

                  optimizer caching
                  The optimizer doesn't know you are reading a value modified by a different thread. declaring the value volatile helps with that: the optimizer will issue a memory read / write for every access, instead of trying to keep the value cached in a register.

                  CPU 缓存
                  不过,您可能会读取一个陈旧的值,因为在现代架构中,您有多个内核和单独的缓存,这些缓存不会自动保持同步.您需要一个读内存屏障,通常是一个特定于平台的指令.

                  CPU cache
                  Still, you might read a stale value, since on modern architectures you have multiple cores with individual cache that is not kept in sync automatically. You need a read memory barrier, usually a platform-specific instruction.

                  在 Wintel 上,线程同步函数会自动添加一个完整的内存屏障,或者您可以使用 InterlockedXxxx 函数.

                  On Wintel, thread synchronization functions will automatically add a full memory barrier, or you can use the InterlockedXxxx functions.

                  MSDN:内存和同步问题,MemoryBarrier 宏

                  [edit] 另请参阅 drhirsch 的评论.

                  [edit] please also see drhirsch's comments.

                  这篇关于在没有锁定的情况下读取同时被修改的整数变量是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                    <bdo id='nkKIm'></bdo><ul id='nkKIm'></ul>
                    • <i id='nkKIm'><tr id='nkKIm'><dt id='nkKIm'><q id='nkKIm'><span id='nkKIm'><b id='nkKIm'><form id='nkKIm'><ins id='nkKIm'></ins><ul id='nkKIm'></ul><sub id='nkKIm'></sub></form><legend id='nkKIm'></legend><bdo id='nkKIm'><pre id='nkKIm'><center id='nkKIm'></center></pre></bdo></b><th id='nkKIm'></th></span></q></dt></tr></i><div id='nkKIm'><tfoot id='nkKIm'></tfoot><dl id='nkKIm'><fieldset id='nkKIm'></fieldset></dl></div>
                      1. <tfoot id='nkKIm'></tfoot>

                        <legend id='nkKIm'><style id='nkKIm'><dir id='nkKIm'><q id='nkKIm'></q></dir></style></legend>

                              <tbody id='nkKIm'></tbody>
                          • <small id='nkKIm'></small><noframes id='nkKIm'>