<tfoot id='f7Ttb'></tfoot>

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

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

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

        基于作用域的锁守卫和返回值的时间安排

        时间:2023-10-17

        1. <small id='RkQ3q'></small><noframes id='RkQ3q'>

          <tfoot id='RkQ3q'></tfoot><legend id='RkQ3q'><style id='RkQ3q'><dir id='RkQ3q'><q id='RkQ3q'></q></dir></style></legend>

              <tbody id='RkQ3q'></tbody>
            • <bdo id='RkQ3q'></bdo><ul id='RkQ3q'></ul>
                <i id='RkQ3q'><tr id='RkQ3q'><dt id='RkQ3q'><q id='RkQ3q'><span id='RkQ3q'><b id='RkQ3q'><form id='RkQ3q'><ins id='RkQ3q'></ins><ul id='RkQ3q'></ul><sub id='RkQ3q'></sub></form><legend id='RkQ3q'></legend><bdo id='RkQ3q'><pre id='RkQ3q'><center id='RkQ3q'></center></pre></bdo></b><th id='RkQ3q'></th></span></q></dt></tr></i><div id='RkQ3q'><tfoot id='RkQ3q'></tfoot><dl id='RkQ3q'><fieldset id='RkQ3q'></fieldset></dl></div>
                  本文介绍了基于作用域的锁守卫和返回值的时间安排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  class C {
                      mutable std::mutex _lock;
                      map<string,string> deep_member;
                  public:
                      auto get_big_lump()
                       {
                       std::unique_lock<std::mutex> lock(_lock); // establish scope guard
                       return deep_member;  // copy the stuff while it can't be changed on another thread.
                       }
                  };
                  

                  关于守卫和复制返回值的保证时间是多少?复制是否会在持有锁时进行,或者在允许(或实际!)优化的情况下,是否可以在函数体返回后进行一些复制?

                  What is the guaranteed timing with respect to the guard and the copying of the return value? Will the copy take place while the lock is held, or can some of it be done after the function body returns, in the case of allowed (or actual!) optimizations?

                  推荐答案

                  局部对象的所有析构函数都在函数体终止后调用.return 语句是函数体的一部分,因此可以保证在执行复制时保持锁定.

                  All destructor of local objects are called after the function body terminates. Return statement is a part of a function body, so it is guaranteed the lock will be held while the copy is performed.

                  优化不会改变这一事实,它们只会改变副本的目的地——它可以是临时的中间目的地,也可以是呼叫站点上的真实目的地.锁只存在于第一个副本,无论它被发送到哪里.

                  Optimizations will not change this fact, they will only change the destination for the copy - it could either be an intermediate temporary or the real destination on the call site. The lock will only exist for the first copy, no matter where it is being sent to.

                  但是,请记住代码中的实际作用域锁定是不正确的.您需要 lock_guard - 但它可能只是一个演示复制粘贴错误,而真正的代码有真正的保护.

                  However, please keep in mind the the actual scope lock in the code is not correct. You need lock_guard - but it is possible it is simply a demo copy-paste error and real code has real guard in place.

                  这篇关于基于作用域的锁守卫和返回值的时间安排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 中的 decltype 和作用域运算符 下一篇:扩展在 if/else 序列中初始化的变量的范围

                  相关文章

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

                      • <bdo id='XyrNC'></bdo><ul id='XyrNC'></ul>
                      <tfoot id='XyrNC'></tfoot>
                    1. <small id='XyrNC'></small><noframes id='XyrNC'>