<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準則CP.26:不要分離線程

          6be0ef5a5160e993311962b50734788e.webp

          CP.26: Don't?detach()?a thread

          CP.26:不要分離線程


          Reason(原因)

          Often, the need to outlive the scope of its creation is inherent in the?threads task, but implementing that idea by?detach?makes it harder to monitor and communicate with the detached thread. In particular, it is harder (though not impossible) to ensure that the thread completed as expected or lives for as long as expected.

          通常,生命周期超過創(chuàng)建它的線程的需求繼承自線程任務(wù),但是通過分離(detach)實現(xiàn)這個想法使得監(jiān)視和與分離的線程進行通信更加困難。在特殊情況下,很難(雖然不是不可能)確保線程可以像期望的那樣中止或繼續(xù)動作。


          Example(示例)

          void heartbeat();

          void use()
          {
          std::thread t(heartbeat); // don't join; heartbeat is meant to run forever
          t.detach();
          // ...
          }

          This is a reasonable use of a thread, for which?detach()?is commonly used. There are problems, though. How do we monitor the detached thread to see if it is alive? Something might go wrong with the heartbeat, and losing a heartbeat can be very serious in a system for which it is needed. So, we need to communicate with the heartbeat thread (e.g., through a stream of messages or notification events using a?condition_variable).

          這段代碼是線程的合理用法,其中通常會使用detach()。雖然這個做法存在問題。我們?nèi)绾伪O(jiān)視一個分離的線程以便知道它是否處于活動狀態(tài)?心跳線程可能發(fā)生某些問題,對于需要心跳功能的系統(tǒng)來說,丟失心跳是很嚴重的問題。因此,我們需要和心跳線程通信(例如通過一個信息流或使用condition_variable的通知事件)。

          An alternative, and usually superior solution is to control its lifetime by placing it in a scope outside its point of creation (or activation). For example:

          另外一個可選的,通常是更高級的做法是將它放到創(chuàng)建它(或者激活它)的作用域之外。例如:

          void heartbeat();

          gsl::joining_thread t(heartbeat); // heartbeat is meant to run "forever"

          This heartbeat will (barring error, hardware problems, etc.) run for as long as the program does.

          心跳線程將會和程序運行同樣長的時間(除非發(fā)生錯誤,硬件問題等情況)。

          Sometimes, we need to separate the point of creation from the point of ownership:

          有時,我們需要將生成視點和所有權(quán)視點相分離。

          void heartbeat();

          unique_ptr tick_tock {nullptr};

          void use()
          {
          // heartbeat is meant to run as long as tick_tock lives
          tick_tock = make_unique(heartbeat);
          // ...
          }

          Enforcement(實施建議)

          Flag?detach().

          標記分離(detach)操作。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp26-dont-detach-a-thread




          覺得本文有幫助?請分享給更多人。

          關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學習每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>


          瀏覽 38
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美乱伦小说 | 91丝袜足交 | 激情五月图片亚洲婷婷 | 欧美日韩国产精品成人在线 | 色婷婷激情AV视 |