{"id":153831,"date":"2020-09-09T01:16:57","date_gmt":"2020-09-08T17:16:57","guid":{"rendered":"http:\/\/4563.org\/?p=153831"},"modified":"2020-09-09T01:16:57","modified_gmt":"2020-09-08T17:16:57","slug":"condition-variable-%e6%98%af%e6%80%8e%e4%b9%88%e5%91%8a%e8%af%89%e8%b0%83%e5%ba%a6%e5%99%a8%e8%bf%99%e4%b8%aa%e7%ba%bf%e7%a8%8b%e4%b8%8d%e5%ba%94%e8%af%a5%e8%a2%ab%e8%b0%83%e5%ba%a6%ef%bc%8c%e5%8f%88","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=153831","title":{"rendered":"Condition variable \u662f\u600e\u4e48\u544a\u8bc9\u8c03\u5ea6\u5668\u8fd9\u4e2a\u7ebf\u7a0b\u4e0d\u5e94\u8be5\u88ab\u8c03\u5ea6\uff0c\u53c8\u662f\u600e\u6837\u5524\u9192\u7684\u5462\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Condition variable \u662f\u600e\u4e48\u544a\u8bc9\u8c03\u5ea6\u5668\u8fd9\u4e2a\u7ebf\u7a0b\u4e0d\u5e94\u8be5\u88ab\u8c03\u5ea6\uff0c\u53c8\u662f\u600e\u6837\u5524\u9192\u7684\u5462\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dangyuluo <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u5de5\u4f5c\u4e2d\u53d1\u73b0\u4e86\u4e00\u4e2a Bug\uff0c\u5c31\u662f\u6709\u4e00\u4e2a condition variable \u5728\u4e34\u754c\u533a\u5185\u8c03\u7528\u4e86<code>notify_one<\/code>\uff0c\u8ba9\u53e6\u4e00\u4e2a\u5728<code>cv.wait<\/code>\u7684\u7ebf\u7a0b\u5524\u9192\u4e86\u4e00\u4e0b\u7136\u540e\u7acb\u9a6c\u53c8\u88ab blocked \u4e86\u3002\u867d\u7136\u4e0d\u5f71\u54cd\u903b\u8f91\uff0c\u4f46\u662f\u4f1a\u5e26\u6765\u989d\u5916\u7684\u7ebf\u7a0b\u5207\u6362\u8d1f\u62c5\u3002<\/p>\n<p>\u516c\u53f8\u7684\u5927\u795e\u8bf4: You shouldn&#8217;t <code>notify_one()<\/code> while holding the mutex.<\/p>\n<p>\u867d\u7136\u95ee\u9898\u89e3\u51b3\u4e86\uff08\u901a\u8fc7<code>lock.unlock(); cv.notify_one(); lock.lock();<\/code>\uff09\uff0c\u4f46\u662f\u8fd9\u5f15\u8d77\u4e86\u6211\u597d\u5947\uff0c<code>cv.wait<\/code>\u662f\u5982\u4f55\u544a\u8bc9\u8c03\u5ea6\u5668\u672c\u7ebf\u7a0b\u4e0d\u5e94\u8be5\u88ab\u8c03\u5ea6\u7684\uff1f\u53c8\u662f\u600e\u6837\u5728\u5f97\u5230\u4fe1\u53f7\u540e\u5524\u9192\u7ebf\u7a0b\u7684\uff1f<\/p>\n<p>\u8bf7\u9ad8\u4eba\u5e2e\u5fd9\u89e3\u7b54\u4e00\u4e0b\u5427\uff0c\u8c22\u8c22\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3227687\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ysc3839 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Windows \u4e0b cv.wait \u4f30\u8ba1\u662f\u8c03\u7528 SleepConditionVariableSRW\uff0c\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b ReactOS \u7684\u5b9e\u73b0 https:\/\/doxygen.reactos.org\/da\/d99\/condvar_8c_source.html<br \/>RtlSleepConditionVariableSRW \u662f\u76f4\u63a5\u8c03\u7528 InternalSleep \u7684\uff0c\u800c InternalSleep \u5219\u662f\u901a\u8fc7\u8c03\u7528 NtWaitForKeyedEvent \u6765\u7b49\u5f85\u7684\uff0c\u7ed3\u8bba\u662f\u7528\u4e86\u5185\u6838\u63d0\u4f9b\u7684\u673a\u5236\u3002<\/p>\n<p>Linux \u4e0b cv.wait \u4f30\u8ba1\u662f\u8c03\u7528 pthread_cond_wait\uff0c\u53ef\u4ee5\u53c2\u8003\u5404\u7c7b pthread \u7684\u5b9e\u73b0\uff0c\u4e3a\u4e86\u4ee3\u7801\u66f4\u52a0\u7b80\u5355\uff0c\u8fd9\u91cc\u9009\u62e9\u53c2\u8003 musl (\u4e0b\u9762\u7684 GitHub \u4ed3\u5e93\u662f\u4e00\u4e2a\u975e\u5b98\u65b9\u7684 musl mirror\uff0c\u7528 GitHub \u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u641c\u7d22)<br \/>pthread_cond_wait \u662f\u76f4\u63a5\u8c03\u7528 pthread_cond_timedwait \u7684 https:\/\/github.com\/ifduyue\/musl\/blob\/0b0640219338b80cf47026d1970b5503414ed7f3\/src\/thread\/pthread_cond_wait.c<br \/>pthread_cond_timedwait \u662f\u901a\u8fc7 __timedwait_cp \u6765\u7b49\u5f85\u7684 https:\/\/github.com\/ifduyue\/musl\/blob\/0b0640219338b80cf47026d1970b5503414ed7f3\/src\/thread\/pthread_cond_timedwait.c#L100<br \/>\u800c __timedwait_cp \u5219\u662f\u901a\u8fc7 futex syscall \u6765\u7b49\u5f85\u7684 https:\/\/github.com\/ifduyue\/musl\/blob\/0b0640219338b80cf47026d1970b5503414ed7f3\/src\/thread\/__timedwait.c#L52<br \/>\u7ed3\u8bba\u4e5f\u662f\u7528\u4e86\u5185\u6838\u63d0\u4f9b\u7684\u673a\u5236\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3227688\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : dangyuluo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ysc3839 \u8c22\u8c22 \u975e\u5e38\u6709\u5e2e\u52a9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3227689\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codehz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5efa\u8bae\u76f4\u63a5\u770b\u6b63\u786e\u7528\u6cd5\uff0c\u5177\u4f53\u5b9e\u73b0\u662f\u6709\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u7684\uff08\u6240\u4ee5\u4e0d\u80fd\u4f9d\u8d56\u5e95\u5c42\u7684\u884c\u4e3a<br \/>https:\/\/zh.cppreference.com\/w\/cpp\/thread\/condition_variable                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Condition variabl&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\/153831"}],"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=153831"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/153831\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153831"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}