{"id":321300,"date":"2021-02-05T14:23:51","date_gmt":"2021-02-05T06:23:51","guid":{"rendered":"http:\/\/4563.org\/?p=321300"},"modified":"2021-02-05T14:23:51","modified_gmt":"2021-02-05T06:23:51","slug":"stdasync-%e7%9a%84%e4%bd%bf%e7%94%a8%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=321300","title":{"rendered":"std::async \u7684\u4f7f\u7528\u603b\u7ed3"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  std::async \u7684\u4f7f\u7528\u603b\u7ed3               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zoule <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><code>C++98<\/code>\u6807\u51c6\u4e2d\u5e76\u6ca1\u6709\u7ebf\u7a0b\u5e93\u7684\u5b58\u5728\uff0c\u76f4\u5230<code>C++11<\/code>\u4e2d\u624d\u7ec8\u4e8e\u63d0\u4f9b\u4e86\u591a\u7ebf\u7a0b\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u7ba1\u7406\u7ebf\u7a0b\u3001\u4fdd\u62a4\u5171\u4eab\u6570\u636e\u3001\u7ebf\u7a0b\u95f4\u540c\u6b65\u64cd\u4f5c\u3001\u539f\u5b50\u64cd\u4f5c\u7b49\u7c7b\u3002\u591a\u7ebf\u7a0b\u5e93\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u662f<code>#include &lt;thread&gt;<\/code>\uff0c\u7c7b\u540d\u4e3a<code>std::thread<\/code>\u3002<\/p>\n<p>\u7136\u800c\u7ebf\u7a0b\u6bd5\u7adf\u662f\u6bd4\u8f83\u8d34\u8fd1\u7cfb\u7edf\u7684\u4e1c\u897f\uff0c\u4f7f\u7528\u8d77\u6765\u4ecd\u7136\u4e0d\u662f\u5f88\u65b9\u4fbf\uff0c\u7279\u522b\u662f\u7ebf\u7a0b\u540c\u6b65\u53ca\u83b7\u53d6\u7ebf\u7a0b\u8fd0\u884c\u7ed3\u679c\u4e0a\u5c31\u66f4\u52a0\u9ebb\u70e6\u3002\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u7684\u901a\u8fc7<code>thread.join()<\/code>\u5f97\u5230\u7ed3\u679c\uff0c\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u5171\u4eab\u7684\u53d8\u91cf\u6765\u4f20\u9012\u7ed3\u679c\uff0c\u540c\u65f6\u8fd8\u8981\u8003\u8651\u7ebf\u7a0b\u95f4\u7684\u4e92\u65a5\u95ee\u9898\u3002\u597d\u5728<code>C++11<\/code>\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u7684\u5f02\u6b65\u63a5\u53e3<code>std::async<\/code>\uff0c\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\u53ef\u4ee5\u7b80\u5355\u7684\u521b\u5efa\u7ebf\u7a0b\u5e76\u901a\u8fc7<code>std::future<\/code>\u4e2d\u83b7\u53d6\u7ed3\u679c\u3002\u4ee5\u5f80\u90fd\u662f\u81ea\u5df1\u53bb\u5c01\u88c5\u7ebf\u7a0b\u5b9e\u73b0\u81ea\u5df1\u7684 async\uff0c\u73b0\u5728\u6709\u7ebf\u7a0b\u7684\u8de8\u5e73\u53f0\u63a5\u53e3\u53ef\u4ee5\u4f7f\u7528\u5c31\u6781\u5927\u7684\u65b9\u4fbf\u4e86 C++\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002<\/p>\n<p>\u5148\u770b\u4e00\u4e0b<code>std::async<\/code>\u7684\u51fd\u6570\u539f\u578b<\/p>\n<pre><code>\/\/(C++11 \u8d77) (C++17 \u524d) template&lt; class Function, class... Args&gt; std::future&lt;std::result_of_t&lt;std::decay_t&lt;Function&gt;(std::decay_t&lt;Args&gt;...)&gt;&gt;     async( Function&amp;&amp; f, Args&amp;&amp;... args );  \/\/(C++11 \u8d77) (C++17 \u524d) template&lt; class Function, class... Args &gt; std::future&lt;std::result_of_t&lt;std::decay_t&lt;Function&gt;(std::decay_t&lt;Args&gt;...)&gt;&gt;     async( std::launch policy, Function&amp;&amp; f, Args&amp;&amp;... args );    <\/code><\/pre>\n<p><strong>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7ebf\u7a0b\u7684\u521b\u5efa\u7b56\u7565\uff0c\u6709\u4e24\u79cd\u7b56\u7565\u53ef\u4f9b\u9009\u62e9<\/strong>\uff1a<\/p>\n<ul>\n<li><code>std::launch::async<\/code>\uff1a\u5728\u8c03\u7528 async \u5c31\u5f00\u59cb\u521b\u5efa\u7ebf\u7a0b\u3002<\/li>\n<li><code>std::launch::deferred<\/code>\uff1a\u5ef6\u8fdf\u52a0\u8f7d\u65b9\u5f0f\u521b\u5efa\u7ebf\u7a0b\u3002\u8c03\u7528 async \u65f6\u4e0d\u521b\u5efa\u7ebf\u7a0b\uff0c\u76f4\u5230\u8c03\u7528\u4e86 future \u7684 get \u6216\u8005 wait \u65f6\u624d\u521b\u5efa\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u9ed8\u8ba4\u7b56\u7565\u662f\uff1a<code>std::launch::async | std::launch::deferred<\/code>\u4e5f\u5c31\u662f\u4e24\u79cd\u7b56\u7565\u7684\u5408\u96c6\uff0c\u5177\u4f53\u4ec0\u4e48\u610f\u601d\u540e\u9762\u8be6\u7ec6\u518d\u8bf4<\/p>\n<p><strong>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7ebf\u7a0b\u51fd\u6570<\/strong><\/p>\n<p>\u7ebf\u7a0b\u51fd\u6570\u53ef\u63a5\u53d7<code>function, lambda expression, bind expression, or another function object<\/code><\/p>\n<p><strong>\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570<\/strong><\/p>\n<p>\u4e0d\u518d\u8bf4\u660e<\/p>\n<p><strong>\u8fd4\u56de\u503c std::future<\/strong><\/p>\n<p><code>std::future<\/code>\u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u8bbf\u95ee\u5f02\u6b65\u64cd\u4f5c\u7ed3\u679c\u7684\u673a\u5236\u3002\u4ece\u5b57\u9762\u610f\u601d\u4e0a\u770b\u5b83\u8868\u793a\u672a\u6765\uff0c\u8fd9\u4e2a\u610f\u601d\u5c31\u975e\u5e38\u8d34\u5207\uff0c\u56e0\u4e3a\u5979\u4e0d\u662f\u7acb\u5373\u83b7\u53d6\u7ed3\u679c\u4f46\u662f\u53ef\u4ee5\u5728\u67d0\u4e2a\u65f6\u5019\u4ee5\u540c\u6b65\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u7ed3\u679c\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2 future \u7684\u72b6\u6001\u6765\u83b7\u53d6\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u6784\u3002future_status \u6709\u4e09\u79cd\u72b6\u6001\uff1a<\/p>\n<ul>\n<li>deferred\uff1a\u5f02\u6b65\u64cd\u4f5c\u8fd8\u672a\u5f00\u59cb<\/li>\n<li>ready\uff1a\u5f02\u6b65\u64cd\u4f5c\u5df2\u7ecf\u5b8c\u6210<\/li>\n<li>timeout\uff1a\u5f02\u6b65\u64cd\u4f5c\u8d85\u65f6\uff0c\u4e3b\u8981\u7528\u4e8e std::future&lt;T&gt;.wait_for()<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code>\/\/\u67e5\u8be2 future \u7684\u72b6\u6001 std::future_status status; do {     status = future.wait_for(std::chrono::seconds(1));     if (status == std::future_status::deferred) {         std::cout &lt;&lt; \"deferred\" &lt;&lt; std::endl;     } else if (status == std::future_status::timeout) {         std::cout &lt;&lt; \"timeout\" &lt;&lt; std::endl;     } else if (status == std::future_status::ready) {         std::cout &lt;&lt; \"ready!\" &lt;&lt; std::endl;     } } while (status != std::future_status::ready);   <\/code><\/pre>\n<p><code>std::future<\/code>\u83b7\u53d6\u7ed3\u679c\u7684\u65b9\u5f0f\u6709\u4e09\u79cd\uff1a<\/p>\n<ul>\n<li>get\uff1a\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u7ed3\u675f\u5e76\u8fd4\u56de\u7ed3\u679c<\/li>\n<li>wait\uff1a\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u7ed3\u675f\uff0c\u4f46\u6ca1\u6709\u8fd4\u56de\u503c<\/li>\n<li>waite_for\uff1a\u8d85\u65f6\u7b49\u5f85\u8fd4\u56de\u7ed3\u679c\uff0c\u4e0a\u9762\u793a\u4f8b\u4e2d\u5c31\u662f\u5bf9\u8d85\u65f6\u7b49\u5f85\u7684\u4f7f\u7528\u5c55\u793a<\/li>\n<\/ul>\n<p><strong>\u4ecb\u7ecd\u5b8c\u4e86<code>std::async<\/code>\u7684\u51fd\u6570\u539f\u578b\uff0c\u90a3\u4e48\u5b83\u5230\u5e95\u8be5\u5982\u4f55\u4f7f\u7528\u5462\uff1f<\/strong><\/p>\n<p><code>std::async<\/code>\u7684\u57fa\u672c\u7528\u6cd5\uff1a\u793a\u4f8b\u94fe\u63a5<\/p>\n<pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;numeric&gt; #include &lt;future&gt; #include &lt;string&gt; #include &lt;mutex&gt;  std::mutex m; struct X {     void foo(int i, const std::string&amp; str) {         std::lock_guard&lt;std::mutex&gt; lk(m);         std::cout &lt;&lt; str &lt;&lt; ' ' &lt;&lt; i &lt;&lt; 'n';     }     void bar(const std::string&amp; str) {         std::lock_guard&lt;std::mutex&gt; lk(m);         std::cout &lt;&lt; str &lt;&lt; 'n';     }     int operator()(int i) {         std::lock_guard&lt;std::mutex&gt; lk(m);         std::cout &lt;&lt; i &lt;&lt; 'n';         return i + 10;     }};  template &lt;typename RandomIt&gt;int parallel_sum(RandomIt beg, RandomIt end){     auto len = end - beg;     if (len &lt; 1000)         return std::accumulate(beg, end, 0);      RandomIt mid = beg + len\/2;     auto handle = std::async(std::launch::async,                              parallel_sum&lt;RandomIt&gt;, mid, end);     int sum = parallel_sum(beg, mid);     return sum + handle.get(); }  int main(){     std::vector&lt;int&gt; v(10000, 1);     std::cout &lt;&lt; \"The sum is \" &lt;&lt; parallel_sum(v.begin(), v.end()) &lt;&lt; 'n';      X x;     \/\/ \u4ee5\u9ed8\u8ba4\u7b56\u7565\u8c03\u7528 x.foo(42, \"Hello\") \uff1a     \/\/ \u53ef\u80fd\u540c\u65f6\u6253\u5370 \"Hello 42\" \u6216\u5ef6\u8fdf\u6267\u884c     auto a1 = std::async(&amp;X::foo, &amp;x, 42, \"Hello\");     \/\/ \u4ee5 deferred \u7b56\u7565\u8c03\u7528 x.bar(\"world!\")     \/\/ \u8c03\u7528 a2.get() \u6216 a2.wait() \u65f6\u6253\u5370 \"world!\"     auto a2 = std::async(std::launch::deferred, &amp;X::bar, x, \"world!\");     \/\/ \u4ee5 async \u7b56\u7565\u8c03\u7528 X()(43) \uff1a     \/\/ \u540c\u65f6\u6253\u5370 \"43\"     auto a3 = std::async(std::launch::async, X(), 43);     a2.wait();                     \/\/ \u6253\u5370 \"world!\"     std::cout &lt;&lt; a3.get() &lt;&lt; 'n'; \/\/ \u6253\u5370 \"53\" } \/\/ \u82e5 a1 \u5728\u6b64\u70b9\u672a\u5b8c\u6210\uff0c\u5219 a1 \u7684\u6790\u6784\u51fd\u6570\u5728\u6b64\u6253\u5370 \"Hello 42\"  <\/code><\/pre>\n<p>\u53ef\u80fd\u7684\u7ed3\u679c<\/p>\n<pre><code>The sum is 10000 43 world! 53 Hello 42  <\/code><\/pre>\n<p>\u7531\u6b64\u53ef\u89c1\uff0c<code>std::async<\/code>\u662f\u5f02\u6b65\u64cd\u4f5c\u505a\u4e86\u4e00\u4e2a\u5f88\u597d\u7684\u5c01\u88c5\uff0c\u4f7f\u6211\u4eec\u4e0d\u7528\u5173\u6ce8\u7ebf\u7a0b\u521b\u5efa\u5185\u90e8\u7ec6\u8282\uff0c\u5c31\u80fd\u65b9\u4fbf\u7684\u83b7\u53d6\u5f02\u6b65\u6267\u884c\u72b6\u6001\u548c\u7ed3\u679c\uff0c\u8fd8\u53ef\u4ee5\u6307\u5b9a\u7ebf\u7a0b\u521b\u5efa\u7b56\u7565\u3002<\/p>\n<p><strong>\u6df1\u5165\u7406\u89e3\u7ebf\u7a0b\u521b\u5efa\u7b56\u7565<\/strong><\/p>\n<ul>\n<li>std::launch::async \u8c03\u5ea6\u7b56\u7565\u610f\u5473\u7740\u51fd\u6570\u5fc5\u987b\u5f02\u6b65\u6267\u884c\uff0c\u5373\u5728\u53e6\u4e00\u7ebf\u7a0b\u6267\u884c\u3002<\/li>\n<li>std::launch::deferred \u8c03\u5ea6\u7b56\u7565\u610f\u5473\u7740\u51fd\u6570\u53ef\u80fd\u53ea\u4f1a\u5728 std::async \u8fd4\u56de\u7684 future \u5bf9\u8c61\u8c03\u7528 get \u6216 wait \u65f6\u6267\u884c\u3002\u90a3\u5c31\u662f\uff0c\u6267\u884c\u4f1a\u63a8\u8fdf\u5230\u5176\u4e2d\u4e00\u4e2a\u8c03\u7528\u53d1\u751f\u3002\u5f53\u8c03\u7528 get \u6216 wait \u65f6\uff0c\u51fd\u6570\u4f1a\u540c\u6b65\u6267\u884c\uff0c\u5373\u8c03\u7528\u8005\u4f1a\u963b\u585e\u76f4\u5230\u51fd\u6570\u8fd0\u884c\u7ed3\u675f\u3002\u5982\u679c get \u6216 wait \u6ca1\u6709\u88ab\u8c03\u7528\uff0c\u51fd\u6570\u5c31\u7edd\u5bf9\u4e0d\u4f1a\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u4e24\u8005\u7b56\u7565\u90fd\u5f88\u660e\u786e\uff0c\u7136\u800c\u8be5\u51fd\u6570\u7684\u9ed8\u8ba4\u7b56\u7565\u5374\u5f88\u6709\u8da3\uff0c\u5b83\u4e0d\u662f\u4f60\u663e\u793a\u6307\u5b9a\u7684\uff0c\u4e5f\u5c31\u662f\u7b2c\u4e00\u4e2a\u51fd\u6570\u539f\u578b\u4e2d\u6240\u7528\u7684\u7b56\u7565\u5373<code>std::launch::async | std::launch::deferred<\/code>\uff0cc++\u6807\u51c6\u4e2d\u7ed9\u51fa\u7684\u8bf4\u660e\u662f\uff1a<\/p>\n<blockquote>\n<p>\u8fdb\u884c\u5f02\u6b65\u6267\u884c\u8fd8\u662f\u60f0\u6027\u6c42\u503c\u53d6\u51b3\u4e8e\u5b9e\u73b0<\/p>\n<\/blockquote>\n<pre><code>auto future = std::async(func);        \/\/ \u4f7f\u7528\u9ed8\u8ba4\u53d1\u5c04\u6a21\u5f0f\u6267\u884c func  <\/code><\/pre>\n<p>\u8fd9\u79cd\u8c03\u5ea6\u7b56\u7565\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u9884\u77e5\u51fd\u6570 func \u662f\u5426\u4f1a\u5728\u54ea\u4e2a\u7ebf\u7a0b\u6267\u884c\uff0c\u751a\u81f3\u65e0\u6cd5\u9884\u77e5\u4f1a\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u56e0\u4e3a func \u53ef\u80fd\u4f1a\u88ab\u8c03\u5ea6\u4e3a\u63a8\u8fdf\u6267\u884c\uff0c\u5373\u8c03\u7528 get \u6216 wait \u7684\u65f6\u5019\u6267\u884c\uff0c\u800c get \u6216 wait \u662f\u5426\u4f1a\u88ab\u6267\u884c\u6216\u8005\u5728\u54ea\u4e2a\u7ebf\u7a0b\u6267\u884c\u90fd\u65e0\u6cd5\u9884\u77e5\u3002<\/p>\n<p>\u540c\u65f6\u8fd9\u79cd\u8c03\u5ea6\u7b56\u7565\u7684\u7075\u6d3b\u6027\u8fd8\u4f1a\u6df7\u6dc6\u4f7f\u7528 thread_local \u53d8\u91cf\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c func \u5199\u6216\u8bfb\u8fd9\u79cd\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8(Thread Local Storage\uff0cTLS)\uff0c\u9884\u77e5\u53d6\u5230\u54ea\u4e2a\u7ebf\u7a0b\u7684\u672c\u5730\u53d8\u91cf\u662f\u4e0d\u53ef\u80fd\u7684\u3002<\/p>\n<p>\u5b83\u4e5f\u5f71\u54cd\u4e86\u57fa\u4e8e wait \u5faa\u73af\u4e2d\u7684\u8d85\u65f6\u60c5\u51b5\uff0c\u56e0\u4e3a\u8c03\u5ea6\u7b56\u7565\u53ef\u80fd\u4e3a<code>deferred<\/code>\u7684\uff0c\u8c03\u7528 wait_for \u6216\u8005 wait_until \u4f1a\u8fd4\u56de\u503c std::launch::deferred \u3002\u8fd9\u610f\u5473\u7740\u4e0b\u9762\u7684\u5faa\u73af\uff0c\u770b\u8d77\u6765\u6700\u7ec8\u4f1a\u505c\u6b62\uff0c\u4f46\u662f\uff0c\u5b9e\u9645\u4e0a\u53ef\u80fd\u4f1a\u4e00\u76f4\u8fd0\u884c\uff1a<\/p>\n<pre><code>void func()           \/\/ f \u7761\u7720 1 \u79d2\u540e\u8fd4\u56de {     std::this_thread::sleep_for(1); } auto future = std::async(func);      \/\/ \uff08\u6982\u5ff5\u4e0a\uff09\u5f02\u6b65\u6267\u884c f while(fut.wait_for(100ms) !=         \/\/ \u5faa\u73af\u76f4\u5230 f \u6267\u884c\u7ed3\u675f       std::future_status::ready)     \/\/ \u4f46\u8fd9\u53ef\u80fd\u6c38\u8fdc\u4e0d\u4f1a\u53d1\u751f {     ... }  <\/code><\/pre>\n<p>\u4e3a\u907f\u514d\u9677\u5165\u6b7b\u5faa\u73af\uff0c\u6211\u4eec\u5fc5\u987b\u68c0\u67e5 future \u662f\u5426\u628a\u4efb\u52a1\u63a8\u8fdf\uff0c\u7136\u800c future \u65e0\u6cd5\u83b7\u77e5\u4efb\u52a1\u662f\u5426\u88ab\u63a8\u8fdf\uff0c\u4e00\u4e2a\u597d\u7684\u6280\u5de7\u5c31\u662f\u901a\u8fc7 wait_for(0)\u6765\u83b7\u53d6 future_status \u662f\u5426\u662f deferred\uff1a<\/p>\n<pre><code>auto future = std::async(func);      \/\/ \uff08\u6982\u5ff5\u4e0a\uff09\u5f02\u6b65\u6267\u884c f if (fut.wait_for(0) == std::future_status::deferred)  \/\/ \u5982\u679c\u4efb\u52a1\u88ab\u63a8\u8fdf {     ...     \/\/ fut \u4f7f\u7528 get \u6216 wait \u6765\u540c\u6b65\u8c03\u7528 f } else {            \/\/ \u4efb\u52a1\u6ca1\u6709\u88ab\u63a8\u8fdf     while(fut.wait_for(100ms) != std::future_status::ready) { \/\/ \u4e0d\u53ef\u80fd\u65e0\u9650\u5faa\u73af       ...    \/\/ \u4efb\u52a1\u6ca1\u6709\u88ab\u63a8\u8fdf\u4e5f\u6ca1\u6709\u5c31\u7eea\uff0c\u6240\u4ee5\u505a\u4e00\u4e9b\u5e76\u53d1\u7684\u4e8b\u60c5\u76f4\u5230\u4efb\u52a1\u5c31\u7eea     }     ...        \/\/ fut \u5c31\u7eea }  <\/code><\/pre>\n<p>\u6709\u4eba\u53ef\u80fd\u4f1a\u8bf4\u65e2\u7136\u6709\u8fd9\u4e48\u591a\u7f3a\u70b9\u4e3a\u5565\u8fd8\u8981\u7528\u5b83\uff0c\u56e0\u4e3a\u6bd5\u7adf\u6211\u4eec\u8003\u8651\u7684\u6781\u9650\u60c5\u51b5\u4e0b\u7684\u53ef\u80fd\uff0c\u6709\u65f6\u5019\u6211\u4e0d\u8981\u6c42\u5b83\u662f\u5e76\u53d1\u8fd8\u662f\u540c\u6b65\u6267\u884c\uff0c\u4e5f\u4e0d\u9700\u8981\u8003\u8651\u4fee\u6539\u90a3\u4e2a\u7ebf\u7a0b thread_local \u53d8\u91cf\uff0c\u540c\u65f6\u4e5f\u80fd\u63a5\u53d7\u53ef\u80fd\u4efb\u52a1\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\uff0c\u90a3\u4e48\u8fd9\u79cd\u65b9\u5f0f\u5c31\u662f\u4e00\u79cd\u65b9\u4fbf\u4e14\u9ad8\u6548\u7684\u8c03\u5ea6\u7b56\u7565\u3002<\/p>\n<p><strong>\u7efc\u4e0a\u6240\u8ff0\uff0c\u6211\u4eec\u603b\u7ed3\u51fa\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>std::async \u7684\u9ed8\u8ba4\u8c03\u5ea6\u7b56\u7565\u65e2\u5141\u8bb8\u4efb\u52a1\u5f02\u6b65\u6267\u884c\uff0c\u53c8\u5141\u8bb8\u4efb\u52a1\u540c\u6b65\u6267\u884c\u3002<\/li>\n<li>\u9ed8\u8ba4\u7b56\u7565\u7075\u6d3b\u6027\u5bfc\u81f4\u4e86\u4f7f\u7528 thread_local \u53d8\u91cf\u65f6\u7684\u4e0d\u786e\u5b9a\u6027\uff0c\u5b83\u9690\u542b\u7740\u4efb\u52a1\u53ef\u80fd\u4e0d\u4f1a\u6267\u884c\uff0c\u5b83\u8fd8\u5f71\u54cd\u4e86\u57fa\u4e8e\u8d85\u65f6\u7684 wait \u8c03\u7528\u7684\u7a0b\u5e8f\u903b\u8f91\u3002<\/li>\n<li><strong>\u5982\u679c\u5f02\u6b65\u6267\u884c\u662f\u5fc5\u9700\u7684\uff0c\u6307\u5b9a std::launch::async \u53d1\u5c04\u7b56\u7565\u3002<\/strong><\/li>\n<\/ul>\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<\/p>\n<blockquote>\n<p><em>API Reference Document<\/em><\/p>\n<\/blockquote>\n<blockquote>\n<p><em>\u7528 C++11 \u7684 std::async \u4ee3\u66ff\u7ebf\u7a0b\u7684<\/em>\u521b\u5efa<\/p>\n<\/blockquote><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>std::async \u7684\u4f7f\u7528\u603b\u7ed3 &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\/321300"}],"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=321300"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/321300\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=321300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=321300"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=321300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}