<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++核心準(zhǔn)則CP.42:不要在線程中無條件等待

          4999807d04ffd5a3cbf665db51896b75.webp

          CP.42: Don't?wait?without a condition

          CP.42:不要無條件等待


          Reason(原因)

          A?wait?without a condition can miss a wakeup or wake up simply to find that there is no work to do.

          無條件等待可能錯過喚醒,也可能喚醒之后發(fā)現(xiàn)無事可做。


          Example, bad(反面示例)

          std::condition_variable cv;std::mutex mx;
          void thread1(){ while (true) { // do some work ... std::unique_lock lock(mx); cv.notify_one(); // wake other thread }}
          void thread2(){ while (true) { std::unique_lock lock(mx); cv.wait(lock); // might block forever // do work ... }}

          Here, if some other thread consumes thread1's notification, thread2 can wait forever.

          這里,如果某個另外的線程消耗了線程1的通知,線程2會永遠等待。


          Example(示例)

          template<typename T>class Sync_queue {public:    void put(const T& val);    void put(T&& val);    void get(T& val);private:    mutex mtx;    condition_variable cond;    // this controls access    list q;};
          template<typename T>void Sync_queue::put(const T& val){ lock_guard lck(mtx); q.push_back(val); cond.notify_one();}
          template<typename T>void Sync_queue::get(T& val){ unique_lock lck(mtx); cond.wait(lck, [this] { return !q.empty(); }); // prevent spurious wakeup val = q.front(); q.pop_front();}

          Now if the queue is empty when a thread executing?get()?wakes up (e.g., because another thread has gotten to?get()?before it), it will immediately go back to sleep, waiting.

          現(xiàn)在,當(dāng)某個線程執(zhí)行g(shù)et喚醒時,如果隊列為空(例如,由用戶另外的線程已經(jīng)事先執(zhí)行了get),它會立刻回到休眠狀態(tài)繼續(xù)等待。


          Enforcement(實施建議)

          Flag all?waits without conditions.

          標(biāo)記所有無條件等待。


          原文鏈接

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp42-dont-wait-without-a-condition


          新書介紹

          以下是本人3月份出版的新書,拜托多多關(guān)注!


          81603a6dfb1bce6dd5ecbfe1c82fe615.webp

          本書利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設(shè)計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設(shè)計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設(shè)計模式的利弊,并合理運用設(shè)計模式。

          對設(shè)計模式感興趣而且希望隨學(xué)隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書中的示例作為設(shè)計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




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

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

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


          瀏覽 37
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <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>
                  国产亚洲精品久久久 | 成人做爱免费看 | 欧美精选网页 | 777大香蕉 | 人妻丰满熟妇av无码区蜜桃 |