<legend id='Rqn4J'><style id='Rqn4J'><dir id='Rqn4J'><q id='Rqn4J'></q></dir></style></legend>
    1. <tfoot id='Rqn4J'></tfoot>
    2. <small id='Rqn4J'></small><noframes id='Rqn4J'>

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

        C++ 静态成员变量初始化线程安全吗?

        时间:2023-12-02
          <tbody id='7VG6X'></tbody>

          <tfoot id='7VG6X'></tfoot>

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

                  <small id='7VG6X'></small><noframes id='7VG6X'>

                  本文介绍了C++ 静态成员变量初始化线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  根据以下资源,在 C++(特别是 Visual C++)范围内的静态变量初始化不是线程安全的.但是,全局静态变量是安全的.

                  According to following resources, in C++(Specially Visual C++) scoped static variable initialization isn't thread safe. But, global static variables are safe.

                  没有互斥的线程安全静态变量?

                  http://blogs.msdn.com/oldnewthing/存档/2004/03/08/85901.aspx

                  那么,以下带有静态成员变量的代码是线程安全的吗?

                  So, is following code with static member variable thread-safe?

                  class TestClass
                  {
                  public:
                     static MyClass m_instance;
                  }
                  
                  Myclass TestClass::m_instance;
                  

                  提前致谢!

                  推荐答案

                  更多是函数作用域静态变量与其他所有类型静态变量的问题,而不是作用域与全局变量的问题.

                  It's more a question of function-scoped static variables vs. every other kind of static variable, rather than scoped vs. globals.

                  所有非函数作用域的静态变量都是在 main() 之前构造的,而只有一个活动线程.函数作用域的静态变量在它们的包含函数第一次被调用时被构造.当函数在多个线程上被调用时,该标准对如何构造函数级静态的问题保持沉默.但是,我使用过的每个实现都在构造函数周围使用了一个锁(带有两次检查的标志)以保证线程安全.

                  All non-function-scope static variables are constructed before main(), while there is only one active thread. Function-scope static variables are constructed the first time their containing function is called. The standard is silent on the question of how function-level statics are constructed when the function is called on multiple threads. However, every implementation I've worked with uses a lock around the constructor (with a twice-checked flag) to guarantee thread-safety.

                  这篇关于C++ 静态成员变量初始化线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++中只有静态方法的类的优点 下一篇:将静态 libcurl 添加到 Code::Blocks IDE

                  相关文章

                • <legend id='6S495'><style id='6S495'><dir id='6S495'><q id='6S495'></q></dir></style></legend>

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

                  <small id='6S495'></small><noframes id='6S495'>

                  • <bdo id='6S495'></bdo><ul id='6S495'></ul>

                  1. <tfoot id='6S495'></tfoot>