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

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

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

      2. <legend id='K4mPU'><style id='K4mPU'><dir id='K4mPU'><q id='K4mPU'></q></dir></style></legend>

        在完成工作后立即删除 boost::thread 对象的最佳方法是什么?

        What’s the best way to delete boost::thread object right after its work is complete?(在完成工作后立即删除 boost::thread 对象的最佳方法是什么?)

            <tfoot id='EjcSK'></tfoot>
            • <small id='EjcSK'></small><noframes id='EjcSK'>

            • <legend id='EjcSK'><style id='EjcSK'><dir id='EjcSK'><q id='EjcSK'></q></dir></style></legend>
              <i id='EjcSK'><tr id='EjcSK'><dt id='EjcSK'><q id='EjcSK'><span id='EjcSK'><b id='EjcSK'><form id='EjcSK'><ins id='EjcSK'></ins><ul id='EjcSK'></ul><sub id='EjcSK'></sub></form><legend id='EjcSK'></legend><bdo id='EjcSK'><pre id='EjcSK'><center id='EjcSK'></center></pre></bdo></b><th id='EjcSK'></th></span></q></dt></tr></i><div id='EjcSK'><tfoot id='EjcSK'></tfoot><dl id='EjcSK'><fieldset id='EjcSK'></fieldset></dl></div>
                <tbody id='EjcSK'></tbody>
              • <bdo id='EjcSK'></bdo><ul id='EjcSK'></ul>
                  本文介绍了在完成工作后立即删除 boost::thread 对象的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我用一个 new 操作符创建了 boost::thread 对象并继续而不等待这个线程完成它的工作:

                  I create boost::thread object with a new operator and continue without waiting this thread to finish its work:

                  void do_work()
                  {
                      // perform some i/o work
                  }
                  
                  boost::thread *thread = new boost::thread(&do_work);
                  

                  我想,当工作完成时,有必要删除thread.不显式等待线程终止的最佳方法是什么?

                  I guess, it’s necessary to delete thread when the work is done. What’s the best way to this without explicitly waiting for thread termination?

                  推荐答案

                  boost::thread 对象的生命周期与本机线程的生命周期无关.boost::thread 对象可以随时超出范围.

                  The boost::thread object's lifetime and the native thread's lifetime are unrelated. The boost::thread object can go out of scope at any time.

                  来自 boost::thread 类 文档

                  正如文件的生命周期可能与代表文件的 iostream 对象的生命周期不同,执行线程的生命周期可能与代表执行线程的线程对象的生命周期不同.特别是,在调用 join() 之后,即使线程对象继续存在直到其正常生命周期结束,执行线程也将不再存在.反过来也是可能的;如果一个线程对象在没有首先调用 join() 的情况下被销毁,则该线程将继续执行,直到其初始函数完成.

                  Just as the lifetime of a file may be different from the lifetime of an iostream object which represents the file, the lifetime of a thread of execution may be different from the thread object which represents the thread of execution. In particular, after a call to join(), the thread of execution will no longer exist even though the thread object continues to exist until the end of its normal lifetime. The converse is also possible; if a thread object is destroyed without join() having first been called, the thread of execution continues until its initial function completes.

                  编辑:如果你只需要启动一个线程并且从不调用join,你可以使用线程的构造函数作为一个函数:

                  Edit: If you just need to start a thread and never invoke join, you can use the thread's constructor as a function:

                      // Launch thread.
                  boost::thread(&do_work); 
                  

                  然而,我不建议你这样做,即使你认为你确定线程会在 main() 之前完成.

                  However, I don't suggest you do that, even if you think you're sure the thread will complete before main() does.

                  这篇关于在完成工作后立即删除 boost::thread 对象的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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自动链接如何做出选择?)
                  <legend id='KO8Ha'><style id='KO8Ha'><dir id='KO8Ha'><q id='KO8Ha'></q></dir></style></legend>

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

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

                  • <tfoot id='KO8Ha'></tfoot>

                      <tbody id='KO8Ha'></tbody>

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