1. <tfoot id='4IvJl'></tfoot>

    1. <legend id='4IvJl'><style id='4IvJl'><dir id='4IvJl'><q id='4IvJl'></q></dir></style></legend>
    2. <i id='4IvJl'><tr id='4IvJl'><dt id='4IvJl'><q id='4IvJl'><span id='4IvJl'><b id='4IvJl'><form id='4IvJl'><ins id='4IvJl'></ins><ul id='4IvJl'></ul><sub id='4IvJl'></sub></form><legend id='4IvJl'></legend><bdo id='4IvJl'><pre id='4IvJl'><center id='4IvJl'></center></pre></bdo></b><th id='4IvJl'></th></span></q></dt></tr></i><div id='4IvJl'><tfoot id='4IvJl'></tfoot><dl id='4IvJl'><fieldset id='4IvJl'></fieldset></dl></div>
    3. <small id='4IvJl'></small><noframes id='4IvJl'>

      • <bdo id='4IvJl'></bdo><ul id='4IvJl'></ul>

      是 boost shared_ptr &lt;XXX&gt;线程安全?

      Is boost shared_ptr lt;XXXgt; thread safe?(是 boost shared_ptr lt;XXXgt;线程安全?)

        <bdo id='3RSo9'></bdo><ul id='3RSo9'></ul>

              <tbody id='3RSo9'></tbody>

          1. <small id='3RSo9'></small><noframes id='3RSo9'>

          2. <i id='3RSo9'><tr id='3RSo9'><dt id='3RSo9'><q id='3RSo9'><span id='3RSo9'><b id='3RSo9'><form id='3RSo9'><ins id='3RSo9'></ins><ul id='3RSo9'></ul><sub id='3RSo9'></sub></form><legend id='3RSo9'></legend><bdo id='3RSo9'><pre id='3RSo9'><center id='3RSo9'></center></pre></bdo></b><th id='3RSo9'></th></span></q></dt></tr></i><div id='3RSo9'><tfoot id='3RSo9'></tfoot><dl id='3RSo9'><fieldset id='3RSo9'></fieldset></dl></div>
            <tfoot id='3RSo9'></tfoot>
                <legend id='3RSo9'><style id='3RSo9'><dir id='3RSo9'><q id='3RSo9'></q></dir></style></legend>
              • 本文介绍了是 boost shared_ptr &lt;XXX&gt;线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个关于 boost::shared_ptr 的问题.

                I have a question about boost::shared_ptr<T>.

                有很多线程.

                using namespace boost;
                
                class CResource
                {
                  // xxxxxx
                }
                
                class CResourceBase
                {
                public:
                   void SetResource(shared_ptr<CResource> res)
                   {
                     m_Res = res;
                   }
                
                   shared_ptr<CResource> GetResource()
                   {
                      return m_Res;
                   }
                private:
                   shared_ptr<CResource> m_Res;
                }
                
                CResourceBase base;
                
                //----------------------------------------------
                // Thread_A:
                    while (true)
                    {
                       //...
                       shared_ptr<CResource> nowResource = base.GetResource();
                       nowResource.doSomeThing();
                       //...
                    }
                
                // Thread_B:
                    shared_ptr<CResource> nowResource;
                    base.SetResource(nowResource);
                    //...
                

                第一季度

                如果Thread_A不关心nowResource是最新的,这部分代码会不会有问题?

                Q1

                If Thread_A do not care the nowResource is the newest, will this part of code have problem?

                我的意思是当Thread_B没有完全SetResource()时,Thread_A通过GetResource()得到一个错误的智能点代码>?

                I mean when Thread_B do not SetResource() completely, Thread_A get a wrong smart point by GetResource()?

                线程安全是什么意思?

                如果不关心资源是不是最新的,shared_ptr会不会是最新的?nowResourcenowResource 被释放时会导致程序崩溃还是会破坏 shared_ptr?

                If I do not care about whether the resource is newest, will the shared_ptr<CResource> nowResource crash the program when the nowResource is released or will the problem destroy the shared_ptr<CResource>?

                推荐答案

                来自 boost 文档:

                shared_ptr 对象提供相同的内置线程安全级别类型.shared_ptr 实例可以是读取"(仅使用 const 访问操作)同时由多个线程.不同 shared_ptr实例可以写入"(使用可变操作访问例如 operator= 或 reset)同时由多个线程(即使这些实例是副本,并共享相同的引用计数在下面.)

                shared_ptr objects offer the same level of thread safety as built-in types. A shared_ptr instance can be "read" (accessed using only const operations) simultaneously by multiple threads. Different shared_ptr instances can be "written to" (accessed using mutable operations such as operator= or reset) simultaneously by multiple threads (even when these instances are copies, and share the same reference count underneath.)

                任何其他同时访问都会导致未定义的行为.

                Any other simultaneous accesses result in undefined behavior.

                所以你的使用是不安全的,因为它使用了m_res的同时读写.boost 文档中的示例 3 也说明了这一点.

                So your usage is not safe, since it uses simultaneous read and write of m_res. Example 3 in the boost documentation also illustrates this.

                您应该使用单独的 mutex 保护对 SetResource/GetResource 中的 m_res 的访问.

                You should use a separate mutex that guards the access to m_res in SetResource/GetResource.

                这篇关于是 boost shared_ptr &lt;XXX&gt;线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                  <tfoot id='qSaDR'></tfoot>
                • <small id='qSaDR'></small><noframes id='qSaDR'>

                  • <legend id='qSaDR'><style id='qSaDR'><dir id='qSaDR'><q id='qSaDR'></q></dir></style></legend>

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