{"id":10,"date":"2023-12-21T19:08:41","date_gmt":"2023-12-21T11:08:41","guid":{"rendered":"http:\/\/www.lemi7.com\/?p=10"},"modified":"2023-12-21T19:09:35","modified_gmt":"2023-12-21T11:09:35","slug":"c-11-promise","status":"publish","type":"post","link":"http:\/\/www.lemi7.com\/index.php\/2023\/12\/21\/c-11-promise\/","title":{"rendered":"c++ 11 promise"},"content":{"rendered":"\n<p>\u76ee\u5f55<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            \u4e00 promise\n            \u4e8c \u5b9a\u4e49\n            \u4e09 \u6210\u5458\u51fd\u6570\n            \u56db get_future\n            \u4e94 \u8bbe\u7f6e\u7ed3\u679c\n                1. set_value\n                2. set_value_at_thread_exit\n                3. set_exception\n                4. set_exception_at_thread_exit\n            \u516d \u53c2\u8003<\/code><\/pre>\n\n\n\n<p>\u4e00 promise<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u524d\u6587 C++11 async \u53ef\u77e5\uff0c\u5f02\u6b65\u64cd\u4f5c\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u662f\u901a\u8fc7 std::async \u63a5\u53e3\u8c03\u7528\u53ef\u6267\u884c\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7 std::future \u83b7\u5f97\u7ed3\u679c\u3002\n\u53e6\u4e00\u79cd\u5e38\u89c1\u65b9\u5f0f\u662f\u5728\u6307\u5b9a\u7684 std::thread \u4e2d\u6267\u884c\uff0c\u5982\u4f55\u83b7\u53d6\u5728 std::thread \u4e2d\u7684\u6267\u884c\u7ed3\u679c\uff1f\u529e\u6cd5\u4e4b\u4e00\u5c31\u662f\u901a\u8fc7 std::promise\u3002<\/code><\/pre>\n\n\n\n<p>\u4e8c \u5b9a\u4e49<\/p>\n\n\n\n<p>\/\/ \u5934\u6587\u4ef6<br>template&lt; class R &gt; class promise; (1)(C++11 \u8d77) \/\/ \u7a7a\u6a21\u677f<br>template&lt; class R &gt; class promise; (2)(C++11 \u8d77) \/\/ \u975e void \u7279\u5316\uff0c\u7528\u4e8e\u5728\u7ebf\u7a0b\u95f4\u4ea4\u6d41\u5bf9\u8c61<br>template&lt;&gt; class promise; (3)(C++11 \u8d77) \/\/ void \u7279\u5316\uff0c\u7528\u4e8e\u4ea4\u6d41\u65e0\u72b6\u6001\u4e8b\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n\n\u7c7b\u6a21\u677f std::promise \u63d0\u4f9b\u5b58\u50a8\u503c\u6216\u5f02\u5e38\u7684\u8bbe\u65bd\u3002\n\npromise \u5bf9\u8c61\u4e0e\u5176 get_future() \u63a5\u53e3\u8fd4\u56de\u7684 future \u5bf9\u8c61\u6784\u6210 promise-future \u4ea4\u6d41\u901a\u9053\u3002\u901a\u9053\u7684 promise\u5bf9\u8c61\u4e0e future \u5bf9\u8c61\u5173\u8054\u540c\u4e00\u5171\u4eab\u72b6\u6001(shared state)\uff0c promise \u5bf9\u8c61\u662f\u901a\u9053\u7684 push \u7aef\uff0cfuture \u5bf9\u8c61\u662f\u901a\u9053\u7684 pop \u7aef\u3002promise \u5b58\u50a8\u503c\u6216\u5f02\u5e38\u5230\u5171\u4eab\u72b6\u6001\u4e2d\uff0c\u4f7f\u5171\u4eab\u72b6\u6001\u5c31\u7eea\uff0cfuture \u901a\u8fc7get()\u5f02\u6b65\u83b7\u5f97\u7ed3\u679c\u3002\n\npromise \u5bf9\u5171\u4eab\u72b6\u6001\u7684\u4e09\u79cd\u5904\u7406\n\n    \u4f7f\u5c31\u7eea\uff1a promise \u5b58\u50a8\u7ed3\u679c\u6216\u5f02\u5e38\u4e8e\u5171\u4eab\u72b6\u6001\u3002\u6807\u8bb0\u5171\u4eab\u72b6\u6001\u4e3a\u5c31\u7eea\uff0c\u5e76\u89e3\u9664\u963b\u585e\u4efb\u4f55\u7b49\u5f85\u4e8e\u4e0e\u8be5\u5171\u4eab\u72b6\u6001\u5173\u8054\u7684 future \u4e0a\u7684\u7ebf\u7a0b\u3002\n    \u91ca\u653e\uff1a promise \u653e\u5f03\u5176\u5bf9\u5171\u4eab\u72b6\u6001\u7684\u5f15\u7528\u3002\u82e5\u8fd9\u662f\u6700\u540e\u4e00\u4e2a\u8fd9\u79cd\u5f15\u7528\uff0c\u5219\u9500\u6bc1\u5171\u4eab\u72b6\u6001\u3002\u9664\u975e\u8fd9\u662f std::async \u6240\u521b\u5efa\u7684\u672a\u5c31\u7eea\u7684\u5171\u4eab\u72b6\u6001\uff0c\u5426\u5219\u6b64\u64cd\u4f5c\u4e0d\u963b\u585e\u3002\n    \u629b\u5f03\uff1a promise \u5b58\u50a8\u4ee5 std::future_errc::broken_promise \u4e3a error_code \u7684 std::future_error \u7c7b\u578b\u5f02\u5e38\uff0c\u4ee4\u5171\u4eab\u72b6\u6001\u4e3a\u5c31\u7eea\uff0c\u7136\u540e\u91ca\u653e\u5b83\u3002\n\nstd::promise \u53ea\u5e94\u5f53\u4f7f\u7528\u4e00\u6b21\u3002\n\n    Note that the std::promise object is meant to be used only once.<\/code><\/pre>\n\n\n\n<p>\u4e09 \u6210\u5458\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6784\u9020\n\npromise(); (1)(C++11 \u8d77)\ntemplate&lt; class Alloc &gt;\npromise( std::allocator_arg_t, const Alloc&amp; alloc ); (2)(C++11 \u8d77)\npromise( promise&amp;&amp; other ) noexcept; (3)(C++11 \u8d77)\npromise( const promise&amp; other ) = delete; (4)(C++11 \u8d77) \/\/ \u4e0d\u53ef\u590d\u5236\u6784\u9020\n    1\n    2\n    3\n    4\n    5\n\n\u6790\u6784\n\n~promise(); (C++11 \u8d77) \/\/ \u629b\u5f03\u5171\u4eab\u72b6\u6001\n    1\n\n\u82e5\u5171\u4eab\u72b6\u6001\u5c31\u7eea\uff0c\u5219\u91ca\u653e\u5b83\u3002\u82e5\u5171\u4eab\u72b6\u6001\u672a\u5c31\u7eea\uff0c\u5219\u629b\u5f03\u3002\n\noperator=\n\npromise&amp; operator=( promise&amp;&amp; other ) noexcept; (1)(C++11 \u8d77)\npromise&amp; operator=( const promise&amp; rhs ) = delete; (2)(C++11 \u8d77) \/\/ \u4e0d\u53ef\u590d\u5236\u8d4b\u503c\n    1\n    2<\/code><\/pre>\n\n\n\n<p>\u56db get_future<\/p>\n\n\n\n<p>std::future get_future(); (C++11 \u8d77)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n\n\u82e5\u65e0\u5171\u4eab\u72b6\u6001\uff0c\u6216\u5df2\u8c03\u7528 get_future \u5219\u629b\u51fa\u5f02\u5e38\u3002\u5373\u4ec5\u53ef\u8c03\u7528\u4e00\u6b21\u3002\n\u53ef\u901a\u8fc7 get_future \u95f4\u63a5\u83b7\u5f97\u7ed3\u679c\u3002<\/code><\/pre>\n\n\n\n<p>\u4e94 \u8bbe\u7f6e\u7ed3\u679c<\/p>\n\n\n\n<ol>\n<li>set_value \u8bbe\u7f6e\u7ed3\u679c\u4e3a\u6307\u5b9a\u503c, \u5e76\u4f7f\u72b6\u6001\u5c31\u7eea\u3002<\/li>\n<\/ol>\n\n\n\n<p>\/\/ (\u4ec5\u4e3a\u6cdb\u578b promise \u6a21\u677f\u7684\u6210\u5458)<br>void set_value( const R&amp; value ) (1)(C++11 \u8d77)<br>void set_value( R&amp;&amp; value ); (2)(C++11 \u8d77)<\/p>\n\n\n\n<p>\/\/ (\u4ec5\u4e3a promise \u6a21\u677f\u7279\u5316\u7684\u6210\u5458)<br>void set_value( R&amp; value ); (3)(C++11 \u8d77)<br>\/\/ (\u4ec5\u4e3a promise \u6a21\u677f\u7279\u5316\u7684\u6210\u5458)<br>void set_value(); (4)(C++11 \u8d77)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n\ndemo<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ sort<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ future promise<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ cout cin endl<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ istream_iterator back_inserter<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ istringstream<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ thread<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ vector<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ accumulate<\/h1>\n\n\n\n<p>void accumulate(std::vector::iterator first,<br>std::vector::iterator last,<br>std::promise accumulate_promise) {<br>int sum = std::accumulate(first, last, 0);<br>accumulate_promise.set_value(sum); \/\/ \u539f\u5b50\u5730\u5b58\u50a8 num \u5230\u5171\u4eab\u72b6\u6001\uff0c\u5e76\u4ee4\u72b6\u6001\u5c31\u7eea<br>}<br>int main() {<br>std::istringstream iss_numbers{&#8220;3 4 1 42 23 -23 93 2 -289 93&#8221;};<br>std::vector numbers;<br>{<br>\/\/ set_value (4)<br>std::promise numbers_promise;<br>std::future numbers_future = numbers_promise.get_future();<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::thread t(&#91;&amp;] {\n  std::copy(std::istream_iterator&lt;int&gt;{iss_numbers},\n            std::istream_iterator&lt;int&gt;{}, std::back_inserter(numbers));\n  numbers_promise.set_value(); \/\/ \u4f7f\u72b6\u6001\u5c31\u7eea\n});\n\nnumbers_future.wait();\nstd::sort(numbers.begin(), numbers.end());\n\nfor (int num : numbers)\n  std::cout &lt;&lt; num &lt;&lt; ' ';\nstd::cout &lt;&lt; std::endl;\n\nt.join();<\/code><\/pre>\n\n\n\n<p>}<br>{<br>\/\/ set_value (1)<br>std::promise accumulate_promise;<br>std::future accumulate_future = accumulate_promise.get_future();<br>std::thread t(accumulate, numbers.begin(), numbers.end(),<br>std::move(accumulate_promise));<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::cout &lt;&lt; \"result=\" &lt;&lt; accumulate_future.get() &lt;&lt; '\\n';\nt.join(); <\/code><\/pre>\n\n\n\n<p>}<br>}<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n\n\u7ed3\u679c<\/code><\/pre>\n\n\n\n<p>-289 -23 1 2 3 4 23 42 93 93<br>result=-51<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2<\/code><\/pre>\n\n\n\n<ol start=\"2\">\n<li>set_value_at_thread_exit \u539f\u5b50\u5730\u5b58\u50a8 value \u5230\u5171\u4eab\u72b6\u6001\uff0c\u800c\u4e0d\u7acb\u5373\u4ee4\u72b6\u6001\u5c31\u7eea\u3002\u5728\u5f53\u524d\u7ebf\u7a0b\u9000\u51fa\u65f6\uff0c\u9500\u6bc1\u6240\u6709\u62e5\u6709\u7ebf\u7a0b\u5c40\u57df\u5b58\u50a8\u671f\u7684\u5bf9\u8c61\u540e\uff0c\u518d\u4ee4\u72b6\u6001\u5c31\u7eea\u3002<\/li>\n<\/ol>\n\n\n\n<p>\/\/ (\u4ec5\u4e3a\u6cdb\u578b promise \u6a21\u677f\u7684\u6210\u5458)<br>void set_value_at_thread_exit( const R&amp; value );(1) (C++11 \u8d77)<br>void set_value_at_thread_exit( R&amp;&amp; value );(2) (C++11 \u8d77)<\/p>\n\n\n\n<p>\/\/ (\u4ec5\u4e3a promise \u6a21\u677f\u7279\u5316\u7684\u6210\u5458)<br>void set_value_at_thread_exit( R&amp; value );(3) (C++11 \u8d77)<br>\/\/ (\u4ec5\u4e3a promise \u6a21\u677f\u7279\u5316\u7684\u6210\u5458)<br>void set_value_at_thread_exit();(4) (C++11 \u8d77)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n\ndemo<\/code><\/pre>\n\n\n\n<p>\/\/ set_value_at_thread_exit<br>using namespace std::chrono_literals;<br>std::promise p;<br>std::future f = p.get_future();<br>std::thread([&amp;p] {<br>std::this_thread::sleep_for(1s);<br>p.set_value_at_thread_exit(9);<br>}).detach();<\/p>\n\n\n\n<p>std::cout &lt;&lt; &#8220;Waiting\u2026&#8221; &lt;&lt; std::flush;<br>f.wait();<br>std::cout &lt;&lt; &#8220;Done!\\nResult is: &#8221; &lt;&lt; f.get() &lt;&lt; std::endl;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n\n\u7ed3\u679c<\/code><\/pre>\n\n\n\n<p>Waiting\u2026Done!<br>Result is: 9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2<\/code><\/pre>\n\n\n\n<ol start=\"3\">\n<li>set_exception \u5b58\u50a8\u5f02\u5e38\u6307\u9488 p \u5230\u5171\u4eab\u72b6\u6001\u4e2d\uff0c\u5e76\u4ee4\u72b6\u6001\u5c31\u7eea\u3002<\/li>\n<\/ol>\n\n\n\n<p>void set_exception( std::exception_ptr p );(C++11 \u8d77)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n\ndemo<\/code><\/pre>\n\n\n\n<p>\/\/ set_exception<br>std::promise p;<br>std::future f = p.get_future();<\/p>\n\n\n\n<p>std::thread t([&amp;p] {<br>try {<br>throw std::runtime_error(&#8220;Example&#8221;);<br>} catch (\u2026) {<br>try {<br>\/\/ store anything thrown in the promise<br>p.set_exception(std::current_exception());<br>} catch (\u2026) {<br>} \/\/ set_exception() may throw too<br>}<br>});<\/p>\n\n\n\n<p>try {<br>std::cout &lt;&lt; f.get();<br>} catch (const std::exception&amp; e) {<br>std::cout &lt;&lt; &#8220;Exception from the thread: &#8221; &lt;&lt; e.what() &lt;&lt; std::endl;<br>}<br>t.join();<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n\n\u7ed3\u679c<\/code><\/pre>\n\n\n\n<p>Exception from the thread: Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1<\/code><\/pre>\n\n\n\n<ol start=\"4\">\n<li>set_exception_at_thread_exit \u5b58\u50a8\u5f02\u5e38\u6307\u9488 p \u5230\u5171\u4eab\u72b6\u6001\u4e2d\uff0c\u800c\u4e0d\u7acb\u5373\u4f7f\u72b6\u6001\u5c31\u7eea\u3002\u5728\u5f53\u524d\u7ebf\u7a0b\u9000\u51fa\u65f6\uff0c\u9500\u6bc1\u6240\u6709\u62e5\u6709\u7ebf\u7a0b\u5c40\u57df\u5b58\u50a8\u671f\u7684\u53d8\u91cf\u540e\uff0c\u518d\u96f6\u72b6\u6001\u5c31\u7eea\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u516d \u53c2\u8003<\/p>\n\n\n\n<p>cppreference-promise<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u4e00 promise \u4e8c \u5b9a\u4e49 \/\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts\/10"}],"collection":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":1,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":11,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions\/11"}],"wp:attachment":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}