{"id":1869,"date":"2022-11-25T22:13:39","date_gmt":"2022-11-25T14:13:39","guid":{"rendered":"https:\/\/courtship.top\/?p=1869"},"modified":"2022-11-25T22:13:39","modified_gmt":"2022-11-25T14:13:39","slug":"c%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2022\/11\/25\/c%e7%ba%bf%e7%a8%8b\/","title":{"rendered":"C++\u7ebf\u7a0b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u642c\u8fd0\u81ea<a href=\"https:\/\/www.cnblogs.com\/zizbee\/p\/13520823.html\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >https:\/\/www.cnblogs.com\/zizbee\/p\/13520823.html<\/a><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u6982\u5ff5<\/h2>\n\n\n\n<p>\u7ebf\u7a0b\uff1a\u662f\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u8fdb\u884c\u8fd0\u7b97\u8c03\u5ea6\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u5b83\u88ab\u5305\u542b\u5728\u8fdb\u7a0b\u4e4b\u4e2d\uff0c\u8fdb\u7a0b\u5305\u542b\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u7ebf\u7a0b\u3002<strong>\u8fdb\u7a0b\u53ef\u4ee5\u7406\u89e3\u4e3a\u5b8c\u6210\u4e00\u4ef6\u4e8b\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u7ebf\u7a0b\u53ef\u4ee5\u7406\u89e3\u4e3a\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u4e2d\u7684\u7684\u4e00\u4e2a\u6b65\u9aa4\uff0c\u53ef\u80fd\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u5c31\u8fd9\u53ea\u6709\u4e00\u4e2a\u6b65\u9aa4\uff0c\u4e5f\u53ef\u80fd\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u6709\u591a\u4e2a\u6b65\u9aa4\u3002<\/strong><\/p>\n\n\n\n<p>\u591a\u7ebf\u7a0b\uff1a\u591a\u7ebf\u7a0b\u662f\u5b9e\u73b0\u5e76\u53d1\uff08\u5e76\u884c\uff09\u7684\u624b\u6bb5\uff0c\u5e76\u53d1\uff08\u5e76\u884c\uff09\u5373\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\uff0c\u4e00\u822c\u800c\u8a00\uff0c\u591a\u7ebf\u7a0b\u5c31\u662f\u628a\u6267\u884c\u4e00\u4ef6\u4e8b\u60c5\u7684\u5b8c\u6574\u6b65\u9aa4\u62c6\u5206\u4e3a\u591a\u4e2a\u5b50\u6b65\u9aa4\uff0c\u7136\u540e\u4f7f\u5f97\u8fd9\u591a\u4e2a\u6b65\u9aa4\u540c\u65f6\u6267\u884c\u3002<\/p>\n\n\n\n<p>C++\u591a\u7ebf\u7a0b\uff1a\uff08\u7b80\u5355\u60c5\u51b5\u4e0b\uff09C++\u591a\u7ebf\u7a0b\u4f7f\u7528\u591a\u4e2a\u51fd\u6570\u5b9e\u73b0\u5404\u81ea\u529f\u80fd\uff0c\u7136\u540e\u5c06\u4e0d\u540c\u51fd\u6570\u751f\u6210\u4e0d\u540c\u7ebf\u7a0b\uff0c\u5e76\u540c\u65f6\u6267\u884c\u8fd9\u4e9b\u7ebf\u7a0b\uff08\u4e0d\u540c\u7ebf\u7a0b\u53ef\u80fd\u5b58\u5728\u4e00\u5b9a\u7a0b\u5ea6\u7684\u6267\u884c\u5148\u540e\u987a\u5e8f\uff0c\u4f46\u603b\u4f53\u4e0a\u53ef\u4ee5\u770b\u505a\u540c\u65f6\u6267\u884c\uff09\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u7ebf\u7a0b<\/h3>\n\n\n\n<p>\u9996\u5148\u8981\u5f15\u5165\u5934\u6587\u4ef6#include&lt;thread>(C++11\u7684\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u591a\u7ebf\u7a0b\u5e93)\uff0c\u8be5\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86thread\u7c7b\uff0c\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5373\u5b9e\u4f8b\u5316\u4e00\u4e2a\u8be5\u7c7b\u7684\u5bf9\u8c61\uff0c\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u5019\u8c03\u7528\u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u5c31\u662f\u51fd\u6570\u540d\uff0cthread th1(proc1)\uff1b\u5982\u679c\u4f20\u9012\u8fdb\u53bb\u7684\u51fd\u6570\u672c\u8eab\u9700\u8981\u4f20\u9012\u53c2\u6570\uff0c\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\u5c06\u8fd9\u4e9b\u53c2\u6570\u6309\u5e8f\u5199\u5230\u51fd\u6570\u540d\u540e\u9762\uff0cthread th1(proc1,a,b);<strong>\u53ea\u8981\u521b\u5efa\u4e86\u7ebf\u7a0b\u5bf9\u8c61<\/strong>\uff08\u4f20\u9012\u201c\u51fd\u6570\u540d\/\u53ef\u8c03\u7528\u5bf9\u8c61\u201d\u4f5c\u4e3a\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\uff09\uff0c<strong>\u7ebf\u7a0b\u5c31\u5f00\u59cb\u6267\u884c<\/strong>\uff08std::thread \u6709\u4e00\u4e2a\u65e0\u53c2\u6784\u9020\u51fd\u6570\u91cd\u8f7d\u7684\u7248\u672c\uff0c\u4e0d\u4f1a\u521b\u5efa\u5e95\u5c42\u7684\u7ebf\u7a0b\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u963b\u585e\u673a\u5236 \/ \u8d44\u6e90\u56de\u6536<\/h3>\n\n\n\n<p>join\u4f1a\u5bfc\u81f4\u8c03\u7528\u7ebf\u7a0b\uff0c\u4f46\u963b\u585e\u5e76\u4e0d\u662f\u76ee\u7684\uff0cjoin\uff08\u8fde\u63a5\uff09\u5b50\u7ebf\u7a0b\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u56de\u6536\u5df2\u7ecf\u9000\u51fa\u7684\u5b50\u7ebf\u7a0b\u8d44\u6e90\u3002\u5982\u679c\u4e0d\u56de\u6536\u8d44\u6e90\uff0c\u5c31\u4f1a\u5bfc\u81f4\u8d44\u6e90\u65e0\u6cd5\u91ca\u653e\u3002<\/p>\n\n\n\n<p>\u6574\u4e2a\u8fc7\u7a0b\u5c31\u76f8\u5f53\u4e8e\u4f60\u5728\u505a\u67d0\u4ef6\u4e8b\u60c5\uff0c\u4e2d\u9014\u4f60\u8ba9\u8001\u738b\u5e2e\u4f60\u529e\u4e00\u4e2a\u4efb\u52a1\uff08\u4f60\u529e\u7684\u65f6\u5019\u4ed6\u540c\u65f6\u529e\uff09\uff08\u521b\u5efa\u7ebf\u7a0b1\uff09\uff0c\u53c8\u53eb\u8001\u674e\u5e2e\u4f60\u529e\u4e00\u4ef6\u4efb\u52a1\uff08\u521b\u5efa\u7ebf\u7a0b2\uff09\uff0c\u73b0\u5728\u4f60\u7684\u8fd9\u90e8\u5206\u5de5\u4f5c\u505a\u5b8c\u4e86\uff0c\u9700\u8981\u7528\u5230\u4ed6\u4eec\u7684\u7ed3\u679c\uff0c\u53ea\u9700\u8981\u7b49\u5f85\u8001\u738b\u548c\u8001\u674e\u5904\u7406\u5b8c\uff08join()\uff0c\u963b\u585e\u4e3b\u7ebf\u7a0b\uff09\uff0c\u7b49\u4ed6\u4eec\u628a\u4efb\u52a1\u505a\u5b8c\uff08\u5b50\u7ebf\u7a0b\u8fd0\u884c\u7ed3\u675f\uff09\uff0c\u4f60\u53c8\u53ef\u4ee5\u5f00\u59cb\u4f60\u624b\u5934\u7684\u5de5\u4f5c\u4e86\uff08\u4e3b\u7ebf\u7a0b\u4e0d\u518d\u963b\u585e\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\n#include&lt;thread>\nusing namespace std;\nvoid proc(int a)\n{\n    cout &lt;&lt; \"\u6211\u662f\u5b50\u7ebf\u7a0b,\u4f20\u5165\u53c2\u6570\u4e3a\" &lt;&lt; a &lt;&lt; endl;\n    cout &lt;&lt; \"\u5b50\u7ebf\u7a0b\u4e2d\u663e\u793a\u5b50\u7ebf\u7a0bid\u4e3a\" &lt;&lt; this_thread::get_id()&lt;&lt; endl;\n}\nint main()\n{\n    cout &lt;&lt; \"\u6211\u662f\u4e3b\u7ebf\u7a0b\" &lt;&lt; endl;\n    int a = 9;\n    thread th2(proc,a);\/\/\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u51fd\u6570\u540d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8be5\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u5982\u679c\u8be5\u51fd\u6570\u63a5\u6536\u591a\u4e2a\u53c2\u6570\u5c31\u4f9d\u6b21\u5199\u5728\u540e\u9762\u3002\u6b64\u65f6\u7ebf\u7a0b\u5f00\u59cb\u6267\u884c\u3002\n    cout &lt;&lt; \"\u4e3b\u7ebf\u7a0b\u4e2d\u663e\u793a\u5b50\u7ebf\u7a0bid\u4e3a\" &lt;&lt; th2.get_id() &lt;&lt; endl;\n    th2.join()\uff1b\/\/\u6b64\u65f6\u4e3b\u7ebf\u7a0b\u88ab\u963b\u585e\u76f4\u81f3\u5b50\u7ebf\u7a0b\u6267\u884c\u7ed3\u675f\u3002\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e92\u65a5\u9501<\/h3>\n\n\n\n<p>\u8fd9\u6837\u6bd4\u55bb\uff0c\u5355\u4f4d\u4e0a\u6709\u4e00\u53f0\u6253\u5370\u673a\uff08\u5171\u4eab\u6570\u636ea\uff09\uff0c\u4f60\u8981\u7528\u6253\u5370\u673a\uff08\u7ebf\u7a0b1\u8981\u64cd\u4f5c\u6570\u636ea\uff09\uff0c\u540c\u4e8b\u8001\u738b\u4e5f\u8981\u7528\u6253\u5370\u673a(\u7ebf\u7a0b2\u4e5f\u8981\u64cd\u4f5c\u6570\u636ea)\uff0c\u4f46\u662f\u6253\u5370\u673a\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u7ed9\u4e00\u4e2a\u4eba\u7528\uff0c\u6b64\u65f6\uff0c\u89c4\u5b9a\u4e0d\u7ba1\u662f\u8c01\uff0c\u5728\u7528\u6253\u5370\u673a\u4e4b\u524d\u90fd\u8981\u5411\u9886\u5bfc\u7533\u8bf7\u8bb8\u53ef\u8bc1\uff08lock\uff09\uff0c\u7528\u5b8c\u540e\u518d\u5411\u9886\u5bfc\u5f52\u8fd8\u8bb8\u53ef\u8bc1(unlock)\uff0c\u8bb8\u53ef\u8bc1\u603b\u5171\u53ea\u6709\u4e00\u4e2a,\u6ca1\u6709\u8bb8\u53ef\u8bc1\u7684\u4eba\u5c31\u7b49\u7740\u5728\u7528\u6253\u5370\u673a\u7684\u540c\u4e8b\u7528\u5b8c\u540e\u624d\u80fd\u7533\u8bf7\u8bb8\u53ef\u8bc1(\u963b\u585e\uff0c\u7ebf\u7a0b1lock\u4e92\u65a5\u91cf\u540e\u5176\u4ed6\u7ebf\u7a0b\u5c31\u65e0\u6cd5lock,\u53ea\u80fd\u7b49\u7ebf\u7a0b1unlock\u540e\uff0c\u5176\u4ed6\u7ebf\u7a0b\u624d\u80fdlock)\uff0c\u90a3\u4e48\uff0c\u8fd9\u4e2a\u8bb8\u53ef\u8bc1\u5c31\u662f\u4e92\u65a5\u91cf\u3002\u4e92\u65a5\u91cf\u4fdd\u8bc1\u4e86\u4f7f\u7528\u6253\u5370\u673a\u8fd9\u4e00\u8fc7\u7a0b\u4e0d\u88ab\u6253\u65ad\u3002<\/p>\n\n\n\n<p>\u7a0b\u5e8f\u5b9e\u4f8b\u5316mutex\u5bf9\u8c61m,\u7ebf\u7a0b\u8c03\u7528\u6210\u5458\u51fd\u6570m.lock()\u4f1a\u53d1\u751f\u4e0b\u9762 3 \u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u5f53\u524d\u672a\u4e0a\u9501\uff0c\u5219\u8c03\u7528\u7ebf\u7a0b\u5c06\u8be5\u4e92\u65a5\u91cf\u9501\u4f4f\uff0c\u76f4\u5230\u8c03\u7528unlock()\u4e4b\u524d\uff0c\u8be5\u7ebf\u7a0b\u4e00\u76f4\u62e5\u6709\u8be5\u9501\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u5f53\u524d\u88ab\u9501\u4f4f\uff0c\u5219\u8c03\u7528\u7ebf\u7a0b\u88ab\u963b\u585e,\u76f4\u81f3\u8be5\u4e92\u65a5\u91cf\u88ab\u89e3\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream>\n#include&lt;thread>\n#include&lt;mutex>\nusing namespace std;\nmutex m;\/\/\u5b9e\u4f8b\u5316m\u5bf9\u8c61\uff0c\u4e0d\u8981\u7406\u89e3\u4e3a\u5b9a\u4e49\u53d8\u91cf\nvoid proc1(int a)\n{\n    m.lock();\n    cout &lt;&lt; \"proc1\u51fd\u6570\u6b63\u5728\u6539\u5199a\" &lt;&lt; endl;\n    cout &lt;&lt; \"\u539f\u59cba\u4e3a\" &lt;&lt; a &lt;&lt; endl;\n    cout &lt;&lt; \"\u73b0\u5728a\u4e3a\" &lt;&lt; a + 2 &lt;&lt; endl;\n    m.unlock();\n}\n\nvoid proc2(int a)\n{\n    m.lock();\n    cout &lt;&lt; \"proc2\u51fd\u6570\u6b63\u5728\u6539\u5199a\" &lt;&lt; endl;\n    cout &lt;&lt; \"\u539f\u59cba\u4e3a\" &lt;&lt; a &lt;&lt; endl;\n    cout &lt;&lt; \"\u73b0\u5728a\u4e3a\" &lt;&lt; a + 1 &lt;&lt; endl;\n    m.unlock();\n}\nint main()\n{\n    int a = 0;\n    thread proc1(proc1, a);\n    thread proc2(proc2, a);\n    proc1.join();\n    proc2.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u4e0d\u63a8\u8350\u5b9e\u76f4\u63a5\u53bb\u8c03\u7528\u6210\u5458\u51fd\u6570lock()\uff0c\u56e0\u4e3a\u5982\u679c\u5fd8\u8bb0unlock()\uff0c\u5c06\u5bfc\u81f4\u9501\u65e0\u6cd5\u91ca\u653e\uff0c\u4f7f\u7528lock_guard\u6216\u8005unique_lock\u80fd\u907f\u514d\u5fd8\u8bb0\u89e3\u9501\u8fd9\u79cd\u95ee\u9898\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">lock_guard()<\/h4>\n\n\n\n<p>\u58f0\u660e\u4e00\u4e2a\u5c40\u90e8\u7684lock_guard\u5bf9\u8c61\uff0c<strong>\u5728\u5176\u6784\u9020\u51fd\u6570\u4e2d\u8fdb\u884c\u52a0\u9501\uff0c\u5728\u5176\u6790\u6784\u51fd\u6570\u4e2d\u8fdb\u884c\u89e3\u9501\u3002\u6700\u7ec8\u7684\u7ed3\u679c\u5c31\u662f\uff1a\u521b\u5efa\u5373\u52a0\u9501\uff0c\u4f5c\u7528\u57df\u7ed3\u675f\u81ea\u52a8\u89e3\u9501\u3002<\/strong>\u4ece\u800c\u4f7f\u7528lock_guard()\u5c31\u53ef\u4ee5\u66ff\u4ee3lock()\u4e0eunlock()\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8bbe\u5b9a\u4f5c\u7528\u57df\uff0c\u4f7f\u5f97lock_guard\u5728\u5408\u9002\u7684\u5730\u65b9\u88ab\u6790\u6784\uff08\u5728\u4e92\u65a5\u91cf\u9501\u5b9a\u5230\u4e92\u65a5\u91cf\u89e3\u9501\u4e4b\u95f4\u7684\u4ee3\u7801\u53eb\u505a\u4e34\u754c\u533a\uff08\u9700\u8981\u4e92\u65a5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u90a3\u6bb5\u4ee3\u7801\u79f0\u4e3a\u4e34\u754c\u533a\uff09\uff0c\u4e34\u754c\u533a\u8303\u56f4\u5e94\u8be5\u5c3d\u53ef\u80fd\u7684\u5c0f\uff0c\u5373lock\u4e92\u65a5\u91cf\u540e\u5e94\u8be5\u5c3d\u65e9unlock\uff09\uff0c<strong>\u901a\u8fc7\u4f7f\u7528{}\u6765\u8c03\u6574\u4f5c\u7528\u57df\u8303\u56f4\uff0c\u53ef\u4f7f\u5f97\u4e92\u65a5\u91cfm\u5728\u5408\u9002\u7684\u5730\u65b9\u88ab\u89e3\u9501<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code class=\"language-C++ highlighter-hljs hljs language-C\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">proc2<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> a)<\/span>\n{\n    {\n        lock_guard&lt;mutex> <span class=\"hljs-title function_\">g2<\/span><span class=\"hljs-params\">(m)<\/span>;\n        <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"proc2\u51fd\u6570\u6b63\u5728\u6539\u5199a\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">endl<\/span>;\n        <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"\u539f\u59cba\u4e3a\"<\/span> &lt;&lt; a &lt;&lt; <span class=\"hljs-built_in\">endl<\/span>;\n        <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"\u73b0\u5728a\u4e3a\"<\/span> &lt;&lt; a + <span class=\"hljs-number\">1<\/span> &lt;&lt; <span class=\"hljs-built_in\">endl<\/span>;\n    }<span class=\"hljs-comment\">\/\/\u901a\u8fc7\u4f7f\u7528{}\u6765\u8c03\u6574\u4f5c\u7528\u57df\u8303\u56f4\uff0c\u53ef\u4f7f\u5f97m\u5728\u5408\u9002\u7684\u5730\u65b9\u88ab\u89e3\u9501<\/span>\n    <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"\u4f5c\u7528\u57df\u5916\u7684\u5185\u5bb93\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">endl<\/span>;\n    <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"\u4f5c\u7528\u57df\u5916\u7684\u5185\u5bb94\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">endl<\/span>;\n    <span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"\u4f5c\u7528\u57df\u5916\u7684\u5185\u5bb95\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">endl<\/span>;\n}<\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">unique_lock<\/h4>\n\n\n\n<p>unique_lock\u7c7b\u4f3c\u4e8elock_guard,\u53ea\u662funique_lock\u7528\u6cd5\u66f4\u52a0\u4e30\u5bcc\uff0c\u540c\u65f6\u652f\u6301lock_guard()\u7684\u539f\u6709\u529f\u80fd\u3002<br \/>\u4f7f\u7528lock_guard\u540e\u4e0d\u80fd\u624b\u52a8lock()\u4e0e\u624b\u52a8unlock();\u4f7f\u7528unique_lock\u540e\u53ef\u4ee5\u624b\u52a8lock()\u4e0e\u624b\u52a8unlock();<br \/>unique_lock\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u9664\u4e86\u53ef\u4ee5\u662fadopt_lock,\u8fd8\u53ef\u4ee5\u662ftry_to_lock\u4e0edefer_lock;<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><thead><tr><th> <\/th><th>lock_guard<\/th><th>unique_lock<\/th><\/tr><\/thead><tbody><tr><td>\u624b\u52a8lock\u4e0e\u624b\u52a8unlock<\/td><td>\u4e0d\u652f\u6301<\/td><td>\u652f\u6301<\/td><\/tr><tr><td>\u53c2\u6570<\/td><td>\u652f\u6301adopt_lock<\/td><td>\u652f\u6301adopt_lock\/try_to_lock\/defer_lock<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">condition_variable<\/h4>\n\n\n\n<p>\u9700\u8981#include&lt;condition_variable><br \/>wait(locker):\u5728\u7ebf\u7a0b\u88ab\u963b\u585e\u65f6\uff0c\u8be5\u51fd\u6570\u4f1a\u81ea\u52a8\u8c03\u7528 locker.unlock() \u91ca\u653e\u9501\uff0c\u4f7f\u5f97\u5176\u4ed6\u88ab\u963b\u585e\u5728\u9501\u7ade\u4e89\u4e0a\u7684\u7ebf\u7a0b\u5f97\u4ee5\u7ee7\u7eed\u6267\u884c\u3002\u53e6\u5916\uff0c\u4e00\u65e6\u5f53\u524d\u7ebf\u7a0b\u83b7\u5f97\u901a\u77e5(\u901a\u5e38\u662f\u53e6\u5916\u67d0\u4e2a\u7ebf\u7a0b\u8c03\u7528 notify_* \u5524\u9192\u4e86\u5f53\u524d\u7ebf\u7a0b)\uff0cwait() \u51fd\u6570\u6b64\u65f6\u518d\u81ea\u52a8\u8c03\u7528 locker.lock()\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">notify_all()<\/h4>\n\n\n\n<p>\u968f\u673a\u5524\u9192\u4e00\u4e2a\u7b49\u5f85\u7684\u7ebf\u7a0b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">notify_once()<\/h4>\n\n\n\n<p>\u5524\u9192\u6240\u6709\u7b49\u5f85\u7684\u7ebf\u7a0b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f02\u6b65\u7ebf\u7a0b<\/h3>\n\n\n\n<p>\u9700\u8981#include&lt;future><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">async\u4e0efuture<\/h4>\n\n\n\n<p>async\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u7528\u6765\u542f\u52a8\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2afuture\u7c7b\u6a21\u677f\u5bf9\u8c61\uff0cfuture\u5bf9\u8c61\u8d77\u5230\u4e86<strong>\u5360\u4f4d<\/strong>\u7684\u4f5c\u7528\uff0c\u521a\u5b9e\u4f8b\u5316\u7684future\u662f\u6ca1\u6709\u50a8\u5b58\u503c\u7684\uff0c\u4f46\u5728\u8c03\u7528future\u5bf9\u8c61\u7684get()\u6210\u5458\u51fd\u6570\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\u76f4\u5230\u5f02\u6b65\u7ebf\u7a0b\u6267\u884c\u7ed3\u675f\uff0c\u5e76\u628a\u8fd4\u56de\u7ed3\u679c\u4f20\u9012\u7ed9future\uff0c\u5373\u901a\u8fc7FutureObject.get()\u83b7\u53d6\u51fd\u6570\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u76f8\u5f53\u4e8e\u4f60\u53bb\u529e\u653f\u5e9c\u529e\u4e1a\u52a1\uff08\u4e3b\u7ebf\u7a0b\uff09\uff0c\u628a\u8d44\u6599\u4ea4\u7ed9\u4e86\u524d\u53f0\uff0c\u524d\u53f0\u5b89\u6392\u4e86\u4eba\u5458\u53bb\u7ed9\u4f60\u529e\u7406\uff08async\u521b\u5efa\u5b50\u7ebf\u7a0b\uff09\uff0c\u524d\u53f0\u7ed9\u4e86\u4f60\u4e00\u4e2a\u5355\u636e\uff08future\u5bf9\u8c61\uff09\uff0c\u8bf4\u4f60\u7684\u4e1a\u52a1\u6b63\u5728\u7ed9\u4f60\u529e\uff08\u5b50\u7ebf\u7a0b\u6b63\u5728\u8fd0\u884c\uff09\uff0c\u7b49\u6bb5\u65f6\u95f4\u4f60\u518d\u8fc7\u6765\u51ed\u8fd9\u4e2a\u5355\u636e\u53d6\u7ed3\u679c\u3002\u8fc7\u4e86\u6bb5\u65f6\u95f4\uff0c\u4f60\u53bb\u524d\u53f0\u53d6\u7ed3\u679c\uff0c\u4f46\u662f\u7ed3\u679c\u8fd8\u6ca1\u51fa\u6765\uff08\u5b50\u7ebf\u7a0b\u8fd8\u6ca1return\uff09\uff0c\u4f60\u5c31\u5728\u524d\u53f0\u7b49\u7740\uff08\u963b\u585e\uff09\uff0c\u76f4\u5230\u4f60\u62ff\u5230\u7ed3\u679c\uff08get()\uff09\u4f60\u624d\u79bb\u5f00\uff08\u4e0d\u518d\u963b\u585e\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;thread>\n#include &lt;mutex>\n#include&lt;future>\n#include&lt;Windows.h>\nusing namespace std;\ndouble t1(const double a, const double b)\n{\n\tdouble c = a + b;\n\tSleep(3000);\/\/\u5047\u8bbet1\u51fd\u6570\u662f\u4e2a\u590d\u6742\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff0c\u9700\u8981\u6d88\u80173\u79d2\n\treturn c;\n}\n\nint main()\n{\n\tdouble a = 2.3;\n\tdouble b = 6.7;\n\tfuture&lt;double> fu = async(t1, a, b);\/\/\u521b\u5efa\u5f02\u6b65\u7ebf\u7a0b\u7ebf\u7a0b\uff0c\u5e76\u5c06\u7ebf\u7a0b\u7684\u6267\u884c\u7ed3\u679c\u7528fu\u5360\u4f4d\uff1b\n\tcout &lt;&lt; \"\u6b63\u5728\u8fdb\u884c\u8ba1\u7b97\" &lt;&lt; endl;\n\tcout &lt;&lt; \"\u8ba1\u7b97\u7ed3\u679c\u9a6c\u4e0a\u5c31\u51c6\u5907\u597d\uff0c\u8bf7\u60a8\u8010\u5fc3\u7b49\u5f85\" &lt;&lt; endl;\n\tcout &lt;&lt; \"\u8ba1\u7b97\u7ed3\u679c\uff1a\" &lt;&lt; fu.get() &lt;&lt; endl;\/\/\u963b\u585e\u4e3b\u7ebf\u7a0b\uff0c\u76f4\u81f3\u5f02\u6b65\u7ebf\u7a0breturn\n        \/\/cout &lt;&lt; \"\u8ba1\u7b97\u7ed3\u679c\uff1a\" &lt;&lt; fu.get() &lt;&lt; endl;\/\/\u53d6\u6d88\u8be5\u8bed\u53e5\u6ce8\u91ca\u540e\u8fd0\u884c\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3afuture\u5bf9\u8c61\u7684get()\u65b9\u6cd5\u53ea\u80fd\u8c03\u7528\u4e00\u6b21\u3002\n\treturn 0;\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">shared_future<\/h4>\n\n\n\n<p>future\u4e0eshard_future\u7684\u7528\u9014\u90fd\u662f\u4e3a\u4e86<strong>\u5360\u4f4d<\/strong>\uff0c\u4f46\u662f\u4e24\u8005\u6709\u4e9b\u8bb8\u5dee\u522b\u3002<br \/>future\u7684get()\u6210\u5458\u51fd\u6570\u662f\u8f6c\u79fb\u6570\u636e\u6240\u6709\u6743;shared_future\u7684get()\u6210\u5458\u51fd\u6570\u662f\u590d\u5236\u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>future\u5bf9\u8c61\u7684get()\u53ea\u80fd\u8c03\u7528\u4e00\u6b21<\/strong>\uff1b\u65e0\u6cd5\u5b9e\u73b0\u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u540c\u4e00\u4e2a\u5f02\u6b65\u7ebf\u7a0b\uff0c\u4e00\u65e6\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u53d6\u4e86\u5f02\u6b65\u7ebf\u7a0b\u7684\u8fd4\u56de\u503c\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u65e0\u6cd5\u518d\u6b21\u83b7\u53d6\u3002<\/li>\n\n\n\n<li><strong>shared_future\u5bf9\u8c61\u7684get()\u53ef\u4ee5\u8c03\u7528\u591a\u6b21<\/strong>\uff1b\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u540c\u4e00\u4e2a\u5f02\u6b65\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u83b7\u53d6\u5f02\u6b65\u7ebf\u7a0b\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u539f\u5b50\u7c7b\u578bautomic<\/h2>\n\n\n\n<p>\u539f\u5b50\u64cd\u4f5c\u6307\u201c\u4e0d\u53ef\u5206\u5272\u7684\u64cd\u4f5c\u201d\uff1b\u4e5f\u5c31\u662f\u8bf4\u8fd9\u79cd\u64cd\u4f5c\u72b6\u6001\u8981\u4e48\u662f\u5b8c\u6210\u7684\uff0c\u8981\u4e48\u662f\u6ca1\u5b8c\u6210\u7684\u3002\u4e92\u65a5\u91cf\u7684\u52a0\u9501\u4e00\u822c\u662f\u9488\u5bf9\u4e00\u4e2a\u4ee3\u7801\u6bb5\uff0c\u800c\u539f\u5b50\u64cd\u4f5c\u9488\u5bf9\u7684\u4e00\u822c\u90fd\u662f\u4e00\u4e2a\u53d8\u91cf\u3002<br \/>automic\u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\uff0c\u4f7f\u7528\u8be5\u6a21\u677f\u7c7b\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4fdd\u8bc1\u539f\u5b50\u6027\u7684\u6210\u5458\u51fd\u6570\u6765\u5b9e\u73b0\u5171\u4eab\u6570\u636e\u7684\u5e38\u7528\u64cd\u4f5c\u3002\u539f\u5b50\u64cd\u4f5c\u7531CPU\u6307\u4ee4\u63d0\u4f9b\u652f\u6301\uff0c\u5b83\u7684\u6027\u80fd\u6bd4\u9501\u548c\u6d88\u606f\u4f20\u9012\u66f4\u9ad8\uff0c\u5e76\u4e14\uff0c\u4e0d\u9700\u8981\u7a0b\u5e8f\u5458\u5904\u7406\u52a0\u9501\u548c\u91ca\u653e\u9501\u7684\u95ee\u9898\uff0c\u652f\u6301\u4fee\u6539\u3001\u8bfb\u53d6\u3001\u4ea4\u6362\u3001\u6bd4\u8f83\u5e76\u4ea4\u6362\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>store\u662f\u539f\u5b50\u5199\u64cd\u4f5c\uff0cload\u662f\u539f\u5b50\u8bfb\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li>exchange\u662f\u4e8e\u4e24\u4e2a\u6570\u503c\u8fdb\u884c\u4ea4\u6362\u7684\u539f\u5b50\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5373\u4f7f\u4f7f\u7528\u4e86automic\uff0c\u4e5f\u8981\u6ce8\u610f\u6267\u884c\u7684\u64cd\u4f5c\u662f\u5426\u652f\u6301\u539f\u5b50\u6027<\/strong>\u3002\u4e00\u822catomic\u539f\u5b50\u64cd\u4f5c\uff0c\u9488\u5bf9++\uff0c\u2013\uff0c+=\uff0c-=\uff0c&amp;=\uff0c|=\uff0c^=\u662f\u652f\u6301\u7684\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u642c\u8fd0\u81eahttps:\/\/www.cnblogs.com\/zizbee\/p\/13520823.html \u6982\u5ff5 \u7ebf\u7a0b\uff1a\u662f\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u8fdb\u884c &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[22],"tags":[],"class_list":["post-1869","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/1869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=1869"}],"version-history":[{"count":0,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/1869\/revisions"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=1869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=1869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=1869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}