{"id":134544,"date":"2020-07-21T04:34:07","date_gmt":"2020-07-20T20:34:07","guid":{"rendered":"http:\/\/4563.org\/?p=134544"},"modified":"2020-07-21T04:34:07","modified_gmt":"2020-07-20T20:34:07","slug":"c-thread-%e5%b9%b6%e5%8f%91%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=134544","title":{"rendered":"c++ thread \u5e76\u53d1\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  c++ thread \u5e76\u53d1\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Huelse <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6211\u60f3\u7528 thread \u5e76\u53d1\u5904\u7406 for \u5faa\u73af\uff0c\u4f46\u603b\u662f join \u653e\u5728\u5faa\u73af\u5916\u5c31\u4f1a\u51fa\u9519\uff0c\u662f lambda \u7684\u95ee\u9898\u5417\uff1f<\/p>\n<pre><code>vector&lt;thread&gt; threads;  for (int k = 1; k &lt; d; k++) {     thread t1([&amp;](){         A_result[k] = Linear_Transform_Plain(A_result[0], params);     });     thread t2([&amp;](){         B_result[k] = Linear_Transform_Plain(B_result[0], params);     });     threads.push_back(t1);     threads.push_back(t2);     \/\/ join \u5728\u91cc\u9762\u5c31\u4e0d\u4f1a\u51fa\u9519     \/\/ t1.join();     \/\/ t2.join(); } for(auto i = threads.begin(); i != threads.end(); i++){     i-&gt;join(); } <\/code><\/pre>\n<p>\u62a5\u9519\uff1a<\/p>\n<pre><code>terminate called after throwing an instance of 'std::bad_alloc'   what():  std::bad_alloc Aborted (core dumped) <\/code><\/pre>\n<p>\u5c1d\u8bd5\u8fc7\u6362\u7528<code>thread threads[14]<\/code>\u7136\u540e\u7d22\u5f15\u8d4b\u503c\u7684\u65b9\u6cd5\uff0c\u62a5\u9519\uff1a<\/p>\n<pre><code>terminate called recursively Aborted (core dumped) <\/code><\/pre>\n<p>\u7136\u540e\u4e0d\u7528 lambda \u5c31\u53ef\u4ee5\u8fd0\u884c\uff0c\u4f46\u83b7\u53d6\u4e0d\u5230\u8fd4\u56de\u503c\u4e86<\/p>\n<pre><code>thread threads[14];  for (int k = 1; k &lt; d; k++) {     threads[k-1] = thread(Linear_Transform_Plain, A_result[0], params);     threads[k-1+7] = thread(Linear_Transform_Plain, B_result[0], params); } for (int k = 1; k &lt; d; k++) {     threads[k-1].join();     threads[k-1+7].join(); } <\/code><\/pre>\n<p>\u7f51\u4e0a\u627e\u4e86\u5927\u591a\u662f\u57fa\u7840\u7528\u6cd5\u548c\u7ebf\u7a0b\u6c60\uff0c\u5e76\u4e0d\u80fd\u8fbe\u5230\u6211\u60f3\u8981\u7684\u5168\u6838\u5fc3\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<p>\u8fd9\u91cc\u5148\u611f\u8c22\u5404\u4f4d\u4e86\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>17<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2532203\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hankai17 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             thread \u4e0d\u652f\u6301 copy?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532204\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Huelse <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hankai17 #1 \u662f\u4ec0\u4e48\u610f\u601d\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532205\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gantleman <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ece C++\u7684\u8bed\u6cd5\u6765\u8bf4\u4f60\u5728 for \u5faa\u73af\u91cc\u58f0\u660e\u4e86\u4e24\u4e2a thread t1 t2 \u7684\u5c40\u90e8\u53d8\u91cf\u3002<br \/>\u79bb\u5f00 for \u5faa\u73af\u540e\u8fd9\u4e24\u4e2a\u5c40\u90e8\u53d8\u91cf\u88ab\u9500\u6bc1\uff0c\u4efb\u4f55\u4f7f\u7528\u7684\u64cd\u4f5c\u90fd\u662f\u975e\u6cd5\u7684\u3002<br \/>\u5efa\u8bae\u4f60\u6539\u7528 new \u5c1d\u8bd5\u4e0b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532206\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nightwitch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e00\u4e2a\u91cc\u9762\uff0c\u4e0d\u8981\u7528\u5f15\u7528\u53bb\u6355\u83b7 k \u7684\u503c\uff0ck \u7684\u503c\u4e00\u76f4\u5728\u53d8\uff0c\u800c\u4e14\u5f53 for \u5faa\u73af\u7ed3\u675f\u4ee5\u540e\uff0ck \u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\uff0c\u4f60\u7684 lambda \u91cc\u9762\u7684 k \u5c31\u662f\u7a7a\u60ac\u5f15\u7528\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532207\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nightwitch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gantleman t1\uff0ct2 \u88ab push \u5230 vector \u91cc\u9762\u53bb\u4e86\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532208\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nannanziyu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             std::thread \u6ca1\u6709\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/p>\n<p>http:\/\/www.cplusplus.com\/reference\/thread\/thread\/thread\/<br \/>3) copy constructor<br \/>Deleted constructor form (thread objects cannot be copied).<\/p>\n<p>\u6240\u4ee5\u4fee\u6539 threads.push_back(t1); \u4e3a threads.push_back(std::move(t1))                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532209\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : V2WT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/>#include &lt;iostream&gt;<br \/>#include &lt;thread&gt;<br \/>#include &lt;vector&gt;<br \/>#include &lt;algorithm&gt;<br \/>int main()<br \/>{<br \/> \/\/ vector container stores threads<br \/> std::vector&lt;std::thread&gt; workers;<br \/> for (int i = 0; i &lt; 5; i++) {<br \/> workers.push_back(std::thread([]() <br \/> {<br \/> std::cout &lt;&lt; &#8220;thread functionn&#8221;;<br \/> }));<br \/> }<br \/> std::cout &lt;&lt; &#8220;main threadn&#8221;;<\/p>\n<p> \/\/ Looping every thread via for_each<br \/> \/\/ The 3rd argument assigns a task<br \/> \/\/ It tells the compiler we&#8217;re using lambda ([])<br \/> \/\/ The lambda function takes its argument as a reference to a thread, t<br \/> \/\/ Then, joins one by one, and this works like barrier<br \/> std::for_each(workers.begin(), workers.end(), [](std::thread &amp;t) <br \/> {<br \/> t.join();<br \/> });<\/p>\n<p> return 0;<br \/>}<\/p>\n<p>&#8220;`<br \/>I found these code, from here: <br \/>[click]( https:\/\/www.bogotobogo.com\/cplusplus\/C11\/3_C11_Threading_Lambda_Functions.php).<br \/>Hope this would help!                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532210\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nannanziyu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7136\u540e\u8fd8\u6709 4 \u8bf4\u7684\uff0c\u4f60\u7684 k \u4e0d\u80fd\u4f20\u5f15\u7528\uff0c\u8981\u4f20\u503c <br \/>\u6539\u6210<br \/>std::thread t1([&amp;,k]() {<br \/>});                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532211\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Huelse <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nannanziyu #6 <br \/>@nightwitch #4 <br \/>\u611f\u8c22\u611f\u8c22~<br \/>\u7efc\u5408\u4e24\u4f4d\u7684\u7b54\u6848\uff0c\u5c06\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u6539\u6210<br \/>&#8220;`<br \/>std::thread t1([&amp;, k](){<br \/> A_result[k] = &#8230;<br \/>});<\/p>\n<p>threads.push_back(std::move(t1));<br \/>&#8220;`<br \/>\u5c31\u53ef\u4ee5\u4e86\uff0c\u6838\u5fc3\u8dd1\u6ee1\uff0c\u8212\u670d\u4e86~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532212\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gantleman <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nightwitch \u662f\u7684\uff0c6 \u662f\u6b63\u786e\u7b54\u6848                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532213\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : GeruzoniAnsasu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             c++11 \u4ee5\u540e\u4e0d\u8981\u8bb0\u5f97\u5bb9\u5668\u6709 push_back \u8fd9\u4e2a\u51fd\u6570<\/p>\n<p>\u4e00\u5f8b\u7528 emplace_back\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u81ea\u52a8\u8f6c\u53d1\u5de6\u53f3\u503c\u5f15\u7528<\/p>\n<p>emplace_back \u8fd8\u6709\u4e00\u4e2a\u91cd\u8f7d\u662f\u7528\u5165\u53c2\u6784\u9020\u5143\u7d20\uff0c\u6240\u4ee5\u53ef\u4ee5<\/p>\n<p>threads.emplace_back([&amp;](){WHATEVER})                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532214\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : billyzs <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @GeruzoniAnsasu \u8fd9\u4e2a\u4f8b\u5b50\u76f4\u63a5\u4e0a emplace_back()\u6ca1\u95ee\u9898\uff0c\u4e0d\u8fc7\u660e\u786e\u9700\u8981\u62f7\u8d1d\u5de6\u503c\u7684\u65f6\u5019 push_back()\u53ef\u8bfb\u6027\u66f4\u9ad8<br \/>[https:\/\/abseil.io\/tips\/112]( https:\/\/abseil.io\/tips\/112)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532215\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tusj <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             threads.push_back(std::move(t1));                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532216\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Sentan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nannanziyu \u5927\u4f6c\uff0c\u95ee\u4e0b\uff0c\u7528 std:move \u5904\u7406 tread \u90a3\u4e2a\u5bf9\u8c61\uff0c\u628a\u4ed6\u53d8\u6210\u5de6\u503c\u662f\u4e3a\u4e86\u4ec0\u4e48\uff0c\u8fd9\u6837\u662f\u4f1a\u628a\u8fd9\u4e2a\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u53d8\u957f\u5417\uff0c\u8fd8\u662f\u8bf4\u8ba9\u4ed6\u4ece\u6808\u7a7a\u95f4\u53d8\u6210\u5806\u7a7a\u95f4\u4e86\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532217\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wirbelwind <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Sentan thread \u53ea\u6709\u79fb\u52a8\u8bed\u4e49\uff0c\u6240\u4ee5\u4e0d\u80fd copy\uff0c\u53ea\u80fd\u8f6c\u8ba9\u6240\u6709\u6743\uff0c\u7528 std::move \u544a\u8bc9\u7f16\u8bd1\u5668\u8c03\u7528\u79fb\u52a8\u6784\u9020                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532218\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wirbelwind <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Sentan https:\/\/yoshino.now.sh\/posts\/How-to-understanding-cpp11-rvalue-reference \u53ef\u4ee5\u770b\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2532219\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Sentan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Wirbelwind \u54e6\u54e6\uff0c\u660e\u767d\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>c++ thread \u5e76\u53d1\u95ee\u9898 \u8cc7&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\/134544"}],"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=134544"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/134544\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134544"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}