{"id":316512,"date":"2021-02-04T17:12:27","date_gmt":"2021-02-04T09:12:27","guid":{"rendered":"http:\/\/4563.org\/?p=316512"},"modified":"2021-02-04T17:12:27","modified_gmt":"2021-02-04T09:12:27","slug":"%e5%85%b3%e4%ba%8e-c-11-%e4%b8%ad%e7%9a%84-condition_variable-%e6%9c%89%e4%b8%80%e5%a4%84%e6%b2%a1%e6%83%b3%e6%98%8e%e7%99%bd%e3%80%82","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=316512","title":{"rendered":"\u5173\u4e8e C++ 11 \u4e2d\u7684 condition_variable. \u6709\u4e00\u5904\u6ca1\u60f3\u660e\u767d\u3002"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e C++ 11 \u4e2d\u7684 condition_variable. \u6709\u4e00\u5904\u6ca1\u60f3\u660e\u767d\u3002               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : V2WT <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u9996\u5148\u6839\u636e: <strong>The C++ core guideline<\/strong> \u7684\u7b2c 42 \u6761\u4e2d\u63d0\u5230\uff1a\u4e0d\u8981\u5728\u6ca1\u6709\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\u7b49\u5f85\uff08 Don\u2019t wait without a condition).\u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3\uff1a<\/p>\n<ul>\n<li>\u5524\u9192\u4e22\u5931<\/li>\n<li>\u865a\u5047\u5524\u9192<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u662f\u8303\u4f8b\u4ee3\u7801:<\/p>\n<pre><code>#include &lt;condition_variable&gt; #include &lt;iostream&gt; #include &lt;thread&gt;  std::mutex mutex_; std::condition_variable condVar;  bool dataReady{false};  void waitingForWork(){     std::cout &lt;&lt; \"Waiting \" &lt;&lt; std::endl;     std::unique_lock&lt;std::mutex&gt; lck(mutex_);     condVar.wait(lck, []{ return dataReady; });   \/\/ (4)     std::cout &lt;&lt; \"Running \" &lt;&lt; std::endl; }  void setDataReady(){     {         std::lock_guard&lt;std::mutex&gt; lck(mutex_);         dataReady = true;     }     std::cout &lt;&lt; \"Data prepared\" &lt;&lt; std::endl;     condVar.notify_one();                        \/\/ (3) }  int main(){   std::cout &lt;&lt; std::endl;   std::thread t1(waitingForWork);               \/\/ (1)   std::thread t2(setDataReady);                 \/\/ (2)   t1.join();   t2.join();   std::cout &lt;&lt; std::endl; } <\/code><\/pre>\n<p>\u8fd9\u91cc\u518d\u8d34\u4e00\u4e0b\u641c\u5230\u7684\u8d44\u6599\u91cc condition_variable \u7684\u5904\u7406\u903b\u8f91\uff1a <code>wait (lck, pred);<\/code> \u5176\u5b9e\u7b49\u4ef7\u4e8e <code>while (!pred()) wait(lck);<\/code>\uff0c\u5b83\u7684\u8fd0\u884c\u673a\u5236\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u7ebf\u7a0b\u83b7\u53d6 mutex \u7684\u9501\uff0c\u7136\u540e\u5bf9 predicate \u7684\u7ed3\u679c\u8fdb\u884c\u68c0\u67e5\uff1a true\uff1a\u7ebf\u7a0b\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff1b false\uff1acondVar.wait() \u89e3\u9501 mutex\uff0c\u7136\u540e\u7ebf\u7a0b\u8fdb\u5165\u7b49\u5f85\uff08\u963b\u585e\uff09\u72b6\u6001\u3002<\/li>\n<li>\u5047\u5982 condVar \u5df2\u7ecf\u5728\u7b49\u5f85\u72b6\u6001\uff0c\u6b64\u65f6\u5f97\u5230\u901a\u77e5\uff08\u4e0d\u7ba1\u53d1\u9001\u7ebf\u7a0b\u53d1\u9001\uff0c\u8fd8\u662f\u865a\u5047\u5524\u9192\uff09\uff1a \u7ebf\u7a0b\u8fdb\u5165\u975e\u963b\u585e\u72b6\u6001\uff0c\u7136\u540e\u91cd\u65b0\u83b7\u53d6 mutex \u7684\u9501\u3002 \u7ebf\u7a0b\u68c0\u67e5 predicate \u7684\u7ed3\u679c\uff1a true\uff1a\u7ebf\u7a0b\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff1b false\uff1acondVar.wait() \u89e3\u9501 mutex\uff0c\u7136\u540e\u7ebf\u7a0b\u8fdb\u5165\u7b49\u5f85\uff08\u963b\u585e\uff09\u72b6\u6001\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u91cc\u6211\u7684\u95ee\u9898\u662f\uff1a \u5047\u8bbe\u8fd0\u884c\u6b65\u9aa4\u662f\u8fd9\u6837\uff1a<\/p>\n<ol>\n<li>t2 \u7ebf\u7a0b(setDataReady)\u5148\u6267\u884c\u4e86\u3002\u8fd9\u4e2a\u65f6\u5019<code>dataReady = true<\/code>. \u5e76\u4e14 <code>notify_one<\/code>\u4e86\u3002<\/li>\n<li>t1 \u7ebf\u7a0b(waitingForWork)\u91cc wait \u7684 predicate \u4e3a true.\u76f4\u63a5\u5f80\u4e0b\u6267\u884c\u4e86\u3002\u5e76\u4e0d\u4f1a\u963b\u585e\u5728 condVar.wait \u7684\u9636\u6bb5(\u6839\u636e\u4e0a\u9762\u7684\u8fd0\u884c\u673a\u5236\u662f\u8fd9\u6837)\u3002<\/li>\n<\/ol>\n<p>\u6240\u4ee5\u8fd9\u4e2a\u65f6\u5019\u76f8\u5f53\u4e8e<strong>\u7a7a\u767d notify_one<\/strong>\u4e86\u4e00\u6b21\u3002\u5b9e\u9645\u4e0a t1 \u7ebf\u7a0b\u7684\u8fd0\u884c\u53ea\u8ddf <code>dataReady<\/code>\u6709\u5173\uff0c\u800c\u8ddf condition_variable \u65e0\u5173\u4e86\u3002\u90a3\u4e48\u8fd9\u6b21\u7684<code>notify_one<\/code>\u7684\u4fe1\u53f7\u5230\u5e95\u53bb\u54ea\u91cc\u4e86\u5462\uff1f<\/p>\n<p>\u4e0d\u77e5\u9053\u6211\u6709\u6ca1\u6709\u63cf\u8ff0\u6e05\u695a\u95ee\u9898\u3002\u6709\u5927\u4f6c\u80fd\u89e3\u7b54\u4e00\u4e0b\u5417\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5109464\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chuckzhou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             notify_one \u4f1a\u68c0\u67e5\u662f\u5426\u6709 waiter\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5565\u4e5f\u4e0d\u5e72\u5c31\u9000\u51fa\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5109465\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : V2WT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chuckzhou \u5047\u8bbe\u73b0\u5728\u6709\u4e00\u4e2a Thread t3 = t2. \u8fd9\u6837\u4e00\u6b21 notify_one(). \u56e0\u4e3a\u4fee\u6539\u4e86\u6807\u5fd7\u4f4d\u7f6e\u3002\u5b9e\u9645\u4e0a t2 t3 \u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u6267\u884c\u3002\u90a3\u8fd9\u4e2a condition_variable \u8fd8\u6709\u4ec0\u4e48\u7528\u5462\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5109466\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : V2WT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chuckzhou \u554a\u8fd9\u662f\u6211\u7406\u89e3\u6709\u95ee\u9898\u4e86\u3002\u591a\u4e2a\u540c\u65f6\u7b49\u7684\u65f6\u5019\uff0c\u5e94\u8be5\u7b49\u5230\u4e86\u8981\u628a flag \u53d6\u53cd\u7684\u3002 \u4e0d\u597d\u610f\u601d\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5109467\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chuckzhou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @V2WT \u5982\u679c\u662f\u4f60\u8bf4\u7684\u8fd9\u79cd\u60c5\u51b5\uff0cpredicate \u5c31\u4e0d\u5e94\u8be5\u8fd9\u4e48\u5199\u3002<br \/>\u4e00\u4e2a\u5e38\u89c1\u7684\u573a\u666f\u5c31\u662f\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u3002<br \/>\u4e00\u4e2a\u7ebf\u7a0b\u751f\u4ea7\u4e86\u4e00\u4e2a\u6570\u636e\uff0c\u6bd4\u5982\u628a\u6570\u636e\u653e\u5230\u4e00\u4e2a list \u91cc\u9762\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u80fd\u6709\u5f88\u591a\u6d88\u8d39\u8005\uff0c\u4f46\u662f\u53ea\u6709\u4e00\u4e2a\u6570\u636e\uff0c\u5982\u679c\u4f60\u7684 predicate \u662f dataReady\uff0c\u90a3\u786e\u5b9e\u4f1a\u5bfc\u81f4\u6d88\u8d39\u8005\u90fd\u8fd0\u884c\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u3002<br \/>\u6240\u4ee5\u4f60\u7684 predicate \u8981\u5199\u6210 !list.empty() \u8fd9\u6837\uff0c\u7b2c\u4e00\u4e2a\u7ebf\u7a0b\u628a\u6570\u636e\u53d6\u8d70\u4e86\u4e4b\u540e\uff0c\u7b2c\u4e8c\u4e2a\u7ebf\u7a0b\u5c31\u4f1a\u7ee7\u7eed\u7b49\u5f85\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5109468\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : V2WT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chuckzhou \u662f\u7684\u3002\u540e\u6765\u60f3\u60f3\u660e\u767d\u4e86\u3002\u611f\u8c22\u7b54\u590d\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e C++ 11 \u4e2d\u7684 cond&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/316512"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=316512"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/316512\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316512"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}