{"id":3747,"date":"2019-08-06T11:18:34","date_gmt":"2019-08-06T03:18:34","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=3747"},"modified":"2019-08-06T11:18:34","modified_gmt":"2019-08-06T03:18:34","slug":"c%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b-%e7%ad%89%e5%be%85%e4%b8%8e%e5%94%a4%e9%86%92","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/3747","title":{"rendered":"C++\u5e76\u53d1\u7f16\u7a0b \u7b49\u5f85\u4e0e\u5524\u9192"},"content":{"rendered":"<h3>\u6761\u4ef6\u53d8\u91cf<\/h3>\n<p>\u6761\u4ef6\u53d8\u91cf, \u5305\u62ec(std::condition_variable \u548c std::condition_variable_any)<br \/>\n\u5b9a\u4e49\u5728 condition_variable \u5934\u6587\u4ef6\u4e2d, \u5b83\u4eec\u90fd\u9700\u8981\u4e0e\u4e92\u65a5\u91cf(\u4f5c\u4e3a\u540c\u6b65\u5de5\u5177)\u4e00\u8d77\u624d\u80fd\u5de5\u4f5c.<\/p>\n<p>std::condition_variable \u5141\u8bb8\u963b\u585e\u4e00\u4e2a\u7ebf\u7a0b, \u76f4\u5230\u6761\u4ef6\u8fbe\u6210.<\/p>\n<p>&nbsp;<\/p>\n<h3>\u6210\u5458\u51fd\u6570<\/h3>\n<p>void wait(std::unique_lock&lt;std::mutex&gt;&amp; lock);<br \/>\n\u7b49\u5f85, \u901a\u8fc7 notify_one(), notify_all()\u6216\u4f2a\u5524\u9192\u7ed3\u675f\u7b49\u5f85<\/p>\n<p>void wait(std::unique_lock&lt;std::mutex&gt;&amp; lock, Predicate pred);<br \/>\n\u7b49\u5f85, \u901a\u8fc7 notify_one(), notify_all()\u88ab\u8c03\u7528, \u5e76\u4e14\u8c13\u8bcd\u4e3a true \u65f6\u7ed3\u675f\u7b49\u5f85.<br \/>\npred \u8c13\u8bcd\u5fc5\u987b\u662f\u5408\u6cd5\u7684, \u5e76\u4e14\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u503c, \u8fd9\u4e2a\u503c\u53ef\u4ee5\u548cbool\u4e92\u76f8\u8f6c\u5316<\/p>\n<p>cv_status wait_until(std::unique_lock&lt;std::mutex&gt;&amp; lock, const std::chrono::time_point&lt;Clock, Duration&gt;&amp; absolute_time);<br \/>\n\u8c03\u7528 notify_one(), notify_all(), \u8d85\u65f6\u6216\u7ebf\u7a0b\u4f2a\u5524\u9192\u65f6, \u7ed3\u675f\u7b49\u5f85.<br \/>\n\u8fd4\u56de\u503c\u6807\u8bc6\u4e86\u662f\u5426\u8d85\u65f6.<\/p>\n<p>bool wait_until(std::unique_lock&lt;std::mutex&gt;&amp; lock, const std::chrono::time_point&lt;Clock, Duration&gt;&amp; absolute_time, Predicate pred);<br \/>\n\u7b49\u5f85, \u901a\u8fc7 notify_one(), notify_all(), \u8d85\u65f6, \u7ebf\u7a0b\u4f2a\u5524\u9192, \u5e76\u4e14\u8c13\u8bcd\u4e3a true \u65f6\u7ed3\u675f\u7b49\u5f85.<\/p>\n<p>cv_status wait_for(std::unique_lock&lt;std::mutex&gt;&amp; lock, const std::chrono::duration&lt;Rep, Period&gt;&amp; relative_time);<br \/>\n\u8c03\u7528 notify_one(), notify_all(), \u6307\u5b9a\u65f6\u95f4\u5185\u8fbe\u6210\u6761\u4ef6\u6216\u7ebf\u7a0b\u4f2a\u5524\u9192\u65f6\uff0c\u7ed3\u675f\u7b49\u5f85<\/p>\n<p>bool wait_for(std::unique_lock&lt;std::mutex&gt;&amp; lock, const std::chrono::duration&lt;Rep, Period&gt;&amp; relative_time, Predicate pred);<br \/>\n\u8c03\u7528 notify_one(), notify_all(), \u6307\u5b9a\u65f6\u95f4\u5185\u8fbe\u6210\u6761\u4ef6\u6216\u7ebf\u7a0b\u4f2a\u5524\u9192\u65f6\uff0c\u5e76\u4e14\u8c13\u8bcd\u4e3a true \u65f6\u7ed3\u675f\u7b49\u5f85.<\/p>\n<p>void notify_one() noexcept; \u5524\u9192\u4e00\u4e2a\u7b49\u5f85\u5f53\u524d std::condition_variable \u5b9e\u4f8b\u7684\u7ebf\u7a0b<br \/>\nvoid notify_all() noexcept; \u5524\u9192\u6240\u6709\u7b49\u5f85\u5f53\u524d std::condition_variable \u5b9e\u4f8b\u7684\u7ebf\u7a0b<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u8bbe\u8ba1:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">#ifndef _THREAD_SAFE_QUEUE_\r\n#define _THREAD_SAFE_QUEUE_\r\n\r\n#include &lt;condition_variable&gt;\r\n#include &lt;mutex&gt;\r\n#include &lt;queue&gt;\r\n#include &lt;memory&gt;\r\n\r\ntemplate&lt;typename Ty, typename ConditionVar = std::condition_variable, typename Mutex = std::mutex&gt;\r\nclass ThreadSafeQueue\r\n{\r\n    typedef std::queue&lt;Ty&gt;                Queue;\r\n    typedef std::shared_ptr&lt;Ty&gt;           SharedPtr;\r\n    typedef std::lock_guard&lt;Mutex&gt;        MutexLockGuard;\r\n    typedef std::unique_lock&lt;Mutex&gt;       MutexUniqueLock;\r\n\r\npublic:\r\n    explicit ThreadSafeQueue() {}\r\n    ~ThreadSafeQueue() {}\r\n\r\n    ThreadSafeQueue(const ThreadSafeQueue&amp;) = delete;\r\n    ThreadSafeQueue&amp; operator=(const ThreadSafeQueue&amp;) = delete;\r\n\r\n    bool IsEmpty() const\r\n    {\r\n        MutexLockGuard lk(mMutex);\r\n        return mQueue.empty();\r\n    }\r\n\r\n    void WaitAndPop(Ty&amp; value)\r\n    {\r\n        MutexUniqueLock lk(mMutex);\r\n        mConVar.wait(lk, [this] {\r\n            return !mQueue.empty(); \r\n        });\r\n        value = mQueue.front();\r\n        mQueue.pop();\r\n    }\r\n\r\n    SharedPtr WaitAndPop()\r\n    {\r\n        MutexUniqueLock lk(mMutex);\r\n        mConVar.wait(lk, [this] {\r\n            return !mQueue.empty();\r\n        });\r\n        SharedPtr sp = std::make_shared&lt;Ty&gt;(mQueue.front());\r\n        mQueue.pop();\r\n        return sp;\r\n    }\r\n\r\n    bool TryPop(Ty&amp; value)\r\n    {\r\n        MutexLockGuard lk(mMutex);\r\n        if (mQueue.empty())\r\n            return false;\r\n        value = mQueue.front();\r\n        mQueue.pop();\r\n        return true;\r\n    }\r\n\r\n    SharedPtr TryPop()\r\n    {\r\n        MutexLockGuard lk(mMutex);\r\n        if (mQueue.empty())\r\n            return false;\r\n        SharedPtr sp = std::make_shared&lt;Ty&gt;(mQueue.front());\r\n        mQueue.pop();\r\n        return sp;\r\n    }\r\n\r\n    void Push(const Ty&amp; value)\r\n    {\r\n        MutexLockGuard lk(mMutex);\r\n        mQueue.push(value);\r\n        mConVar.notify_all();\r\n    }\r\n\r\nprivate:\r\n    mutable Mutex             mMutex;\r\n    ConditionVar              mConVar;\r\n    Queue                     mQueue;\r\n};\r\n\r\n#endif \/\/ _THREAD_SAFE_QUEUE_<\/pre>\n<p>&nbsp;<\/p>\n<p>\u53e6\u4e00\u4e2a\u7248\u672c, \u4f7f\u7528 shared_ptr \u4f5c\u4e3a\u6210\u5458\u5bf9\u961f\u5217\u7684\u6027\u80fd\u6709\u5f88\u5927\u7684\u63d0\u5347, \u5176\u5728push\u65f6\u51cf\u5c11\u4e86\u4e92\u65a5\u91cf\u6301\u6709\u7684\u65f6\u95f4, \u5141\u8bb8\u5176\u5b83\u7ebf\u7a0b\u5728\u5206\u914d\u5185\u5b58\u7684\u540c\u65f6,\u5bf9\u961f\u5217\u8fdb\u884c\u5176\u5b83\u64cd\u4f5c.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">template&lt;typename Ty, typename ConditionVar = std::condition_variable, typename Mutex = std::mutex&gt;\r\nclass ThreadSafeQueue\r\n{\r\n    typedef std::shared_ptr&lt;Ty&gt;               SharedPtr;\r\n    typedef std::queue&lt;SharedPtr&gt;             Queue;\r\n    typedef std::shared_ptr&lt;Ty&gt;               SharedPtr;\r\n    typedef std::lock_guard&lt;Mutex&gt;            MutexLockGuard;\r\n    typedef std::unique_lock&lt;Mutex&gt;           MutexUniqueLock;\r\n\r\npublic:\r\n    explicit ThreadSafeQueue() {}\r\n    ~ThreadSafeQueue() {}\r\n\r\n    ThreadSafeQueue(const ThreadSafeQueue&amp;) = delete;\r\n    ThreadSafeQueue&amp; operator=(const ThreadSafeQueue&amp;) = delete;\r\n\r\n    bool IsEmpty() const\r\n    {\r\n        MutexLockGuard lk(mMutex);\r\n        return mQueue.empty();\r\n    }\r\n\r\n    void WaitAndPop(Ty&amp; value)\r\n    {\r\n        MutexUniqueLock lk(mMutex);\r\n        mConVar.wait(lk, [this] {\r\n            return !mQueue.empty();\r\n        });\r\n        value = std::move(*mQueue.front());\r\n        mQueue.pop();\r\n    }\r\n\r\n    SharedPtr WaitAndPop()\r\n    {\r\n        MutexUniqueLock lk(mMutex);\r\n        mConVar.wait(lk, [this] {\r\n            return !mQueue.empty();\r\n        });\r\n        SharedPtr sp = mQueue.front();\r\n        mQueue.pop();\r\n        return sp;\r\n    }\r\n\r\n    bool TryPop(Ty&amp; value)\r\n    {\r\n        MutexLockGuard lk(mMutex);\r\n        if (mQueue.empty())\r\n            return false;\r\n        value = std::move(*mQueue.front());\r\n        mQueue.pop();\r\n        return true;\r\n    }\r\n\r\n    SharedPtr TryPop()\r\n    {\r\n        MutexLockGuard lk(mMutex);\r\n        if (mQueue.empty())\r\n            return false;\r\n        SharedPtr sp = mQueue.front();\r\n        mQueue.pop();\r\n        return sp;\r\n    }\r\n\r\n    void Push(const Ty&amp; value)\r\n    {\r\n        SharedPtr p = std::make_shared&lt;Ty&gt;(value);\r\n        MutexLockGuard lk(mMutex);\r\n        mQueue.push(p);\r\n        mConVar.notify_all();\r\n    }\r\n\r\nprivate:\r\n    mutable Mutex             mMutex;\r\n    ConditionVar              mConVar;\r\n    Queue                     mQueue;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<h3>std::future<\/h3>\n<p>\u671f\u671b(std::future)\u53ef\u4ee5\u7528\u6765\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u4e0a\u7684\u5f02\u6b65\u7ed3\u679c, \u5176\u5b9e\u4f8b\u53ef\u4ee5\u5728\u4efb\u610f\u65f6\u95f4\u5f15\u7528\u5f02\u6b65\u7ed3\u679c.<br \/>\nC++\u5305\u62ec\u4e24\u79cd\u671f\u671b, std::future(\u552f\u4e00\u671f\u671b) \u548c std::shared_future(\u5171\u4eab\u671f\u671b)<br \/>\nstd::future \u7684\u5b9e\u4f8b\u53ea\u80fd\u4e0e\u4e00\u4e2a\u6307\u5b9a\u4e8b\u4ef6\u76f8\u5173\u8054.<br \/>\nstd::shared_future \u7684\u5b9e\u4f8b\u80fd\u5173\u8054\u591a\u4e2a\u4e8b\u4ef6, \u5b83\u4eec\u540c\u65f6\u53d8\u4e3a\u5c31\u7eea\u72b6\u6001, \u5e76\u4e14\u53ef\u4ee5\u8bbf\u95ee\u4e0e\u4e8b\u4ef6\u76f8\u5173\u7684\u4efb\u4f55\u6570\u636e.<br \/>\n\u5728\u4e0e\u6570\u636e\u65e0\u5173\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u4f7f\u7528 std::future&lt;void&gt; \u4e0e std::shared_future&lt;void&gt; \u7684\u7279\u5316\u6a21\u677f.<br \/>\n\u671f\u671b\u5bf9\u8c61\u672c\u8eab\u5e76\u4e0d\u63d0\u4f9b\u540c\u6b65\u8bbf\u95ee, \u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u8981\u8bbf\u95ee\u4e00\u4e2a\u72ec\u7acb\u7684\u671f\u671b\u5bf9\u8c61, \u9700\u8981\u4f7f\u7528\u4e92\u65a5\u4f53\u8fdb\u884c\u4fdd\u62a4.<\/p>\n<h3>std::packaged_task<\/h3>\n<p>std::packaged_task \u53ef\u5305\u88c5\u4e00\u4e2a\u51fd\u6570\u6216\u53ef\u8c03\u7528\u7684\u5bf9\u8c61, \u5e76\u4e14\u5141\u8bb8\u5f02\u6b65\u83b7\u53d6\u8be5\u53ef\u8c03\u7528\u5bf9\u8c61\u4ea7\u751f\u7684\u7ed3\u679c, \u8fd4\u56de\u503c\u901a\u8fc7 get_future \u8fd4\u56de\u7684 std::future \u5bf9\u8c61\u53d6\u5f97, \u5176\u8fd4\u56de\u7684 std::future \u7684\u6a21\u677f\u7c7b\u578b\u4e3a std::packaged_task \u6a21\u677f\u51fd\u6570\u7b7e\u540d\u4e2d\u7684\u8fd4\u56de\u503c\u7c7b\u578b.<br \/>\nstd::packaged_task \u5bf9\u8c61\u88ab\u8c03\u7528\u65f6, \u5c31\u4f1a\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u6216\u53ef\u8c03\u7528\u5bf9\u8c61, \u5c06\u671f\u671b\u7f6e\u4e3a\u5c31\u7eea, \u5e76\u5b58\u50a8\u8fd4\u56de\u503c.<br \/>\nstd::packaged_task \u7684\u6a21\u677f\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\u7b7e\u540d, \u5982 int(std::string&amp;, double*), \u6784\u9020 std::packaged_task \u5b9e\u4f8b\u65f6\u5fc5\u987b\u4f20\u5165\u4e00\u4e2a\u53ef\u4ee5\u5339\u914d\u7684\u51fd\u6570\u6216\u53ef\u8c03\u7528\u5bf9\u8c61, \u4e5f\u53ef\u4ee5\u662f\u9690\u85cf\u8f6c\u6362\u80fd\u5339\u914d\u7684.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">std::packaged_task&lt;std::string(const std::string&amp;)&gt; task([](std::string str) {\r\n    std::stringstream stm;\r\n    stm &lt;&lt; \"tid:\" &lt;&lt; std::this_thread::get_id() &lt;&lt; \", str:\" &lt;&lt; str &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; stm.str();\r\n    std::this_thread::sleep_for(std::chrono::seconds(1));\r\n    return std::string(\"MSG:Hello\");\r\n});\r\nstd::future&lt;std::string&gt; f = task.get_future();\r\nstd::thread t(std::move(task), std::string(\"package task test\"));\r\nt.detach();\r\n\/\/ \u8c03\u7528 f.get \u8fd4\u56de\u7ed3\u679c, \u4f46\u662f\u987b\u963b\u585e\u7b49\u5230\u4efb\u52a1\u6267\u884c\u5b8c\u6210\r\nstd::cout &lt;&lt; \"main tid:\" &lt;&lt; std::this_thread::get_id() &lt;&lt; \", result: \" &lt;&lt; f.get() &lt;&lt; std::endl;<\/pre>\n<h3>std::promise<\/h3>\n<p>std::promise \u7c7b\u578b\u6a21\u677f\u63d0\u4f9b\u8bbe\u7f6e\u5f02\u6b65\u7ed3\u679c\u7684\u65b9\u6cd5, \u8fd9\u6837\u5176\u4ed6\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u901a\u8fc7 std::future \u5b9e\u4f8b\u6765\u7d22\u5f15\u8be5\u7ed3\u679c.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">class SquareRoot\r\n{\r\n    std::promise&lt;double&gt;&amp; prom;\r\npublic: \r\n    SquareRoot(std::promise&lt;double&gt;&amp; p) : prom(p) {}\r\n    ~SquareRoot() {}\r\n    void operator()(double x)\r\n    {\r\n        if (x &lt; 0)\r\n        {\r\n            prom.set_exception(std::make_exception_ptr(std::out_of_range(\"x&lt;0\")));\r\n        }\r\n        else\r\n        {\r\n            double result = std::sqrt(x);\r\n            prom.set_value(result);\r\n        }\r\n    }\r\n};\r\n\r\nstd::promise&lt;double&gt; prom;\r\nSquareRoot p(prom);\r\nstd::thread t(std::bind(&amp;SquareRoot::operator(), &amp;p, 1));\r\n\/\/std::thread t(std::bind(&amp;SquareRoot::operator(), &amp;p, -1));\r\nstd::future&lt;double&gt; f = prom.get_future();\r\ntry\r\n{\r\n    double v = f.get();\r\n    std::cout &lt;&lt; \"value:\" &lt;&lt; v &lt;&lt; std::endl;\r\n}\r\ncatch (std::exception&amp; e)\r\n{\r\n    std::cout &lt;&lt; \"exception:\" &lt;&lt; e.what() &lt;&lt; std::endl;\r\n}\r\nt.join();<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6761\u4ef6\u53d8\u91cf \u6761\u4ef6\u53d8\u91cf, \u5305\u62ec(std::c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-3747","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=3747"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/3747\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=3747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=3747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=3747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}