{"id":1982,"date":"2016-04-17T21:49:40","date_gmt":"2016-04-17T13:49:40","guid":{"rendered":"http:\/\/boweihe.me\/?p=1982"},"modified":"2016-04-17T21:49:40","modified_gmt":"2016-04-17T13:49:40","slug":"c11%e9%87%8c%e5%a4%b4%e7%9a%84%e5%88%9d%e6%8e%a2","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1982","title":{"rendered":"C++11\u91cc\u5934\u7684thread\u521d\u63a2"},"content":{"rendered":"<p>C++11\u6807\u51c6\u7ed9\u4e86\u5f88\u591a\u201c\u7ec8\u4e8e\u7b49\u5230\u4e86\u201d\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e00\u9879\u5c31\u662f\u7ebf\u7a0b\u7c7b<span class=\"lang:c++ decode:true crayon-inline \">&lt;thread&gt;<\/span>\u00a0\uff0c\u6709\u671d\u4e00\u65e5\u7ec8\u4e8e\u53ef\u4ee5\u51cf\u5c11\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u4e86\uff0c\u9694\u58c1J\u5bb6\u7b11\u800c\u4e0d\u8bed\u3002<br \/>\n&nbsp;<br \/>\n\u7528\u6cd5\u771f\u662f\u7b80\u5355\u591a\u4e86\uff0c\u6700\u7b80\u5355\u7684\u4e00\u79cd\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;thread&gt;\n#include &lt;iostream&gt;\nusing namespace std;\nvoid pause_thread(int n) {\n\tthis_thread::sleep_for(chrono::seconds(2));\n\tcout &lt;&lt; \"thread of \" &lt;&lt; n &lt;&lt; \" ended\\n\";\n}\nint main() {\n\tint N = 0;\n\tcin &gt;&gt; N;\n\tthread* ths = new thread[N];\n\tfor (int i = 0; i &lt; N; i++) {\n\t\tths[i] = thread(pause_thread, i);\n\t}\n\tcout &lt;&lt; \"Finished spawning threads.\" &lt;&lt; endl;\n\tfor (int i = 0; i &lt; N; i++) {\n\t\tths[i].join();\n\t}\n\tcout &lt;&lt; \"All threads joined!\" &lt;&lt; endl;\n\tdelete[]ths;\n\treturn 0;\n}<\/pre>\n<p>\u4e0a\u9762\u7a0b\u5e8f\u7684\u8f93\u51fa\uff1a<br \/>\n<a href=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/QQ\u622a\u56fe20160417205608.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1983\" src=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/QQ\u622a\u56fe20160417205608.jpg\" alt=\"cpp11_thread\" width=\"734\" height=\"491\" \/><\/a><br \/>\n\u5f53\u7136\u7531\u4e8e\u6211\u7684\u51fd\u6570\u91cc\u5b9a\u4e49\u7684\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\uff0c\u6240\u4ee5cout\u7684\u65f6\u5019\u5c31\u6df7\u6742\u4e86~<br \/>\n\u6240\u4ee5\u53ef\u4ee5\u7528\u5230<span class=\"lang:c++ decode:true crayon-inline \">&lt;mutex&gt;<\/span>\u00a0\u7c7b\u91cc\u9762\u7684\u4e92\u65a5\u91cf\u6765\u89e3\u51b3\u4e00\u4e0b\uff0c\u6bd4\u5982\u8fd9\u6837<\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;thread&gt;\n#include &lt;mutex&gt;\n#include &lt;iostream&gt;\nusing namespace std;\nvoid pause_thread(int n, mutex* mtx) {\n\tthis_thread::sleep_for(chrono::seconds(2));\n\t(*mtx).lock();\n\tcout &lt;&lt; \"thread of \" &lt;&lt; n &lt;&lt; \" ended\\n\";\n\t(*mtx).unlock();\n}\nint main() {\n\tint N = 0;\n\tcin &gt;&gt; N;\n\tthread* ths = new thread[N];\n\tmutex mtx;\n\tfor (int i = 0; i &lt; N; i++) {\n\t\tths[i] = thread(pause_thread, i, &amp;mtx);\n\t}\n\tcout &lt;&lt; \"Finished spawning threads.\" &lt;&lt; endl;\n\tfor (int i = 0; i &lt; N; i++) {\n\t\tths[i].join();\n\t}\n\tcout &lt;&lt; \"All threads joined!\" &lt;&lt; endl;\n\tdelete[]ths;\n\treturn 0;\n}<\/pre>\n<p>\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u8f93\u51fa\u4e0d\u4f1a\u7ed5\u5728\u4e00\u8d77\u4e86~\u5f53\u7136\u8fd8\u662f\u65e0\u5e8f\u7684\uff0c\u54c8\u54c8<br \/>\n<a href=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/cpp11_mutex.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1984\" src=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/04\/cpp11_mutex.jpg\" alt=\"cpp11_mutex\" width=\"483\" height=\"483\" \/><\/a><br \/>\n&nbsp;<br \/>\n<a href=\"http:\/\/www.cplusplus.com\/reference\/thread\/thread\/join\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u67e5\u6587\u6863<\/a>\u7684\u65f6\u5019\u6ce8\u610f\u5230\uff0c\u7adf\u7136\u8fd8\u6709\u6b7b\u9501\u68c0\u6d4b\u76f8\u5173\u7684exception\u53ef\u80fd\u4f1a\u629b\u51fa\uff0c\u6709\u70b9\u826f\u5fc3\u7684&#8230;\u8fd9\u51e0\u5929\u597d\u597d\u7814\u7a76\u4e00\u4e0b~<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++11\u6807\u51c6\u7ed9\u4e86\u5f88\u591a\u201c\u7ec8\u4e8e\u7b49\u5230\u4e86\u201d\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e00\u9879\u5c31\u662f\u7ebf\u7a0b\u7c7b&lt;thread&gt;\u00a0\uff0c\u6709\u671d\u4e00\u65e5\u7ec8\u4e8e\u53ef\u4ee5\u51cf\u5c11\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u4e86\uff0c\u9694\u58c1J\u5bb6\u7b11\u800c\u4e0d\u8bed\u3002 &nbsp; \u7528\u6cd5\u771f\u662f\u7b80\u5355\u591a\u4e86\uff0c\u6700\u7b80\u5355\u7684\u4e00\u79cd\u5982\u4e0b\uff1a #include &lt;thread&gt; #include &lt;iostream&gt; using namespace std; void pause_thread(int n) { this_thread::sleep_for(chrono::seconds(2)); cout &lt;&lt; &#8220;thread of &#8221; &lt;&lt; n &lt;&lt; &#8221; ended\\n&#8221;; } int main() { int N = 0; cin &gt;&gt; N; thread* ths = new thread[N]; for (int i = 0; i &lt; N; i++) { ths[i] = thread(pause_thread, i); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[28,150],"class_list":["post-1982","post","type-post","status-publish","format-standard","hentry","category-study","tag-c","tag-150"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1982"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1982\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}