{"id":458,"date":"2020-05-08T18:20:24","date_gmt":"2020-05-08T10:20:24","guid":{"rendered":"https:\/\/www.notown.club\/?p=458"},"modified":"2020-05-08T19:26:48","modified_gmt":"2020-05-08T11:26:48","slug":"c%e5%a4%9a%e7%ba%bf%e7%a8%8b%ef%bc%88%e7%ac%94%e8%ae%b0%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=458","title":{"rendered":"C++\u591a\u7ebf\u7a0b\uff08\u7b14\u8bb0\u4e00\uff09"},"content":{"rendered":"\n<p style=\"font-size:24px\"><strong>\u4e00\u3001\u4e00\u4e9b\u6982\u5ff5<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u5e76\u53d1\uff1a<\/strong><\/p>\n\n\n\n<p>\u6709\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u4efb\u52a1\uff08\u72ec\u7acb\u7684\u6d3b\u52a8\uff09\uff0c\u540c\u65f6\u53d1\u751f\uff08\u8fdb\u884c\uff09\uff0c\u6216\u8005\u8bf4<strong>\u4e00\u4e2a\u7a0b\u5e8f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u72ec\u7acb\u4efb\u52a1<\/strong>\u3002<br> \u5f53cpu\u7684\u6570\u91cf\u5c11\u4e8e\u4efb\u52a1\u6570\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u53ea\u662f\u901a\u8fc7\u8f6e\u6d41\u8c03\u5ea6\u6765\u5b9e\u73b0\u4e00\u79cd\u8868\u9762\u7684\u5e76\u53d1\u8fdb\u884c\u3002<br> \u4f7f\u7528\u5e76\u53d1\u7684\u539f\u56e0\uff1a\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u6709\u63d0\u9ad8\u6027\u80fd\u7684\u4f5c\u7528<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u8fdb\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<p>\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u6b63\u5728\u6267\u884c\u7684\u53ef\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u7ebf\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<p>\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u4ee3\u7801\u7684<strong>\u6267\u884c\u901a\u8def<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u5199\u4ee3\u7801\u6765\u521b\u5efa\u5176\u5b83\u7ebf\u7a0b\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff0c\u5728\u540c\u4e00\u4e2a\u65f6\u95f4\uff0c\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u6267\u884c\u591a\u4e2a\u4efb\u52a1<br> \u7ebf\u7a0b\u5e76\u975e\u8d8a\u591a\u8d8a\u597d\uff0c\u7ebf\u7a0b\u5207\u6362\u4e5f\u662f\u9700\u8981\u65f6\u95f4\u7684\uff0c\u9700\u8981\u4fdd\u5b58\u6062\u590d\u5404\u79cd\u5c40\u90e8\u53d8\u91cf\u3002\uff08\u4e00\u822c200~300\u4e2a\u4e3a\u4f73\uff09<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u591a\u8fdb\u7a0b\u5e76\u53d1\u4e0e\u591a\u7ebf\u7a0b\u5e76\u53d1<\/strong><\/p>\n\n\n\n<p><strong>\u591a\u8fdb\u7a0b\u5b9e\u73b0<\/strong>\u5e76\u53d1\u76f8\u6bd4\u4e8e\u591a\u7ebf\u7a0b\u5b9e\u73b0\u5e76\u53d1\u800c\u8a00\uff0c\u5b83\u8fd8\u9700\u8981<strong>\u5207\u6362\u865a\u62df\u5730\u5740\u7a7a\u95f4<\/strong>\uff0c\u5f00\u9500\u66f4\u5927\u3002<br><strong>\u591a\u8fdb\u7a0b\u4e4b\u95f4<\/strong>\u901a\u4fe1\u9700\u8981\u501f\u52a9\u7ba1\u9053\u3001\u6587\u4ef6\u3001\u6d88\u606f\u961f\u5217\u3001\u5171\u4eab\u5185\u5b58\u3001socket\u7b49\u6280\u672f\u624b\u6bb5<br><strong>\u591a\u7ebf\u7a0b\u4e4b\u95f4<\/strong>\u4ed6\u4eec\u662f\u5171\u4eab\u4e00\u4e2a\u865a\u62df\u5730\u5740\u7a7a\u95f4\u7684\uff0c\u5373\u53ef\u4ee5<strong>\u5171\u4eab\u5185\u5b58<\/strong>\uff0c\u7ebf\u7a0b\u95f4\u5207\u6362\u5f00\u9500\u66f4\u5c0f\uff0c\u4f46\u9700\u8981\u89e3\u51b3\u7684\u4e00\u4e2a\u91cd\u8981\u95ee\u9898\u5c31\u662f\uff1a<strong>\u8981\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027<\/strong><\/p>\n\n\n\n<p>\u4ee5\u5f80\u5728\u4e0d\u540c\u7684\u5f00\u53d1\u5e73\u53f0\u4e0a\u8fdb\u884c\u591a\u7ebf\u7a0b\u5f00\u53d1\u90fd\u6709\u4e0d\u540c\u7684\u5e93\u3001\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u56e0\u6b64\u7f16\u5199\u51fa\u6765\u7684\u7a0b\u5e8f\u4e00\u822c\u4e0d\u80fd\u8de8\u5e73\u53f0\u3002\u4ecec++11\u65b0\u6807\u51c6\u5f00\u59cb\uff0cc++\u8bed\u8a00\u672c\u8eab\u589e\u52a0\u4e86\u5bf9\u591a\u7ebf\u7a0b\u7684\u652f\u6301\uff0c\u4f7f\u5f97\u7f16\u5199\u7684\u7a0b\u5e8f\u6709\u4e86\u53ef\u79fb\u690d\u6027<\/p>\n\n\n\n<p style=\"font-size:24px\"><strong>\u4e8c\u3001\u521b\u5efa\u7ebf\u7a0b\u7684\u591a\u79cd\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>thread<\/strong><br>\uff08 \u4e00\u5b9a\u4f1a\u521b\u5efa\u7ebf\u7a0b\uff0c\u5982\u679c\u8d44\u6e90\u592a\u7d27\u5f20\u5bfc\u81f4\u521b\u5efa\u4e0d\u4e86\uff0c\u7a0b\u5e8f\u5c31\u4f1a\u5d29\u6e83 \uff09<\/li><li><strong>async<\/strong><br> \uff08\u4e5f\u53eb\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\uff0c\u4f46\u5f53\u53c2\u6570\u88ab\u6307\u5b9a\u4e3alaunch::deferred\u65f6\uff0c\u5c31\u4e0d\u4f1a\u521b\u5efa\u7ebf\u7a0b\uff09\uff08\u540c\u65f6\u76f8\u6bd4thread\uff0c\u5b83\u4e00\u822c\u7528\u4e8e\u9700\u8981\u8fd4\u56de\u503c\u7684\u7ebf\u7a0b\uff09 <br><\/li><\/ol>\n\n\n\n<p class=\"has-medium-font-size\"><strong>A.thread\u521b\u5efa\u7ebf\u7a0b<\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"> <strong>\u2460 \u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u7136\u540e\u628a\u8be5\u51fd\u6570\u4f5c\u4e3a\u7ebf\u7a0b\u7684\u5165\u53e3\u5730\u5740<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void readTheval(int num)\n{\n    cout &lt;&lt; \"\u6211\u662f\u8bfb\u7ebf\u7a0b\" &lt;&lt; this_thread::get_id() ;\n    cout &lt;&lt; \"  \u6211\u8bfb\u5230\u7684\u6570\u636e\u662f:\" &lt;&lt; num &lt;&lt; endl;\n    return;\n}\n\/\/main\u51fd\u6570\u91cc\u9762\uff1a\n    thread th1(readTheval, 5);\/\/\u521b\u5efa\u7ebf\u7a0b\u5e76\u5f00\u59cb\u6267\u884c\n    th1.join();<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u2461 \u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5e76\u7f16\u5199\u5706\u62ec\u53f7\u91cd\u8f7d\u51fd\u6570\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u8be5\u7c7b\u7684\u5bf9\u8c61\uff0c\u628a\u8be5\u5bf9\u8c61\u4f5c\u4e3a\u7ebf\u7a0b\u5165\u53e3\u5730\u5740<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class CircleReLoad\n{\npublic:\n    void operator()()\n    {\n        cout &lt;&lt; \"\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\" &lt;&lt; this_thread::get_id() &lt;&lt; endl;\n        \/* ...  *\/\n        cout &lt;&lt; \"\u7ebf\u7a0b\u6267\u884c\u7ed3\u675f\uff01\" &lt;&lt; endl;\n    }\n};\n\/\/main\u51fd\u6570\u91cc\u7684\uff1a\n    CircleReLoad Acase;\n    thread th1(Acase);\n    th1.join();<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u2462 lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u7ebf\u7a0b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/main\u51fd\u6570\u91cc\n    auto OneThread = [] {\n        cout &lt;&lt; \"lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u7ebf\u7a0b\" &lt;&lt; endl;\n        \/* \u2026 *\/\n        cout &lt;&lt; this_thread::get_id() &lt;&lt; \"\u7ebf\u7a0b\u6267\u884c\u7ed3\u675f \" &lt;&lt; endl;\n    };\n    thread th1(OneThread);\n    th1.join();<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u2463\u628a\u67d0\u4e2a\u7c7b\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\u4f5c\u4e3a\u7ebf\u7a0b\u7684\u5165\u53e3\u5730\u5740<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Data_\n{\npublic:\n    void GetMsg(){}\n    void SaveMsh(){}\n};\n\/\/main\u51fd\u6570\u91cc\n    Data_ s;\n    thread oneobj(&amp;Data_::SaveMsh,&amp;s);\n    thread twoobj(&amp;Data_::GetMsg,&amp;s);\n    oneobj.join();\n    twoobj.join();<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u591a\u4e2a\u7ebf\u7a0b\u7684\u521b\u5efa\u3001\u7ba1\u7406\uff1a<\/strong><\/p>\n\n\n\n<p>\u53ef\u4ee5\u7528vector\u7b49\u5bb9\u5668\u6765\u5b58\u653e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void TextThread()\n{\n     cout &lt;&lt; \"\u6211\u662f\u7ebf\u7a0b\" &lt;&lt; this_thread::get_id() &lt;&lt; endl;\n     \/*  \u2026  *\/\n     cout &lt;&lt; \"\u7ebf\u7a0b\" &lt;&lt; this_thread::get_id() &lt;&lt; \"\u6267\u884c\u7ed3\u675f\" &lt;&lt; endl; \n}\n \/\/main\u51fd\u6570\u91cc     vector threadagg;\n     for (int i = 0; i &lt; 10; ++i)\n     {\n         threadagg.push_back(thread(TextThread));\n     }\n     for (int i = 0; i &lt; 10; ++i)\n     {\n         threadagg[i].join();\n     }<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>join(), detach(), joinable()\u6210\u5458\u51fd\u6570<\/strong><\/p>\n\n\n\n<p><strong>join()\uff1a<\/strong><br>\u52a0\u5165\/\u6c47\u5408\uff0c\u8d77\u5230\u4e86\u8ba9<strong>\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5<\/strong>\u7684\u4f5c\u7528\uff0c\u8ba9\u5b50\u7ebf\u7a0b\u6c47\u5408\u5230\u4e3b\u7ebf\u7a0b\u3002<br> (\u4e00\u822c\u6765\u8bb2\uff0c\u4e3b\u7ebf\u7a0b\u90fd\u662f\u6700\u540e\u7ed3\u675f\u7684\uff0c\u4fdd\u8bc1\u6240\u6709\u5b50\u7ebf\u7a0b\u90fd\u80fd\u6b63\u5e38\u7684\u6267\u884c\u5b8c\u6bd5)<\/p>\n\n\n\n<p><strong>detach():<\/strong><br> \u5206\u79bb\uff0c\u5c06\u5b50\u7ebf\u7a0b\u548c\u4e3b\u7ebf\u7a0b\u5206\u79bb\u5f00\u6765\uff0c\u5373\u5b50\u7ebf\u7a0b\u5c31<strong>\u4e0d\u548c\u4e3b\u7ebf\u7a0b\u6c47\u5408<\/strong>\u4e86\uff0c\u4e3b\u7ebf\u7a0b\u4e5f\u4e0d\u5fc5\u7b49\u5f85\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5<br> \u4e00\u65e6detach\uff0c\u4e0e\u4e3b\u7ebf\u7a0b\u91cc\u7684thread\u5bf9\u8c61\u5c31\u4f1a\u5931\u53bb\u5bf9\u8fd9\u4e2a\u5b50\u7ebf\u7a0b\u7684\u63a7\u5236\uff0c\u8fd9\u4e2a\u5b50\u7ebf\u7a0b\u5c31\u4f1a\u9a7b\u7559\u5728\u540e\u53f0\u8fd0\u884c\uff08\u5f53\u6b64\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u540e\uff0c\u7531\u7cfb\u7edf\u81ea\u5df1\u6765\u6e05\u7406\u8be5\u7ebf\u7a0b\u7684\u8d44\u6e90\uff09<br> <strong>\uff08\u6ce8\u610f\u5982\u679c\u7528detach\uff0c\u5e76\u4e14\u8be5\u7ebf\u7a0b\u4f7f\u7528\u5230\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u91cc\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u6bd4\u5982\u8bf4\u5b50\u7ebf\u7a0b\u4e2d\u6709\u5f15\u7528\u6216\u6307\u9488\u4f7f\u6307\u5411\u4e3b\u7ebf\u7a0b\u91cc\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u5c31\u4f1a\u4f7f\u7a0b\u5e8f\u5d29\u6e83\uff09<br> \uff08\u5f53\u4f20\u5165\u53c2\u6570\u5b58\u5728\u9690\u5f0f\u8f6c\u6362\u7684\u65f6\u5019\uff0c\u4e5f\u6709\u53ef\u80fd\u51fa\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u5373\u4e3b\u7ebf\u7a0b\u5df2\u7ecf\u7ed3\u675f\uff0c\u4f20\u5165\u53c2\u6570\u7684\u7684\u8fd9\u4e2a\u53d8\u91cf\u5df2\u7ecf\u88ab\u56de\u6536\u4e86\uff0c\u5b83\u624d\u5f00\u59cb\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u5e76\u6267\u884c\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u6837\u5c31\u4f1a\u5f15\u53d1\u9519\u8bef\uff09<\/strong><br><strong> \uff08\u4e00\u53e5\u8bdd\u80fd\u7528join\u5c31\u4e0d\u8981detach\uff09<\/strong><\/p>\n\n\n\n<p><strong>joinable()<\/strong><br> \u53ef\u4ee5\u5224\u65ad\u67d0thread\u5bf9\u8c61\u662f\u5426\u8fd8\u53ef\u4ee5join\u6216detach\uff0c\u53ef\u4ee5\u5c31\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>B.async\u521b\u5efa\u7ebf\u7a0b<\/strong><\/p>\n\n\n\n<p><strong>async<\/strong>\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u7528\u6765\u542f\u52a8\u5f02\u6b65\u4efb\u52a1\uff0c\u5e76\u8fd4\u56de<strong>future<\/strong>\u5bf9\u8c61\uff0cfuture\u662f\u4e00\u4e2a\u7c7b\u6a21\u677f<br>future\u5bf9\u8c61\u91cc\u8fb9\u5305\u542b\u7ebf\u7a0b\u5165\u53e3\u51fd\u6570\u6240\u8fd4\u56de\u7684\u7ed3\u679c\uff08\u7ebf\u7a0b\u8fd4\u56de\u7ed3\u679c\uff09\uff0c\u540e\u7eed\u53ef\u901a\u8fc7get()\u6210\u5458\u51fd\u6570\u5f97\u5230\u8fd4\u56de\u503c\u3002<br>\uff08\u5f53\u4f60\u5e0c\u671b\u591a\u6b21\u8c03\u7528future\u7684\u6210\u5458\u51fd\u6570get\u65f6\uff0c\u53ef\u6539\u7528<strong>shared_future<\/strong>\u7c7b\u6a21\u677f\uff0c\u5f53\u7136\u8fd9\u53ea\u662f\u5c06\u7ed3\u679c\u5b58\u50a8\u8d77\u6765\uff0c\u591a\u6b21\u8c03\u7528\u7684get\u7684\u503c\u662f\u4e00\u6837\u7684\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int TextThread(int num)\n{\n    cout &lt;&lt; \"\u6211\u662f\u7ebf\u7a0b\" &lt;&lt; this_thread::get_id() &lt;&lt; endl;\n    \/*  \u2026  *\/\n    cout &lt;&lt; \"\u7ebf\u7a0b\" &lt;&lt; this_thread::get_id() &lt;&lt; \"\u6267\u884c\u7ed3\u675f\" &lt;&lt; endl;\n    return num;\n}\n\/\/main\u51fd\u6570\u91cc\n    future result = async(launch::async,TextThread,6);\/\/\n    cout &lt;&lt; result.get();<\/pre>\n\n\n\n<p>\u5176\u4e2d\uff1a<br>\u8981\u662flaunch::async\u7701\u7565\u7684\u8bdd\uff0c\u7cfb\u7edf\u4f1a\u628a\u5b83\u5f53\u6210launch::async | launch::deferred \uff0c\u5373\u7cfb\u7edf<strong>\u4f1a\u6839\u636e\u5f53\u524d\u8d44\u6e90\u72b6\u51b5\u81ea\u7531\u8c03\u5ea6<\/strong>\uff0c\u5982\u679c\u8d44\u6e90\u4e0d\u7d27\u5f20\u4e00\u822c\u4f1a\u5f53\u6210launch::async\uff0c\u5373\u4f1a\u7acb\u523b\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u5e76\u5f00\u59cb\u6267\u884c\uff0c\u53cd\u4e4b\uff0c\u5219\u4f1a\u5f53\u6210\u662flaunch::deferred\uff0c\u4f1a\u7b49\u5230\u4f60\u8c03\u7528\u6210\u5458\u51fd\u6570get\u6216wait\u7684\u65f6\u5019\u624d\u7531\u4e3b\u7ebf\u7a0b\u53bb\u5b8c\u6210\uff08\u7c7b\u4f3c\u4e8e\u8c03\u7528\u666e\u901a\u51fd\u6570\uff09\uff0c\u5f53\u7701\u7565\u8be5\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u7528future_status\u5224\u65ad\u7cfb\u7edf\u6700\u7ec8\u6709\u65e0\u521b\u5efa\u7ebf\u7a0b\uff09<\/p>\n\n\n\n<p><strong>future_status<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int GetAVaule(int num)\n{\n    cout &lt;&lt; \"\u6211\u6b63\u5728\u6267\u884c\u554a\uff01\" &lt;&lt; endl;\n    cout &lt;&lt; \"\u6211\u7684ID\u662f:\" &lt;&lt; this_thread::get_id() &lt;&lt; endl;\n    Sleep(3000);\n    return num;\n}\n\/\/main\u51fd\u6570\u91cc\n    shared_future result = async(GetAVaule, 55);\n    future_status status = result.wait_for(chrono::seconds(4));\/\/\u7b494\u79d2\u770b\u770b\u80fd\u4e0d\u80fd\u6267\u884c\u5b8c(wait_for\u62ec\u53f7\u91cc\u53ef\u76f4\u63a5\u51994s)\n    if (status == future_status::timeout)\n    {\n        cout &lt;&lt; \"\u4efb\u52a1\u8d85\u65f6\uff0c\u8fd8\u6ca1\u6267\u884c\u73a9\u5462\" &lt;&lt; endl;\n    }\n    else if (status == future_status::ready)\n    {\n        cout &lt;&lt; \"\u7ed9\u7684\u65f6\u95f4\u591f\u7528\uff0c\u6211\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\" &lt;&lt; endl;\n        cout &lt;&lt; result.get() &lt;&lt; endl;\n    }\n    else if (status == future_status::deferred)\n    {\n        cout &lt;&lt; \"\u5982\u679c\u8fc7\u4f60\u662f\u5ef6\u8fdf\u6267\u884c\u7684\u53c2\u6570\uff0c\u90a3\u5c31\u5230\u8fd9\u6765\" &lt;&lt; endl;\n    }<\/code><\/pre>\n\n\n\n<p><strong>packaged_task<\/strong><\/p>\n\n\n\n<p>\u6253\u5305\u4efb\u52a1\uff0c\u628a\u4efb\u52a1\u5305\u88c5\u8d77\u6765\uff0c\u662f\u4e00\u4e2a\u7c7b\u6a21\u677f\uff0c\u662f\u4e00\u4e2a\u7528\u6765\u5305\u88c5\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53ef\u8c03\u7528\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ TextThread\u4e3a\u4e0a\u9762\u90a3\u4e2a\u51fd\u6570\n    packaged_task obj(TextThread);\/\/\u5305\u88c5\n    thread th1(ref(obj),6);\/\/\u5f00\u59cb\u6267\u884c\u7ebf\u7a0b\n    th1.join();\n    future result = obj.get_future();\n    cout &lt;&lt; result.get() &lt;&lt; endl;<\/code><\/pre>\n\n\n\n<p><strong>promise<\/strong><\/p>\n\n\n\n<p>\u4e00\u4e2a\u7c7b\u6a21\u677f\uff0c\u80fd\u591f\u5728\u67d0\u4e2a\u7ebf\u7a0b\u4e2d\u7ed9\u5b83\u8d4b\u503c\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u5176\u5b83\u7ebf\u7a0b\u4e2d\uff0c\u628a\u8fd9\u4e2a\u503c\u53d6\u51fa\u6765\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void MyThread(promise&amp; pro, int num)\n{\n    num *= 2;\n    pro.set_value(num);\n    return;\n}\n\/\/main\u51fd\u6570\u91cc\n    promise my_prom;\n    thread th1(MyThread, ref(my_prom), 66);\n    th1.join();\n    future result = my_prom.get_future();\/\/future\u548cpromise\u7ed1\u5b9a\uff0c\u7528\u4e8e\u83b7\u53d6\u7ebf\u7a0b\u8fd4\u56de\u503c\n    cout &lt;&lt; result.get() &lt;&lt; endl;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4e00\u4e9b\u6982\u5ff5 \u5e76\u53d1\uff1a \u6709\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u4efb\u52a1\uff08\u72ec\u7acb\u7684\u6d3b\u52a8\uff09\uff0c\u540c\u65f6\u53d1\u751f\uff08\u8fdb\u884c\uff09\uff0c\u6216\u8005\u8bf4\u4e00\u4e2a\u7a0b\u5e8f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u72ec\u7acb\u4efb&hellip;<a href=\"https:\/\/www.notown.top\/?p=458\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++\u591a\u7ebf\u7a0b\uff08\u7b14\u8bb0\u4e00\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14],"tags":[],"class_list":["post-458","post","type-post","status-publish","format-standard","hentry","category-14"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=458"}],"version-history":[{"count":5,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/458\/revisions"}],"predecessor-version":[{"id":474,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/458\/revisions\/474"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}