{"id":100,"date":"2026-03-22T21:42:40","date_gmt":"2026-03-22T13:42:40","guid":{"rendered":"https:\/\/codergodv.xyz\/?p=100"},"modified":"2026-03-22T21:42:41","modified_gmt":"2026-03-22T13:42:41","slug":"c11-%e7%ba%bf%e7%a8%8b%e5%ba%93","status":"publish","type":"post","link":"https:\/\/codergodv.xyz\/index.php\/2026\/03\/22\/c11-%e7%ba%bf%e7%a8%8b%e5%ba%93\/","title":{"rendered":"C++11 \u2014\u2014\u2014 \u7ebf\u7a0b\u5e93"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7ebf\u7a0b\u5e93<\/h2>\n\n\n\n<p>\u5728C++11\u4e4b\u524d\uff0c\u6d89\u53ca\u5230\u591a\u7ebf\u7a0b\u95ee\u9898\uff0c\u90fd\u662f\u548c\u5e73\u53f0\u76f8\u5173\u7684\uff0c\u6bd4\u5982Windows\u548cLinux\u4e0b\u5404\u6709\u81ea\u5df1\u7684\u63a5\u53e3\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u6bd4\u8f83\u5dee\u3002C++11\u4e2d\u6700\u91cd\u8981\u7684\u7279\u6027\u5c31\u662f\u5bf9\u7ebf\u7a0b\u8fdb\u884c\u4e86\u652f\u6301\uff0c\u4f7f\u5f97C++\u5728\u5e76\u884c\u7f16\u7a0b\u65f6\u4e0d\u9700\u8981\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\uff0c\u800c\u4e14\u5728\u539f\u5b50\u64cd\u4f5c\u4e2d\u8fd8\u5f15\u5165\u4e86\u539f\u5b50\u7c7b\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ebf\u7a0b\u5e93\uff08thread\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ebf\u7a0b\u5bf9\u8c61\u7684\u6784\u9020\u65b9\u5f0f<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e00\u3001\u8c03\u7528\u65e0\u53c2\u7684\u6784\u9020\u51fd\u6570<\/p>\n<\/blockquote>\n\n\n\n<p>thread\u63d0\u4f9b\u4e86\u65e0\u53c2\u7684\u6784\u9020\u51fd\u6570\uff0c\u8c03\u7528\u65e0\u53c2\u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u51fa\u6765\u7684\u7ebf\u7a0b\u5bf9\u8c61\u6ca1\u6709\u5173\u8054\u4efb\u4f55\u7ebf\u7a0b\u51fd\u6570\uff0c\u5373\u6ca1\u6709\u542f\u52a8\u4efb\u4f55\u7ebf\u7a0b\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thread t1;<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8ethread\u63d0\u4f9b\u4e86\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\uff0c\u56e0\u6b64\u5f53\u540e\u7eed\u9700\u8981\u8ba9\u8be5\u7ebf\u7a0b\u5bf9\u8c61\u4e0e\u7ebf\u7a0b\u51fd\u6570\u5173\u8054\u65f6\uff0c\u53ef\u4ee5\u4ee5\u5e26\u53c2\u7684\u65b9\u5f0f\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u79fb\u52a8\u8d4b\u503c\u5c06\u8be5\u533f\u540d\u5bf9\u8c61\u5173\u8054\u7ebf\u7a0b\u7684\u72b6\u6001\u8f6c\u79fb\u7ed9\u8be5\u7ebf\u7a0b\u5bf9\u8c61\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n)\n{\n    for (int i = 0; i &lt;= n; i++)\n    {\n        cout &lt;&lt; i &lt;&lt; endl;\n    }\n}\nint main()\n{\n    thread t1;\n    \/\/...\n    t1 = thread(func, 10);\n\n    t1.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u573a\u666f\uff1a<\/strong> \u5b9e\u73b0\u7ebf\u7a0b\u6c60\u7684\u65f6\u5019\u5c31\u662f\u9700\u8981\u5148\u521b\u5efa\u4e00\u6279\u7ebf\u7a0b\uff0c\u4f46\u4e00\u5f00\u59cb\u8fd9\u4e9b\u7ebf\u7a0b\u4ec0\u4e48\u4e5f\u4e0d\u505a\uff0c\u5f53\u6709\u4efb\u52a1\u5230\u6765\u65f6\u518d\u8ba9\u8fd9\u4e9b\u7ebf\u7a0b\u6765\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e8c\u3001\u8c03\u7528\u5e26\u53c2\u7684\u6784\u9020\u51fd\u6570<\/p>\n<\/blockquote>\n\n\n\n<p>thread\u7684\u5e26\u53c2\u7684\u6784\u9020\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;class Fn, class... Args&gt;\nexplicit thread (Fn&amp;&amp; fn, Args&amp;&amp;... args);<\/code><\/pre>\n\n\n\n<p><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fn<\/code>\uff1a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u6bd4\u5982\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u3001lambda\u8868\u8fbe\u5f0f\u3001\u88ab\u5305\u88c5\u5668\u5305\u88c5\u540e\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7b49\u3002<\/li>\n\n\n\n<li><code>args...<\/code>\uff1a\u8c03\u7528\u53ef\u8c03\u7528\u5bf9\u8c61fn\u65f6\u6240\u9700\u8981\u7684\u82e5\u5e72\u53c2\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8c03\u7528\u5e26\u53c2\u7684\u6784\u9020\u51fd\u6570\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u80fd\u591f\u5c06\u7ebf\u7a0b\u5bf9\u8c61\u4e0e\u7ebf\u7a0b\u51fd\u6570fn\u8fdb\u884c\u5173\u8054\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n)\n{\n    for (int i = 0; i &lt;= n; i++)\n    {\n        cout &lt;&lt; i &lt;&lt; endl;\n    }\n}\nint main()\n{\n    thread t2(func, 10);\n\n    t2.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e09\u3001\u8c03\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570<\/p>\n<\/blockquote>\n\n\n\n<p>thread\u63d0\u4f9b\u4e86\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c\u80fd\u591f\u7528\u4e00\u4e2a\u53f3\u503c\u7ebf\u7a0b\u5bf9\u8c61\u6765\u6784\u9020\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n)\n{\n    for (int i = 0; i &lt;= n; i++)\n    {\n        cout &lt;&lt; i &lt;&lt; endl;\n    }\n}\nint main()\n{\n    thread t3 = thread(func, 10);\n\n    t3.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\u4e00\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u4e00\u4e2a\u6982\u5ff5\uff0c\u7ebf\u7a0b\u5bf9\u8c61\u53ef\u4ee5\u5173\u8054\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u7528\u6765\u63a7\u5236\u7ebf\u7a0b\u4ee5\u53ca\u83b7\u53d6\u7ebf\u7a0b\u7684\u72b6\u6001\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61\u65f6\u6ca1\u6709\u63d0\u4f9b\u7ebf\u7a0b\u51fd\u6570\uff0c\u90a3\u4e48\u8be5\u7ebf\u7a0b\u5bf9\u8c61\u5b9e\u9645\u6ca1\u6709\u5bf9\u5e94\u4efb\u4f55\u7ebf\u7a0b\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61\u65f6\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u51fd\u6570\uff0c\u90a3\u4e48\u5c31\u4f1a\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c\u8fd9\u4e2a\u7ebf\u7a0b\u51fd\u6570\uff0c\u8be5\u7ebf\u7a0b\u4e0e\u4e3b\u7ebf\u7a0b\u4e00\u8d77\u8fd0\u884c\u3002<\/li>\n\n\n\n<li>thread\u7c7b\u662f\u9632\u62f7\u8d1d\u7684\uff0c\u4e0d\u5141\u8bb8\u62f7\u8d1d\u6784\u9020\u548c\u62f7\u8d1d\u8d4b\u503c\uff0c\u4f46\u662f\u53ef\u4ee5\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u5173\u8054\u7ebf\u7a0b\u7684\u72b6\u6001\u8f6c\u79fb\u7ed9\u5176\u4ed6\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u5e76\u4e14\u8f6c\u79fb\u671f\u95f4\u4e0d\u5f71\u54cd\u7ebf\u7a0b\u7684\u6267\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">thread\u63d0\u4f9b\u7684\u6210\u5458\u51fd\u6570<\/h4>\n\n\n\n<p>thread\u4e2d\u5e38\u7528\u7684\u6210\u5458\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">\u6210\u5458\u51fd\u6570<\/th><th class=\"has-text-align-center\" data-align=\"center\">\u529f\u80fd<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">join<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5bf9\u8be5\u7ebf\u7a0b\u8fdb\u884c\u7b49\u5f85\uff0c\u5728\u7b49\u5f85\u7684\u7ebf\u7a0b\u8fd4\u56de\u4e4b\u524d\uff0c\u8c03\u7528join\u51fd\u6570\u7684\u7ebf\u7a0b\u5c06\u4f1a\u88ab\u963b\u585e<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">joinable<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5224\u65ad\u8be5\u7ebf\u7a0b\u662f\u5426\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c\u5982\u679c\u662f\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">detach<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u8be5\u7ebf\u7a0b\u4e0e\u521b\u5efa\u7ebf\u7a0b\u8fdb\u884c\u5206\u79bb\uff0c\u88ab\u5206\u79bb\u540e\u7684\u7ebf\u7a0b\u4e0d\u518d\u9700\u8981\u521b\u5efa\u7ebf\u7a0b\u8c03\u7528join\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u7b49\u5f85<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">get_id<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u83b7\u53d6\u8be5\u7ebf\u7a0b\u7684id<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">swap<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c06\u4e24\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u5173\u8054\u7ebf\u7a0b\u7684\u72b6\u6001\u8fdb\u884c\u4ea4\u6362<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6b64\u5916\uff0c<code>joinable<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5224\u5b9a\u7ebf\u7a0b\u662f\u5426\u662f\u6709\u6548\u7684\uff0c\u5982\u679c\u662f\u4ee5\u4e0b\u4efb\u610f\u60c5\u51b5\uff0c\u5219\u7ebf\u7a0b\u65e0\u6548\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u91c7\u7528\u65e0\u53c2\u6784\u9020\u51fd\u6570\u6784\u9020\u7684\u7ebf\u7a0b\u5bf9\u8c61\u3002\uff08\u8be5\u7ebf\u7a0b\u5bf9\u8c61\u6ca1\u6709\u5173\u8054\u4efb\u4f55\u7ebf\u7a0b\uff09<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u5bf9\u8c61\u7684\u72b6\u6001\u5df2\u7ecf\u8f6c\u79fb\u7ed9\u5176\u4ed6\u7ebf\u7a0b\u5bf9\u8c61\u3002\uff08\u5df2\u7ecf\u5c06\u7ebf\u7a0b\u4ea4\u7ed9\u5176\u4ed6\u7ebf\u7a0b\u5bf9\u8c61\u7ba1\u7406\uff09<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u5df2\u7ecf\u8c03\u7528join\u6216detach\u7ed3\u675f\u3002\uff08\u7ebf\u7a0b\u5df2\u7ecf\u7ed3\u675f\uff09<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u83b7\u53d6\u7ebf\u7a0b\u7684id\u7684\u65b9\u5f0f<\/h4>\n\n\n\n<p>\u8c03\u7528thread\u7684\u6210\u5458\u51fd\u6570<code>get_id<\/code>\u53ef\u4ee5\u83b7\u53d6\u7ebf\u7a0b\u7684id\uff0c\u4f46\u8be5\u65b9\u6cd5\u5fc5\u987b\u901a\u8fc7\u7ebf\u7a0b\u5bf9\u8c61\u6765\u8c03\u7528<code>get_id<\/code>\u51fd\u6570\uff0c\u5982\u679c\u8981\u5728\u7ebf\u7a0b\u5bf9\u8c61\u5173\u8054\u7684\u7ebf\u7a0b\u51fd\u6570\u4e2d\u83b7\u53d6\u7ebf\u7a0bid\uff0c\u53ef\u4ee5\u8c03\u7528<code>this_thread<\/code>\u547d\u540d\u7a7a\u95f4\u4e0b\u7684<code>get_id<\/code>\u51fd\u6570\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func()\n{\n    cout &lt;&lt; this_thread::get_id() &lt;&lt; endl; \/\/\u83b7\u53d6\u7ebf\u7a0bid\n}\nint main()\n{\n    thread t(func);\n\n    t.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><code>this_thread<\/code>\u547d\u540d\u7a7a\u95f4\u4e2d\u8fd8\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4e09\u4e2a\u51fd\u6570\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">\u51fd\u6570\u540d<\/th><th class=\"has-text-align-center\" data-align=\"center\">\u529f\u80fd<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">yield<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5f53\u524d\u7ebf\u7a0b\u201c\u653e\u5f03\u201d\u6267\u884c\uff0c\u8ba9\u64cd\u4f5c\u7cfb\u7edf\u8c03\u5ea6\u53e6\u4e00\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">sleep_until<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8ba9\u5f53\u524d\u7ebf\u7a0b\u4f11\u7720\u5230\u4e00\u4e2a\u5177\u4f53\u65f6\u95f4\u70b9<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">sleep_for<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u8ba9\u5f53\u524d\u7ebf\u7a0b\u4f11\u7720\u4e00\u4e2a\u65f6\u95f4\u6bb5<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\u95ee\u9898<\/h4>\n\n\n\n<p>\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\u662f\u4ee5\u503c\u62f7\u8d1d\u7684\u65b9\u5f0f\u62f7\u8d1d\u5230\u7ebf\u7a0b\u6808\u7a7a\u95f4\u4e2d\u7684\uff0c\u5c31\u7b97\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\u4e3a\u5f15\u7528\u7c7b\u578b\uff0c\u5728\u7ebf\u7a0b\u51fd\u6570\u4e2d\u4fee\u6539\u540e\u4e5f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5916\u90e8\u5b9e\u53c2\uff0c\u56e0\u4e3a\u5176\u5b9e\u9645\u5f15\u7528\u7684\u662f\u7ebf\u7a0b\u6808\u4e2d\u7684\u62f7\u8d1d\uff0c\u800c\u4e0d\u662f\u5916\u90e8\u5b9e\u53c2\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void add(int&amp; num)\n{\n    num++;\n}\nint main()\n{\n    int num = 0;\n    thread t(add, num);\n    t.join();\n\n    cout &lt;&lt; num &lt;&lt; endl; \/\/0\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u8981\u901a\u8fc7\u7ebf\u7a0b\u51fd\u6570\u7684\u5f62\u53c2\u6539\u53d8\u5916\u90e8\u7684\u5b9e\u53c2\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4e09\u79cd\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u65b9\u5f0f\u4e00\uff1a\u501f\u52a9std::ref\u51fd\u6570<\/p>\n<\/blockquote>\n\n\n\n<p>\u5f53\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u4e3a\u5f15\u7528\u7c7b\u578b\u65f6\uff0c\u5982\u679c\u8981\u60f3\u7ebf\u7a0b\u51fd\u6570\u5f62\u53c2\u5f15\u7528\u7684\u662f\u5916\u90e8\u4f20\u5165\u7684\u5b9e\u53c2\uff0c\u800c\u4e0d\u662f\u7ebf\u7a0b\u6808\u7a7a\u95f4\u4e2d\u7684\u62f7\u8d1d\uff0c\u90a3\u4e48\u5728\u4f20\u5165\u5b9e\u53c2\u65f6\u9700\u8981\u501f\u52a9ref\u51fd\u6570\u4fdd\u6301\u5bf9\u5b9e\u53c2\u7684\u5f15\u7528\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void add(int&amp; num)\n{\n    num++;\n}\nint main()\n{\n    int num = 0;\n    thread t(add, ref(num));\n    t.join();\n\n    cout &lt;&lt; num &lt;&lt; endl; \/\/1\n    return 0;\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u65b9\u5f0f\u4e8c\uff1a\u5730\u5740\u7684\u62f7\u8d1d<\/p>\n<\/blockquote>\n\n\n\n<p>\u5c06\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\u6539\u4e3a\u6307\u9488\u7c7b\u578b\uff0c\u5c06\u5b9e\u53c2\u7684\u5730\u5740\u4f20\u5165\u7ebf\u7a0b\u51fd\u6570\uff0c\u6b64\u65f6\u5728\u7ebf\u7a0b\u51fd\u6570\u4e2d\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u8be5\u5730\u5740\u5904\u7684\u53d8\u91cf\uff0c\u8fdb\u800c\u5f71\u54cd\u5230\u5916\u90e8\u5b9e\u53c2\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void add(int* num)\n{\n    (*num)++;\n}\nint main()\n{\n    int num = 0;\n    thread t(add, &amp;num);\n    t.join();\n\n    cout &lt;&lt; num &lt;&lt; endl; \/\/1\n    return 0;\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u65b9\u5f0f\u4e09\uff1a\u501f\u52a9lambda\u8868\u8fbe\u5f0f<\/p>\n<\/blockquote>\n\n\n\n<p>\u5c06lambda\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u7ebf\u7a0b\u51fd\u6570\uff0c\u5229\u7528lambda\u51fd\u6570\u7684\u6355\u6349\u5217\u8868\uff0c\u4ee5\u5f15\u7528\u7684\u65b9\u5f0f\u5bf9\u5916\u90e8\u5b9e\u53c2\u8fdb\u884c\u6355\u6349\uff0c\u6b64\u65f6\u5728lambda\u8868\u8fbe\u5f0f\u4e2d\u5bf9\u5f62\u53c2\u7684\u4fee\u6539\u4e5f\u80fd\u5f71\u54cd\u5230\u5916\u90e8\u5b9e\u53c2\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n    int num = 0;\n    thread t(&#91;&amp;num]{num++; });\n    t.join();\n\n    cout &lt;&lt; num &lt;&lt; endl; \/\/1\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">join\u4e0e<a href=\"https:\/\/so.csdn.net\/so\/search?q=detach&amp;spm=1001.2101.3001.7020\">detach<\/a><\/h4>\n\n\n\n<p>\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u540e\uff0c\u5f53\u8fd9\u4e2a\u7ebf\u7a0b\u9000\u51fa\u65f6\uff0c\u9700\u8981\u5bf9\u8be5\u7ebf\u7a0b\u6240\u4f7f\u7528\u7684\u8d44\u6e90\u8fdb\u884c\u56de\u6536\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u7b49\u95ee\u9898\u3002thread\u5e93\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u5982\u4e0b\u4e24\u79cd\u56de\u6536\u7ebf\u7a0b\u8d44\u6e90\u7684\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>join\u65b9\u5f0f<\/p>\n<\/blockquote>\n\n\n\n<p>\u4e3b\u7ebf\u7a0b\u521b\u5efa\u65b0\u7ebf\u7a0b\u540e\uff0c\u53ef\u4ee5\u8c03\u7528join\u51fd\u6570\u7b49\u5f85\u65b0\u7ebf\u7a0b\u7ec8\u6b62\uff0c\u5f53\u65b0\u7ebf\u7a0b\u7ec8\u6b62\u65f6<code>join<\/code>\u51fd\u6570\u5c31\u4f1a\u81ea\u52a8\u6e05\u7406\u7ebf\u7a0b\u76f8\u5173\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<p><code>join<\/code>\u51fd\u6570\u6e05\u7406\u7ebf\u7a0b\u7684\u76f8\u5173\u8d44\u6e90\u540e\uff0cthread\u5bf9\u8c61\u4e0e\u5df2\u9500\u6bc1\u7684\u7ebf\u7a0b\u5c31\u6ca1\u6709\u5173\u7cfb\u4e86\uff0c\u56e0\u6b64\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u4e00\u822c\u53ea\u4f1a\u4f7f\u7528\u4e00\u6b21<code>join<\/code>\uff0c\u5426\u5219\u7a0b\u5e8f\u4f1a\u5d29\u6e83\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n)\n{\n    for (int i = 0; i &lt;= n; i++)\n    {\n        cout &lt;&lt; i &lt;&lt; endl;\n    }\n}\nint main()\n{\n    thread t(func, 20);\n    t.join();\n    t.join(); \/\/\u7a0b\u5e8f\u5d29\u6e83\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u4f46\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61<code>join<\/code>\u540e\uff0c\u53c8\u8c03\u7528\u79fb\u52a8\u8d4b\u503c\u51fd\u6570\uff0c\u5c06\u4e00\u4e2a\u53f3\u503c\u7ebf\u7a0b\u5bf9\u8c61\u7684\u5173\u8054\u7ebf\u7a0b\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u6765\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u53c8\u53ef\u4ee5\u8c03\u7528\u4e00\u6b21<code>join<\/code>\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n)\n{\n    for (int i = 0; i &lt;= n; i++)\n    {\n        cout &lt;&lt; i &lt;&lt; endl;\n    }\n}\nint main()\n{\n    thread t(func, 20);\n    t.join();\n\n    t = thread(func, 30);\n    t.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u4f46\u91c7\u7528<code>join<\/code>\u7684\u65b9\u5f0f\u7ed3\u675f\u7ebf\u7a0b\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u4e5f\u53ef\u80fd\u4f1a\u51fa\u73b0\u95ee\u9898\u3002\u6bd4\u5982\u5728\u8be5\u7ebf\u7a0b\u88ab<code>join<\/code>\u4e4b\u524d\uff0c\u5982\u679c\u4e2d\u9014\u56e0\u4e3a\u67d0\u4e9b\u539f\u56e0\u5bfc\u81f4\u7a0b\u5e8f\u4e0d\u518d\u6267\u884c\u540e\u7eed\u4ee3\u7801\uff0c\u8fd9\u65f6\u8fd9\u4e2a\u7ebf\u7a0b\u5c06\u4e0d\u4f1a\u88ab<code>join<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n)\n{\n    for (int i = 0; i &lt;= n; i++)\n    {\n        cout &lt;&lt; i &lt;&lt; endl;\n    }\n}\nbool DoSomething()\n{\n    return false;\n}\nint main()\n{\n    thread t(func, 20);\n\n    \/\/...\n    if (!DoSomething())\n        return -1;\n    \/\/...\n\n    t.join(); \/\/\u4e0d\u4f1a\u88ab\u6267\u884c\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u56e0\u6b64\u91c7\u7528<code>join<\/code>\u65b9\u5f0f\u7ed3\u675f\u7ebf\u7a0b\u65f6\uff0c<code>join<\/code>\u7684\u8c03\u7528\u4f4d\u7f6e\u975e\u5e38\u5173\u952e\uff0c\u4e3a\u4e86\u907f\u514d\u4e0a\u8ff0\u95ee\u9898\uff0c\u53ef\u4ee5\u91c7\u7528RAII\u7684\u65b9\u5f0f\u5bf9\u7ebf\u7a0b\u5bf9\u8c61\u8fdb\u884c\u5c01\u88c5\uff0c\u4e5f\u5c31\u662f\u5229\u7528\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u6765\u63a7\u5236\u7ebf\u7a0b\u8d44\u6e90\u7684\u91ca\u653e\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class myThread\n{\npublic:\n    myThread(thread&amp; t)\n        :_t(t)\n    {}\n    ~myThread()\n    {\n        if (_t.joinable())\n            _t.join();\n    }\n    \/\/\u9632\u62f7\u8d1d\n    myThread(myThread const&amp;) = delete;\n    myThread&amp; operator=(const myThread&amp;) = delete;\nprivate:\n    thread&amp; _t;\n};<\/code><\/pre>\n\n\n\n<p><strong>\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u5f53\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8c61\u540e\uff0c\u5c31\u7528myThread\u7c7b\u5bf9\u5176\u8fdb\u884c\u5c01\u88c5\u4ea7\u751f\u4e00\u4e2amyThread\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u5f53myThread\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\u5c31\u4f1a\u8c03\u7528\u6790\u6784\u51fd\u6570\uff0c\u5728\u6790\u6784\u4e2d\u4f1a\u901a\u8fc7<code>joinable<\/code>\u5224\u65ad\u8fd9\u4e2a\u7ebf\u7a0b\u662f\u5426\u9700\u8981\u88ab<code>join<\/code>\uff0c\u5982\u679c\u9700\u8981\u90a3\u4e48\u5c31\u4f1a\u8c03\u7528<code>join<\/code>\u5bf9\u5176\u8be5\u7ebf\u7a0b\u8fdb\u884c\u7b49\u5f85\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f8b\u5982\u521a\u624d\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528myThread\u7c7b\u5bf9\u7ebf\u7a0b\u5bf9\u8c61\u8fdb\u884c\u5c01\u88c5\u540e\uff0c\u5c31\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u4e00\u5b9a\u4f1a\u88ab<code>join<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n    thread t(func, 20);\n    myThread mt(t); \/\/\u4f7f\u7528myThread\u5bf9\u7ebf\u7a0b\u5bf9\u8c61\u8fdb\u884c\u5c01\u88c5\n\n    \/\/...\n    if (!DoSomething())\n        return -1;\n    \/\/...\n\n    t.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>detach\u65b9\u5f0f<\/p>\n<\/blockquote>\n\n\n\n<p>\u4e3b\u7ebf\u7a0b\u521b\u5efa\u65b0\u7ebf\u7a0b\u540e\uff0c\u4e5f\u53ef\u4ee5\u8c03\u7528<code>detach<\/code>\u51fd\u6570\u5c06\u65b0\u7ebf\u7a0b\u4e0e\u4e3b\u7ebf\u7a0b\u8fdb\u884c\u5206\u79bb\uff0c\u5206\u79bb\u540e\u65b0\u7ebf\u7a0b\u4f1a\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u5176\u6240\u6709\u6743\u548c\u63a7\u5236\u6743\u5c06\u4f1a\u4ea4\u7ed9C++\u8fd0\u884c\u5e93\uff0c\u6b64\u65f6C++\u8fd0\u884c\u5e93\u4f1a\u4fdd\u8bc1\u5f53\u7ebf\u7a0b\u9000\u51fa\u65f6\uff0c\u5176\u76f8\u5173\u8d44\u6e90\u80fd\u591f\u88ab\u6b63\u786e\u56de\u6536\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>detach<\/code>\u7684\u65b9\u5f0f\u56de\u6536\u7ebf\u7a0b\u7684\u8d44\u6e90\uff0c\u4e00\u822c\u5728\u7ebf\u7a0b\u5bf9\u8c61\u521b\u5efa\u597d\u4e4b\u540e\u5c31\u7acb\u5373\u8c03\u7528<code>detach<\/code>\u51fd\u6570\u3002<\/li>\n\n\n\n<li>\u5426\u5219\u7ebf\u7a0b\u5bf9\u8c61\u53ef\u80fd\u4f1a\u56e0\u4e3a\u67d0\u4e9b\u539f\u56e0\uff0c\u5728\u540e\u7eed\u8c03\u7528<code>detach<\/code>\u51fd\u6570\u5206\u79bb\u7ebf\u7a0b\u4e4b\u524d\u88ab\u9500\u6bc1\u6389\uff0c\u8fd9\u65f6\u5c31\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n\n\n\n<li>\u56e0\u4e3a\u5f53\u7ebf\u7a0b\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u4f1a\u8c03\u7528thread\u7684\u6790\u6784\u51fd\u6570\uff0c\u800c\u5728thread\u7684\u6790\u6784\u51fd\u6570\u4e2d\u4f1a\u901a\u8fc7<code>joinable<\/code>\u5224\u65ad\u8fd9\u4e2a\u7ebf\u7a0b\u662f\u5426\u9700\u8981\u88ab<code>join<\/code>\uff0c\u5982\u679c\u9700\u8981\u90a3\u4e48\u5c31\u4f1a\u8c03\u7528<code>terminate<\/code>\u7ec8\u6b62\u5f53\u524d\u7a0b\u5e8f\uff08\u7a0b\u5e8f\u5d29\u6e83\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e92\u65a5\u91cf\u5e93\uff08<a href=\"https:\/\/so.csdn.net\/so\/search?q=mutex&amp;spm=1001.2101.3001.7020\">mutex<\/a>\uff09<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">mutex\u7684\u79cd\u7c7b<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u56db\u79cd\u4e92\u65a5\u91cf<\/p>\n<\/blockquote>\n\n\n\n<p>\u5728C++11\u4e2d\uff0cmutex\u4e2d\u603b\u5171\u5305\u4e86\u56db\u79cd\u4e92\u65a5\u91cf\uff1a<\/p>\n\n\n\n<p><strong>1\u3001std::mute<\/strong><br>mutex\u9501\u662fC++11\u63d0\u4f9b\u7684\u6700\u57fa\u672c\u7684\u4e92\u65a5\u91cf\uff0cmutex\u5bf9\u8c61\u4e4b\u95f4\u4e0d\u80fd\u8fdb\u884c\u62f7\u8d1d\uff0c\u4e5f\u4e0d\u80fd\u8fdb\u884c\u79fb\u52a8\u3002<\/p>\n\n\n\n<p>mutex\u4e2d\u5e38\u7528\u7684\u6210\u5458\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">\u6210\u5458\u51fd\u6570<\/th><th class=\"has-text-align-center\" data-align=\"center\">\u529f\u80fd<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">lock<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5bf9\u4e92\u65a5\u91cf\u8fdb\u884c\u52a0\u9501<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">try_lock<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5c1d\u8bd5\u5bf9\u4e92\u65a5\u91cf\u8fdb\u884c\u52a0\u9501<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">unlock<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u5bf9\u4e92\u65a5\u91cf\u8fdb\u884c\u89e3\u9501\uff0c\u91ca\u653e\u4e92\u65a5\u91cf\u7684\u6240\u6709\u6743<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7ebf\u7a0b\u51fd\u6570\u8c03\u7528<code>lock<\/code>\u65f6\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u5f53\u524d\u6ca1\u6709\u88ab\u5176\u4ed6\u7ebf\u7a0b\u9501\u4f4f\uff0c\u5219\u8c03\u7528\u7ebf\u7a0b\u5c06\u8be5\u4e92\u65a5\u91cf\u9501\u4f4f\uff0c\u76f4\u5230\u8c03\u7528<code>unlock<\/code>\u4e4b\u524d\uff0c\u8be5\u7ebf\u7a0b\u4e00\u81f4\u62e5\u6709\u8be5\u9501\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u5df2\u7ecf\u88ab\u5176\u4ed6\u7ebf\u7a0b\u9501\u4f4f\uff0c\u5219\u5f53\u524d\u7684\u8c03\u7528\u7ebf\u7a0b\u4f1a\u88ab\u963b\u585e\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u88ab\u5f53\u524d\u8c03\u7528\u7ebf\u7a0b\u9501\u4f4f\uff0c\u5219\u4f1a\u4ea7\u751f\u6b7b\u9501\uff08deadlock\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7ebf\u7a0b\u8c03\u7528<code>try_lock<\/code>\u65f6\uff0c\u7c7b\u4f3c\u4e5f\u53ef\u80fd\u4f1a\u53d1\u751f\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u5f53\u524d\u6ca1\u6709\u88ab\u5176\u4ed6\u7ebf\u7a0b\u9501\u4f4f\uff0c\u5219\u8c03\u7528\u7ebf\u7a0b\u5c06\u8be5\u4e92\u65a5\u91cf\u9501\u4f4f\uff0c\u76f4\u5230\u8c03\u7528<code>unlock<\/code>\u4e4b\u524d\uff0c\u8be5\u7ebf\u7a0b\u4e00\u81f4\u62e5\u6709\u8be5\u9501\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u5df2\u7ecf\u88ab\u5176\u4ed6\u7ebf\u7a0b\u9501\u4f4f\uff0c\u5219<code>try_lock<\/code>\u8c03\u7528\u8fd4\u56defalse\uff0c\u5f53\u524d\u7684\u8c03\u7528\u7ebf\u7a0b\u4e0d\u4f1a\u88ab\u963b\u585e\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u8be5\u4e92\u65a5\u91cf\u88ab\u5f53\u524d\u8c03\u7528\u7ebf\u7a0b\u9501\u4f4f\uff0c\u5219\u4f1a\u4ea7\u751f\u6b7b\u9501\uff08deadlock\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2\u3001std::recursive_mutex<\/strong><\/p>\n\n\n\n<p>recursive_mutex\u53eb\u505a\u9012\u5f52\u4e92\u65a5\u9501\uff0c\u8be5\u9501\u4e13\u95e8\u7528\u4e8e\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u52a0\u9501\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u4f7f\u7528mutex\u4e92\u65a5\u9501\u8fdb\u884c\u52a0\u9501\uff0c\u90a3\u4e48\u5728\u7ebf\u7a0b\u8fdb\u884c\u9012\u5f52\u8c03\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u91cd\u590d\u7533\u8bf7\u5df2\u7ecf\u7533\u8bf7\u5230\u4f46\u81ea\u5df1\u8fd8\u672a\u91ca\u653e\u7684\u9501\uff0c\u8fdb\u800c\u5bfc\u81f4\u6b7b\u9501\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u800crecursive_mutex\u5141\u8bb8\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u4e92\u65a5\u91cf\u591a\u6b21\u4e0a\u9501\uff08\u5373\u9012\u5f52\u4e0a\u9501\uff09\uff0c\u6765\u83b7\u5f97\u4e92\u65a5\u91cf\u5bf9\u8c61\u7684\u591a\u5c42\u6240\u6709\u6743\uff0c\u4f46\u662f\u91ca\u653e\u4e92\u65a5\u91cf\u65f6\u9700\u8981\u8c03\u7528\u4e0e\u8be5\u9501\u5c42\u6b21\u6df1\u5ea6\u76f8\u540c\u6b21\u6570\u7684<code>unlock<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\uff0crecursive_mutex\u4e5f\u63d0\u4f9b\u4e86<code>lock<\/code>\u3001<code>try_lock<\/code>\u548c<code>unlock<\/code>\u6210\u5458\u51fd\u6570\uff0c\u5176\u7684\u7279\u6027\u4e0emutex\u5927\u81f4\u76f8\u540c\u3002<\/p>\n\n\n\n<p><strong>3\u3001std::timed_mutex<\/strong><\/p>\n\n\n\n<p>timed_mutex\u4e2d\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4e24\u4e2a\u6210\u5458\u51fd\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>try_lock_for<\/code>\uff1a\u63a5\u53d7\u4e00\u4e2a\u65f6\u95f4\u8303\u56f4\uff0c\u8868\u793a\u5728\u8fd9\u4e00\u6bb5\u65f6\u95f4\u8303\u56f4\u4e4b\u5185\u7ebf\u7a0b\u5982\u679c\u6ca1\u6709\u83b7\u5f97\u9501\u5219\u88ab\u963b\u585e\u4f4f\uff0c\u5982\u679c\u5728\u6b64\u671f\u95f4\u5176\u4ed6\u7ebf\u7a0b\u91ca\u653e\u4e86\u9501\uff0c\u5219\u8be5\u7ebf\u7a0b\u53ef\u4ee5\u83b7\u5f97\u5bf9\u4e92\u65a5\u91cf\u7684\u9501\uff0c\u5982\u679c\u8d85\u65f6\uff08\u5373\u5728\u6307\u5b9a\u65f6\u95f4\u4e4b\u5185\u8fd8\u662f\u6ca1\u6709\u83b7\u5f97\u9501\uff09\uff0c\u5219\u8fd4\u56defalse\u3002<\/li>\n\n\n\n<li><code>try_lock_untill<\/code>\uff1a\u63a5\u53d7\u4e00\u4e2a\u65f6\u95f4\u70b9\u4f5c\u4e3a\u53c2\u6570\uff0c\u5728\u6307\u5b9a\u65f6\u95f4\u70b9\u672a\u5230\u6765\u4e4b\u524d\u7ebf\u7a0b\u5982\u679c\u6ca1\u6709\u83b7\u5f97\u9501\u5219\u88ab\u963b\u585e\u4f4f\uff0c\u5982\u679c\u5728\u6b64\u671f\u95f4\u5176\u4ed6\u7ebf\u7a0b\u91ca\u653e\u4e86\u9501\uff0c\u5219\u8be5\u7ebf\u7a0b\u53ef\u4ee5\u83b7\u5f97\u5bf9\u4e92\u65a5\u91cf\u7684\u9501\uff0c\u5982\u679c\u8d85\u65f6\uff08\u5373\u5728\u6307\u5b9a\u65f6\u95f4\u70b9\u5230\u6765\u65f6\u8fd8\u662f\u6ca1\u6709\u83b7\u5f97\u9501\uff09\uff0c\u5219\u8fd4\u56defalse\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\uff0ctimed_mutex\u4e5f\u63d0\u4f9b\u4e86<code>lock<\/code>\u3001<code>try_lock<\/code>\u548c<code>unlock<\/code>\u6210\u5458\u51fd\u6570\uff0c\u5176\u7684\u7279\u6027\u4e0emutex\u76f8\u540c\u3002<\/p>\n\n\n\n<p><strong>4\u3001std::recursive_timed_mutex<\/strong><\/p>\n\n\n\n<p>recursive_timed_mutex\u5c31\u662frecursive_mutex\u548ctimed_mutex\u7684\u7ed3\u5408\uff0crecursive_timed_mutex\u65e2\u652f\u6301\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u8fdb\u884c\u52a0\u9501\u64cd\u4f5c\uff0c\u4e5f\u652f\u6301\u5b9a\u65f6\u5c1d\u8bd5\u7533\u8bf7\u9501\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u52a0\u9501\u793a\u4f8b<\/p>\n<\/blockquote>\n\n\n\n<p>\u5728\u6ca1\u6709\u4f7f\u7528\u4e92\u65a5\u9501\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u7684\u60c5\u51b5\u4e0b\uff0c\u8ba9\u4e24\u4e2a\u7ebf\u7a0b\u5404\u81ea\u6253\u53701-100\u7684\u6570\u5b57\uff0c\u5c31\u4f1a\u5bfc\u81f4\u63a7\u5236\u53f0\u8f93\u51fa\u9519\u4e71\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n)\n{\n    for (int i = 1; i &lt;= n; i++)\n    {\n        cout &lt;&lt; i &lt;&lt; endl;\n    }\n}\nint main()\n{\n    thread t1(func, 100);\n    thread t2(func, 100);\n\n    t1.join();\n    t2.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u8981\u8ba9\u4e24\u4e2a\u7ebf\u7a0b\u7684\u8f93\u51fa\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\uff0c\u5373\u4e0d\u4f1a\u8ba9\u67d0\u4e00\u6b21\u8f93\u51fa\u4e2d\u9014\u88ab\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6253\u65ad\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u7528\u4e92\u65a5\u9501\u5bf9\u6253\u5370\u8fc7\u7a0b\u8fdb\u884c\u4fdd\u62a4\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u52a0\u9501\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u5728for\u5faa\u73af\u4f53\u5185\u8fdb\u884c\u52a0\u9501\uff0c\u4e00\u79cd\u662f\u5728for\u5faa\u73af\u4f53\u5916\u8fdb\u884c\u52a0\u9501\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int n, mutex&amp; mtx)\n{\n    mtx.lock(); \/\/for\u5faa\u73af\u4f53\u5916\u52a0\u9501\n    for (int i = 1; i &lt;= n; i++)\n    {\n        \/\/mtx.lock(); \/\/for\u5faa\u73af\u4f53\u5185\u52a0\u9501\n        cout &lt;&lt; i &lt;&lt; endl;\n        \/\/mtx.unlock();\n    }\n    mtx.unlock();\n}\nint main()\n{\n    mutex mtx;\n    thread t1(func, 100, ref(mtx));\n    thread t2(func, 100, ref(mtx));\n\n    t1.join();\n    t2.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\u4e00\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6b64\u5904\u5728for\u5faa\u73af\u4f53\u5916\u52a0\u9501\u6bd4\u5728for\u5faa\u73af\u4f53\u5185\u52a0\u9501\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5728for\u5faa\u73af\u4f53\u5185\u52a0\u9501\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u6253\u5370\u6570\u5b57\u65f6\u9891\u7e41\u8fdb\u884c\u52a0\u9501\u89e3\u9501\u64cd\u4f5c\uff0c\u800c\u5982\u679c\u5728for\u5faa\u73af\u4f53\u5916\u52a0\u9501\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u53ea\u9700\u8981\u5728\u5f00\u59cb\u6253\u53701\u4e4b\u524d\u8fdb\u884c\u4e00\u6b21\u52a0\u9501\uff0c\u5728\u6253\u5370\u5b8c100\u540e\u8fdb\u884c\u4e00\u6b21\u89e3\u9501\u5c31\u884c\u4e86\u3002<\/li>\n\n\n\n<li>\u5728for\u5faa\u73af\u4f53\u5916\u52a0\u9501\u4e5f\u5c31\u610f\u5473\u7740\u4e24\u4e2a\u7ebf\u7a0b\u7684\u6253\u5370\u8fc7\u7a0b\u53d8\u6210\u4e86\u4e32\u884c\u7684\uff0c\u5373\u4e00\u4e2a\u7ebf\u7a0b\u6253\u5370\u5b8c1-100\u540e\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u518d\u6253\u5370\uff0c\u4f46\u8fd9\u65f6\u6253\u5370\u6548\u7387\u63d0\u9ad8\u4e86\uff0c\u56e0\u4e3a\u907f\u514d\u4e86\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u95f4\u7684\u9891\u7e41\u5207\u6362\u3002<\/li>\n\n\n\n<li>\u4e3a\u4e86\u4fdd\u8bc1\u4e24\u4e2a\u7ebf\u7a0b\u4f7f\u7528\u7684\u662f\u540c\u4e00\u4e2a\u4e92\u65a5\u9501\uff0c\u7ebf\u7a0b\u51fd\u6570\u5fc5\u987b\u4ee5\u5f15\u7528\u7684\u65b9\u5f0f\u63a5\u6536\u4f20\u5165\u7684\u4e92\u65a5\u9501\uff0c\u5e76\u4e14\u5728\u4f20\u53c2\u65f6\u9700\u8981\u4f7f\u7528ref\u51fd\u6570\u4fdd\u6301\u5bf9\u4e92\u65a5\u9501\u7684\u5f15\u7528\u3002<\/li>\n\n\n\n<li>\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u5c06\u4e92\u65a5\u9501\u5b9a\u4e49\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u6216\u662f\u7528lambda\u8868\u8fbe\u5f0f\u5b9a\u4e49\u7ebf\u7a0b\u51fd\u6570\uff0c\u7136\u540e\u4ee5\u5f15\u7528\u7684\u65b9\u5f0f\u5c06\u5c40\u90e8\u7684\u4e92\u65a5\u9501\u8fdb\u884c\u6355\u6349\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e5f\u80fd\u4fdd\u8bc1\u4e24\u4e2a\u7ebf\u7a0b\u4f7f\u7528\u7684\u662f\u540c\u4e00\u4e2a\u4e92\u65a5\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7ecf\u9a8c\u5206\u4eab\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9879\u76ee\u4e2d\u5b9e\u9645\u4e0d\u592a\u5efa\u8bae\u5b9a\u4e49\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u5168\u5c40\u53d8\u91cf\u5982\u679c\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u5f53\u8fd9\u4e2a\u5934\u6587\u4ef6\u88ab\u591a\u4e2a\u6e90\u6587\u4ef6\u5305\u542b\u65f6\uff0c\u5728\u8fd9\u591a\u4e2a\u6e90\u6587\u4ef6\u4e2d\u90fd\u4f1a\u5bf9\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\u8fdb\u884c\u5b9a\u4e49\uff0c\u8fd9\u65f6\u5c31\u4f1a\u5bfc\u81f4\u53d8\u91cf\u91cd\u5b9a\u4e49\uff0c\u4f46\u5982\u679c\u5c06\u5168\u5c40\u53d8\u91cf\u5b9a\u4e49\u4e3a\u9759\u6001\uff0c\u90a3\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\u5c31\u53ea\u5728\u5f53\u524d\u6587\u4ef6\u53ef\u89c1\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u786e\u5b9e\u6709\u4e00\u4e9b\u53d8\u91cf\u9700\u8981\u5728\u591a\u4e2a\u6587\u4ef6\u4e2d\u4f7f\u7528\uff0c\u90a3\u4e48\u4e00\u822c\u5efa\u8bae\u5c06\u8fd9\u4e9b\u53d8\u91cf\u5c01\u88c5\u5230\u4e00\u4e2a\u7c7b\u5f53\u4e2d\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u7c7b\u8bbe\u8ba1\u6210\u5355\u4f8b\u6a21\u5f0f\uff0c\u5f53\u9700\u8981\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u65f6\u5c31\u901a\u8fc7\u8fd9\u4e2a\u5355\u4f8b\u5bf9\u8c61\u53bb\u8bbf\u95ee\u5373\u53ef\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/so.csdn.net\/so\/search?q=lock_guard&amp;spm=1001.2101.3001.7020\">lock_guard<\/a>\u548cunique_lock<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4f7f\u7528\u4e92\u65a5\u9501\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898<\/p>\n<\/blockquote>\n\n\n\n<p>\u4f7f\u7528\u4e92\u65a5\u9501\u65f6\uff0c\u5982\u679c\u52a0\u9501\u7684\u8303\u56f4\u592a\u5927\uff0c\u90a3\u4e48\u6781\u6709\u53ef\u80fd\u5728\u4e2d\u9014\u8fd4\u56de\u65f6\u5fd8\u8bb0\u4e86\u89e3\u9501\uff0c\u6b64\u540e\u7533\u8bf7\u8fd9\u4e2a\u4e92\u65a5\u9501\u7684\u7ebf\u7a0b\u5c31\u4f1a\u88ab\u963b\u585e\u4f4f\uff0c\u4e5f\u5c31\u662f\u9020\u6210\u4e86\u6b7b\u9501\u95ee\u9898\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mutex mtx;\nvoid func()\n{\n    mtx.lock();\n    \/\/...\n    FILE* fout = fopen(\"data.txt\", \"r\");\n    if (fout == nullptr)\n    {\n        \/\/...\n        return; \/\/\u4e2d\u9014\u8fd4\u56de\uff08\u672a\u89e3\u9501\uff09\n    }\n    \/\/...\n    mtx.unlock();\n}\nint main()\n{\n    func();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u56e0\u6b64\u4f7f\u7528\u4e92\u65a5\u9501\u65f6\u5982\u679c\u63a7\u5236\u4e0d\u597d\u5c31\u4f1a\u9020\u6210\u6b7b\u9501\uff0c\u6700\u5e38\u89c1\u7684\u5c31\u662f\u6b64\u5904\u5728\u9501\u4e2d\u95f4\u4ee3\u7801\u8fd4\u56de\uff0c\u6b64\u5916\u8fd8\u6709\u4e00\u4e2a\u6bd4\u8f83\u5e38\u89c1\u7684\u60c5\u51b5\u5c31\u662f\u5728\u9501\u7684\u8303\u56f4\u5185\u629b\u5f02\u5e38\uff0c\u4e5f\u5f88\u5bb9\u6613\u5bfc\u81f4\u6b7b\u9501\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64C++11\u91c7\u7528RAII\u7684\u65b9\u5f0f\u5bf9\u9501\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u4e8e\u662f\u5c31\u51fa\u73b0\u4e86lock_guard\u548cunique_lock\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>lock_guard<\/p>\n<\/blockquote>\n\n\n\n<p>lock_guard\u662fC++11\u4e2d\u7684\u4e00\u4e2a\u6a21\u677f\u7c7b\uff0c\u5176\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;class Mutex&gt;\nclass lock_guard;<\/code><\/pre>\n\n\n\n<p>lock_guard\u7c7b\u6a21\u677f\u4e3b\u8981\u662f\u901a\u8fc7RAII\u7684\u65b9\u5f0f\uff0c\u5bf9\u5176\u7ba1\u7406\u7684\u4e92\u65a5\u9501\u8fdb\u884c\u4e86\u5c01\u88c5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9700\u8981\u52a0\u9501\u7684\u5730\u65b9\uff0c\u7528\u4e92\u65a5\u9501\u5b9e\u4f8b\u5316\u4e00\u4e2alock_guard\u5bf9\u8c61\uff0c\u5728lock_guard\u7684\u6784\u9020\u51fd\u6570\u4e2d\u4f1a\u8c03\u7528<code>lock<\/code>\u8fdb\u884c\u52a0\u9501\u3002<\/li>\n\n\n\n<li>\u5f53lock_guard\u5bf9\u8c61\u51fa\u4f5c\u7528\u57df\u524d\u4f1a\u8c03\u7528\u6790\u6784\u51fd\u6570\uff0c\u5728lock_guard\u7684\u6790\u6784\u51fd\u6570\u4e2d\u4f1a\u8c03\u7528<code>unlock<\/code>\u81ea\u52a8\u89e3\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u6784\u9020\u5bf9\u8c61\u65f6\u52a0\u9501\uff0c\u6790\u6784\u5bf9\u8c61\u65f6\u81ea\u52a8\u89e3\u9501\u7684\u65b9\u5f0f\u5c31\u6709\u6548\u7684\u907f\u514d\u4e86\u6b7b\u9501\u95ee\u9898\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mutex mtx;\nvoid func()\n{\n    lock_guard&lt;mutex&gt; lg(mtx); \/\/\u8c03\u7528\u6784\u9020\u51fd\u6570\u52a0\u9501\n    \/\/...\n    FILE* fout = fopen(\"data.txt\", \"r\");\n    if (fout == nullptr)\n    {\n        \/\/...\n        return; \/\/\u8c03\u7528\u6790\u6784\u51fd\u6570\u89e3\u9501\n    }\n    \/\/...\n} \/\/\u8c03\u7528\u6790\u6784\u51fd\u6570\u89e3\u9501\nint main()\n{\n    func();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u4ecelock_guard\u5bf9\u8c61\u5b9a\u4e49\u5230\u8be5\u5bf9\u8c61\u6790\u6784\uff0c\u8fd9\u6bb5\u533a\u57df\u7684\u4ee3\u7801\u90fd\u5c5e\u4e8e\u4e92\u65a5\u9501\u7684\u4fdd\u62a4\u8303\u56f4\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u53ea\u60f3\u7528lock_guard\u4fdd\u62a4\u67d0\u4e00\u6bb5\u4ee3\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u533f\u540d\u7684\u5c40\u90e8\u57df\u6765\u63a7\u5236lock_guard\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mutex mtx;\nvoid func()\n{\n    \/\/...\n    \/\/\u533f\u540d\u5c40\u90e8\u57df\n    {\n        lock_guard&lt;mutex&gt; lg(mtx); \/\/\u8c03\u7528\u6784\u9020\u51fd\u6570\u52a0\u9501\n        FILE* fout = fopen(\"data.txt\", \"r\");\n        if (fout == nullptr)\n        {\n            \/\/...\n            return; \/\/\u8c03\u7528\u6790\u6784\u51fd\u6570\u89e3\u9501\n        }\n    } \/\/\u8c03\u7528\u6790\u6784\u51fd\u6570\u89e3\u9501\n    \/\/...\n}\nint main()\n{\n    func();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6a21\u62df\u5b9e\u73b0lock_guard<\/p>\n<\/blockquote>\n\n\n\n<p>\u6a21\u62df\u5b9e\u73b0lock_guard\u7c7b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>lock_guard\u7c7b\u4e2d\u5305\u542b\u4e00\u4e2a\u9501\u6210\u5458\u53d8\u91cf\uff08\u5f15\u7528\u7c7b\u578b\uff09\uff0c\u8fd9\u4e2a\u9501\u5c31\u662f\u6bcf\u4e2alock_guard\u5bf9\u8c61\u7ba1\u7406\u7684\u4e92\u65a5\u9501\u3002<\/li>\n\n\n\n<li>\u8c03\u7528lock_guard\u7684\u6784\u9020\u51fd\u6570\u65f6\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u88ab\u7ba1\u7406\u4e92\u65a5\u9501\uff0c\u7528\u8be5\u4e92\u65a5\u9501\u6765\u521d\u59cb\u5316\u9501\u6210\u5458\u53d8\u91cf\u540e\uff0c\u8c03\u7528\u4e92\u65a5\u9501\u7684<code>lock<\/code>\u51fd\u6570\u8fdb\u884c\u52a0\u9501\u3002<\/li>\n\n\n\n<li>lock_guard\u7684\u6790\u6784\u51fd\u6570\u4e2d\u8c03\u7528\u4e92\u65a5\u9501\u7684<code>unlock<\/code>\u8fdb\u884c\u89e3\u9501\u3002<\/li>\n\n\n\n<li>\u9700\u8981\u5220\u9664lock_guard\u7c7b\u7684\u62f7\u8d1d\u6784\u9020\u548c\u62f7\u8d1d\u8d4b\u503c\uff0c\u56e0\u4e3alock_guard\u7c7b\u4e2d\u7684\u9501\u6210\u5458\u53d8\u91cf\u672c\u8eab\u4e5f\u662f\u4e0d\u652f\u6301\u62f7\u8d1d\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace cl\n{\n    template&lt;class Mutex&gt;\n    class lock_guard\n    {\n    public:\n        lock_guard(Mutex&amp; mtx)\n            :_mtx(mtx)\n        {\n            mtx.lock(); \/\/\u52a0\u9501\n        }\n        ~lock_guard()\n        {\n            mtx.unlock(); \/\/\u89e3\u9501\n        }\n        lock_guard(const lock_guard&amp;) = delete;\n        lock_guard&amp; operator=(const lock_guard&amp;) = delete;\n    private:\n        Mutex&amp; _mtx;\n    };\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>unique_lock<\/p>\n<\/blockquote>\n\n\n\n<p>\u4f46\u7531\u4e8elock_guard\u592a\u5355\u4e00\uff0c\u7528\u6237\u6ca1\u6709\u529e\u6cd5\u5bf9\u9501\u8fdb\u884c\u63a7\u5236\uff0c\u56e0\u6b64C++11\u53c8\u63d0\u4f9b\u4e86unique_lock\u3002<\/p>\n\n\n\n<p>unique_lock\u4e0elock_guard\u7c7b\u4f3c\uff0cunique_lock\u7c7b\u6a21\u677f\u4e5f\u662f\u91c7\u7528RAII\u7684\u65b9\u5f0f\u5bf9\u9501\u8fdb\u884c\u4e86\u5c01\u88c5\u3002\u5728\u521b\u5efaunique_lock\u5bf9\u8c61\u8c03\u7528\u6784\u9020\u51fd\u6570\u65f6\u4e5f\u4f1a\u8c03\u7528lock\u8fdb\u884c\u52a0\u9501\uff0c\u5728unique_lock\u5bf9\u8c61\u9500\u6bc1\u8c03\u7528\u6790\u6784\u51fd\u6570\u65f6\u4e5f\u4f1a\u8c03\u7528unlock\u8fdb\u884c\u89e3\u9501\u3002<\/p>\n\n\n\n<p>\u4f46lock_guard\u4e0d\u540c\u7684\u662f\uff0cunique_lock\u66f4\u52a0\u7684\u7075\u6d3b\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u6210\u5458\u51fd\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u52a0\u9501\/\u89e3\u9501\u64cd\u4f5c\uff1alock\u3001try_lock\u3001try_lock_for\u3001try_lock_until\u548cunlock\u3002<\/li>\n\n\n\n<li>\u4fee\u6539\u64cd\u4f5c\uff1a\u79fb\u52a8\u8d4b\u503c\u3001swap\u3001release\uff08\u8fd4\u56de\u5b83\u6240\u7ba1\u7406\u7684\u4e92\u65a5\u91cf\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u5e76\u91ca\u653e\u6240\u6709\u6743\uff09\u3002<\/li>\n\n\n\n<li>\u83b7\u53d6\u5c5e\u6027\uff1aowns_lock\uff08\u8fd4\u56de\u5f53\u524d\u5bf9\u8c61\u662f\u5426\u4e0a\u4e86\u9501\uff09\u3001operator bool\uff08\u4e0eowns_lock\u7684\u529f\u80fd\u76f8\u540c\uff09\u3001mutex\uff08\u8fd4\u56de\u5f53\u524dunique_lock\u6240\u7ba1\u7406\u7684\u4e92\u65a5\u91cf\u7684\u6307\u9488\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6bd4\u5982\u5982\u4e0b\u573a\u666f\u5c31\u9002\u5408\u4f7f\u7528unique_lock\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8981\u7528\u4e92\u65a5\u9501\u4fdd\u62a4\u51fd\u65701\u7684\u5927\u90e8\u5206\u4ee3\u7801\uff0c\u4f46\u662f\u4e2d\u95f4\u6709\u4e00\u5c0f\u5757\u4ee3\u7801\u8c03\u7528\u4e86\u51fd\u65702\uff0c\u800c\u8c03\u7528\u51fd\u65702\u65f6\u4e0d\u9700\u8981\u7528\u51fd\u65701\u4e2d\u7684\u4e92\u65a5\u9501\u8fdb\u884c\u4fdd\u62a4\uff0c\u51fd\u65702\u5185\u90e8\u7684\u4ee3\u7801\u7531\u5176\u4ed6\u4e92\u65a5\u9501\u8fdb\u884c\u4fdd\u62a4\u3002<\/li>\n\n\n\n<li>\u56e0\u6b64\u5728\u8c03\u7528\u51fd\u65702\u4e4b\u524d\u9700\u8981\u5bf9\u5f53\u524d\u4e92\u65a5\u9501\u8fdb\u884c\u89e3\u9501\uff0c\u5f53\u51fd\u65702\u8c03\u7528\u8fd4\u56de\u540e\u518d\u8fdb\u884c\u52a0\u9501\uff0c\u8fd9\u6837\u5f53\u8c03\u7528\u51fd\u65702\u65f6\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528\u51fd\u65701\u5c31\u80fd\u591f\u83b7\u53d6\u5230\u8fd9\u4e2a\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u4e0b\u56fe\uff1a<img decoding=\"async\" src=\"https:\/\/cdn.codergodv.xyz\/3fbdef9bb1ffe308160af753e91bce4c.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u539f\u5b50\u6027\u64cd\u4f5c\u5e93\uff08atomic\uff09<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/p>\n<\/blockquote>\n\n\n\n<p>\u591a\u7ebf\u7a0b\u6700\u4e3b\u8981\u7684\u95ee\u9898\u662f\u5171\u4eab\u6570\u636e\u5e26\u6765\u7684\u95ee\u9898\uff08\u5373\u7ebf\u7a0b\u5b89\u5168\uff09\u3002\u5982\u679c\u5171\u4eab\u6570\u636e\u90fd\u662f\u53ea\u8bfb\u7684\uff0c\u90a3\u4e48\u6ca1\u95ee\u9898\uff0c\u56e0\u4e3a\u53ea\u8bfb\u64cd\u4f5c\u4e0d\u4f1a\u5f71\u54cd\u5230\u6570\u636e\uff0c\u66f4\u4e0d\u4f1a\u6d89\u53ca\u5bf9\u6570\u636e\u7684\u4fee\u6539\uff0c\u6240\u4ee5\u6240\u6709\u7ebf\u7a0b\u90fd\u4f1a\u83b7\u5f97\u540c\u6837\u7684\u6570\u636e\u3002\u4f46\u662f\uff0c\u5f53\u4e00\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u8981\u4fee\u6539\u5171\u4eab\u6570\u636e\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f\u5f88\u591a\u6f5c\u5728\u7684\u9ebb\u70e6\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int&amp; n, int times)\n{\n    for (int i = 0; i &lt; times; i++)\n    {\n        n++;\n    }\n}\nint main()\n{\n    int n = 0;\n    int times = 100000; \/\/\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9n++\u7684\u6b21\u6570\n    thread t1(func, ref(n), times);\n    thread t2(func, ref(n), times);\n\n    t1.join();\n    t2.join();\n    cout &lt;&lt; n &lt;&lt; endl; \/\/\u6253\u5370n\u7684\u503c\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u5206\u522b\u8ba9\u4e24\u4e2a\u7ebf\u7a0b\u5bf9\u540c\u4e00\u4e2a\u53d8\u91cfn\u8fdb\u884c\u4e86100000\u6b21<code>++<\/code>\u64cd\u4f5c\uff0c\u7406\u8bba\u4e0a\u6700\u7ec8n\u7684\u503c\u5e94\u8be5\u662f200000\uff0c\u4f46\u6700\u7ec8\u6253\u5370\u51fan\u7684\u503c\u5374\u662f\u5c0f\u4e8e200000\u7684\u3002<\/p>\n\n\n\n<p>\u6839\u672c\u539f\u56e0\u5c31\u662f<code>++<\/code>\u64cd\u4f5c\u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\uff0c\u8be5\u64cd\u4f5c\u5206\u4e3a\u4e09\u6b65\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>load<\/code>\uff1a\u5c06\u5171\u4eab\u53d8\u91cfn\u4ece\u5185\u5b58\u52a0\u8f7d\u5230\u5bc4\u5b58\u5668\u4e2d\u3002<\/li>\n\n\n\n<li><code>update<\/code>\uff1a\u66f4\u65b0\u5bc4\u5b58\u5668\u91cc\u9762\u7684\u503c\uff0c\u6267\u884c+1\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><code>store<\/code>\uff1a\u5c06\u65b0\u503c\u4ece\u5bc4\u5b58\u5668\u5199\u56de\u5171\u4eab\u53d8\u91cfn\u7684\u5185\u5b58\u5730\u5740\u3002<\/li>\n<\/ul>\n\n\n\n<p><code>++<\/code>\u64cd\u4f5c\u5bf9\u5e94\u7684\u6c47\u7f16\u4ee3\u7801\u5982\u4e0b\uff1a<img decoding=\"async\" src=\"https:\/\/cdn.codergodv.xyz\/ecbe18ee3a6b875a40447f134e0a6440.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n\n\n\n<p>\u56e0\u6b64\u53ef\u80fd\u5f53\u7ebf\u7a0b1\u521a\u5c06n\u7684\u503c\u52a0\u8f7d\u5230\u5bc4\u5b58\u5668\u4e2d\u5c31\u88ab\u5207\u8d70\u4e86\uff0c\u4e5f\u5c31\u662f\u53ea\u5b8c\u6210\u4e86<code>++<\/code>\u64cd\u4f5c\u7684\u7b2c\u4e00\u6b65\uff0c\u800c\u7ebf\u7a0b2\u53ef\u80fd\u987a\u5229\u5b8c\u6210\u4e86\u4e00\u6b21\u5b8c\u6574\u7684<code>++<\/code>\u64cd\u4f5c\u624d\u88ab\u5207\u8d70\uff0c\u800c\u8fd9\u65f6\u7ebf\u7a0b1\u7ee7\u7eed\u7528\u4e4b\u524d\u52a0\u8f7d\u5230\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\u5b8c\u6210\u5269\u4f59\u7684\u4e24\u6b65\u64cd\u4f5c\uff0c\u6700\u7ec8\u5c31\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u7ebf\u7a0b\u5206\u522b\u5bf9\u5171\u4eab\u53d8\u91cfn\u8fdb\u884c\u4e86\u4e00\u6b21<code>++<\/code>\u64cd\u4f5c\uff0c\u4f46\u6700\u7ec8n\u7684\u503c\u5374\u53ea\u88ab<code>++<\/code>\u4e86\u4e00\u6b21\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u52a0\u9501\u89e3\u51b3\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/p>\n<\/blockquote>\n\n\n\n<p>C++98\u4e2d\u5bf9\u4e8e\u8fd9\u91cc\u51fa\u73b0\u7684\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff0c\u4f1a\u9009\u62e9\u5bf9\u5171\u4eab\u4fee\u6539\u7684\u6570\u636e\u8fdb\u884c\u52a0\u9501\u4fdd\u62a4\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(int&amp; n, int times, mutex&amp; mtx)\n{\n    mtx.lock();\n    for (int i = 0; i &lt; times; i++)\n    {\n        \/\/mtx.lock();\n        n++;\n        \/\/mtx.unlock();\n    }\n    mtx.unlock();\n}\nint main()\n{\n    int n = 0;\n    int times = 100000; \/\/\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9n++\u7684\u6b21\u6570\n    mutex mtx;\n    thread t1(func, ref(n), times, ref(mtx));\n    thread t2(func, ref(n), times, ref(mtx));\n\n    t1.join();\n    t2.join();\n    cout &lt;&lt; n &lt;&lt; endl; \/\/\u6253\u5370n\u7684\u503c\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u9009\u62e9\u5728for\u5faa\u73af\u4f53\u91cc\u9762\u8fdb\u884c\u52a0\u9501\u89e3\u9501\uff0c\u4e5f\u53ef\u4ee5\u9009\u62e9\u5728for\u5faa\u73af\u4f53\u5916\u8fdb\u884c\u52a0\u9501\u89e3\u9501\u3002\u4f46\u6548\u679c\u7ec8\u7a76\u662f\u4e0d\u5c3d\u4eba\u610f\u7684\uff0c\u5728for\u5faa\u73af\u4f53\u91cc\u9762\u8fdb\u884c\u52a0\u9501\u89e3\u9501\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u7684\u9891\u7e41\u8fdb\u884c\u52a0\u9501\u89e3\u9501\u64cd\u4f5c\uff0c\u5728for\u5faa\u73af\u4f53\u5916\u9762\u8fdb\u884c\u52a0\u9501\u89e3\u9501\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u7ebf\u7a0b\u7684\u6267\u884c\u903b\u8f91\u53d8\u4e3a\u4e32\u884c\uff0c\u800c\u4e14\u5982\u679c\u9501\u63a7\u5236\u5f97\u4e0d\u597d\uff0c\u8fd8\u5bb9\u6613\u9020\u6210\u6b7b\u9501\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u539f\u5b50\u7c7b\u89e3\u51b3\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/p>\n<\/blockquote>\n\n\n\n<p>C++11\u4e2d\u5f15\u5165\u4e86\u539f\u5b50\u64cd\u4f5c\u7c7b\u578b\uff0c\u4f7f\u5f97\u7ebf\u7a0b\u95f4\u6570\u636e\u7684\u540c\u6b65\u53d8\u5f97\u975e\u5e38\u9ad8\u6548\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">\u539f\u5b50\u7c7b\u578b\u540d\u79f0<\/th><th class=\"has-text-align-center\" data-align=\"center\">\u5bf9\u5e94\u7684\u5185\u7f6e\u7c7b\u578b\u540d\u79f0<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_bool<\/td><td class=\"has-text-align-center\" data-align=\"center\">bool<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_char<\/td><td class=\"has-text-align-center\" data-align=\"center\">char<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_schar<\/td><td class=\"has-text-align-center\" data-align=\"center\">signed char<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_uchar<\/td><td class=\"has-text-align-center\" data-align=\"center\">unsigned char<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_int<\/td><td class=\"has-text-align-center\" data-align=\"center\">int<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_uint<\/td><td class=\"has-text-align-center\" data-align=\"center\">unsigned int<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_short<\/td><td class=\"has-text-align-center\" data-align=\"center\">short<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_ushort<\/td><td class=\"has-text-align-center\" data-align=\"center\">unsigned short<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_long<\/td><td class=\"has-text-align-center\" data-align=\"center\">long<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_ulong<\/td><td class=\"has-text-align-center\" data-align=\"center\">unsigned long<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_llong<\/td><td class=\"has-text-align-center\" data-align=\"center\">long long<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_ullong<\/td><td class=\"has-text-align-center\" data-align=\"center\">unsigned long long<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_char16_t<\/td><td class=\"has-text-align-center\" data-align=\"center\">char16_t<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_char32_t<\/td><td class=\"has-text-align-center\" data-align=\"center\">char32_t<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">atomic_wchar_t<\/td><td class=\"has-text-align-center\" data-align=\"center\">wchar_t<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u9700\u8981\u7528\u5927\u62ec\u53f7\u5bf9\u539f\u5b50\u7c7b\u578b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u7a0b\u5e8f\u5458\u4e0d\u9700\u8981\u5bf9\u539f\u5b50\u7c7b\u578b\u8fdb\u884c\u52a0\u9501\u89e3\u9501\u64cd\u4f5c\uff0c\u7ebf\u7a0b\u80fd\u591f\u5bf9\u539f\u5b50\u7c7b\u578b\u53d8\u91cf\u4e92\u65a5\u8bbf\u95ee\u3002\u6bd4\u5982\u521a\u624d\u7684\u4ee3\u7801\u53ef\u4ee5\u6539\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(atomic_int&amp; n, int times)\n{\n    for (int i = 0; i &lt; times; i++)\n    {\n        n++;\n    }\n}\nint main()\n{\n    atomic_int n = { 0 };\n    int times = 100000; \/\/\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9n++\u7684\u6b21\u6570\n    thread t1(func, ref(n), times);\n    thread t2(func, ref(n), times);\n\n    t1.join();\n    t2.join();\n    cout &lt;&lt; n &lt;&lt; endl; \/\/\u6253\u5370n\u7684\u503c\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528atomic\u7c7b\u6a21\u677f\u5b9a\u4e49\u51fa\u4efb\u610f\u539f\u5b50\u7c7b\u578b\u3002\u6bd4\u5982\u4e0a\u8ff0\u4ee3\u7801\u8fd8\u53ef\u4ee5\u6539\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func(atomic&lt;int&gt;&amp; n, int times)\n{\n    for (int i = 0; i &lt; times; i++)\n    {\n        n++;\n    }\n}\nint main()\n{\n    atomic&lt;int&gt; n = 0;\n    int times = 100000; \/\/\u6bcf\u4e2a\u7ebf\u7a0b\u5bf9n++\u7684\u6b21\u6570\n    thread t1(func, ref(n), times);\n    thread t2(func, ref(n), times);\n\n    t1.join();\n    t2.join();\n    cout &lt;&lt; n &lt;&lt; endl; \/\/\u6253\u5370n\u7684\u503c\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\u4e00\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u539f\u5b50\u7c7b\u578b\u901a\u5e38\u5c5e\u4e8e\u201c\u8d44\u6e90\u7c7b\u578b\u201d\u6570\u636e\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ea\u80fd\u8bbf\u95ee\u5355\u4e2a\u539f\u5b50\u7c7b\u578b\u7684\u62f7\u8d1d\uff0c\u56e0\u6b64\u5728C++11\u4e2d\uff0c\u539f\u5b50\u7c7b\u578b\u53ea\u80fd\u4ece\u5176\u6a21\u677f\u53c2\u6570\u4e2d\u8fdb\u884c\u6784\u9020\uff0c\u4e0d\u5141\u8bb8\u539f\u5b50\u7c7b\u578b\u8fdb\u884c\u62f7\u8d1d\u6784\u9020\u3001\u79fb\u52a8\u6784\u9020\u4ee5\u53ca<code>operator=<\/code>\u7b49\u3002<\/li>\n\n\n\n<li>\u4e3a\u4e86\u9632\u6b62\u610f\u5916\uff0c\u6807\u51c6\u5e93\u5df2\u7ecf\u5c06atomic\u6a21\u677f\u7c7b\u4e2d\u7684\u62f7\u8d1d\u6784\u9020\u3001\u79fb\u52a8\u6784\u9020\u3001<code>operator=<\/code>\u9ed8\u8ba4\u5220\u9664\u6389\u4e86\u3002<\/li>\n\n\n\n<li>\u539f\u5b50\u7c7b\u578b\u4e0d\u4ec5\u4ec5\u652f\u6301\u539f\u5b50\u7684<code>++<\/code>\u64cd\u4f5c\uff0c\u8fd8\u652f\u6301\u539f\u5b50\u7684<code>--<\/code>\u3001\u52a0\u4e00\u4e2a\u503c\u3001\u51cf\u4e00\u4e2a\u503c\u3001\u4e0e\u3001\u6216\u3001\u5f02\u6216\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u6761\u4ef6\u53d8\u91cf\u5e93\uff08condition_variable\uff09<\/h3>\n\n\n\n<p>condition_variable\u4e2d\u63d0\u4f9b\u7684\u6210\u5458\u51fd\u6570\uff0c\u53ef\u5206\u4e3await\u7cfb\u5217\u548cnotify\u7cfb\u5217\u4e24\u7c7b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>wait\u7cfb\u5217\u6210\u5458\u51fd\u6570<\/p>\n<\/blockquote>\n\n\n\n<p>wait\u7cfb\u5217\u6210\u5458\u51fd\u6570\u7684\u4f5c\u7528\u5c31\u662f\u8ba9\u8c03\u7528\u7ebf\u7a0b\u8fdb\u884c\u963b\u585e\u7b49\u5f85\uff0c\u5305\u62ec<code>wait<\/code>\u3001<code>wait_for<\/code>\u548c<code>wait_until<\/code>\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u5148\u4ee5<code>wait<\/code>\u4e3a\u4f8b\u8fdb\u884c\u4ecb\u7ecd\uff0cwait\u51fd\u6570\u63d0\u4f9b\u4e86\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684\u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u7248\u672c\u4e00\nvoid wait(unique_lock&lt;mutex&gt;&amp; lck);\n\/\/\u7248\u672c\u4e8c\ntemplate&lt;class Predicate&gt;\nvoid wait(unique_lock&lt;mutex&gt;&amp; lck, Predicate pred);<\/code><\/pre>\n\n\n\n<p><strong>\u51fd\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8c03\u7528\u7b2c\u4e00\u4e2a\u7248\u672c\u7684wait\u51fd\u6570\u65f6\u53ea\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u4e92\u65a5\u9501\uff0c\u7ebf\u7a0b\u8c03\u7528wait\u540e\u4f1a\u7acb\u5373\u88ab\u963b\u585e\uff0c\u76f4\u5230\u88ab\u5524\u9192\u3002<\/li>\n\n\n\n<li>\u8c03\u7528\u7b2c\u4e8c\u4e2a\u7248\u672c\u7684wait\u51fd\u6570\u65f6\u9664\u4e86\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u4e92\u65a5\u9501\uff0c\u8fd8\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u8fd4\u56de\u503c\u7c7b\u578b\u4e3abool\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4e0e\u7b2c\u4e00\u4e2a\u7248\u672c\u7684wait\u4e0d\u540c\u7684\u662f\uff0c\u5f53\u7ebf\u7a0b\u88ab\u5524\u9192\u540e\u8fd8\u9700\u8981\u8c03\u7528\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u5982\u679c\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u4e3afalse\uff0c\u90a3\u4e48\u8be5\u7ebf\u7a0b\u8fd8\u9700\u8981\u7ee7\u7eed\u88ab\u963b\u585e\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u8c03\u7528wait\u7cfb\u5217\u51fd\u6570\u65f6\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u4e92\u65a5\u9501\uff1f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u56e0\u4e3await\u7cfb\u5217\u51fd\u6570\u4e00\u822c\u662f\u5728\u4e34\u754c\u533a\u4e2d\u8c03\u7528\u7684\uff0c\u4e3a\u4e86\u8ba9\u5f53\u524d\u7ebf\u7a0b\u8c03\u7528wait\u963b\u585e\u65f6\u5176\u4ed6\u7ebf\u7a0b\u80fd\u591f\u83b7\u53d6\u5230\u9501\uff0c\u56e0\u6b64\u8c03\u7528wait\u7cfb\u5217\u51fd\u6570\u65f6\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u4e92\u65a5\u9501\uff0c\u5f53\u7ebf\u7a0b\u88ab\u963b\u585e\u65f6\u8fd9\u4e2a\u4e92\u65a5\u9501\u4f1a\u88ab\u81ea\u52a8\u89e3\u9501\uff0c\u800c\u5f53\u8fd9\u4e2a\u7ebf\u7a0b\u88ab\u5524\u9192\u65f6\uff0c\u53c8\u4f1a\u81ea\u52a8\u83b7\u5f97\u8fd9\u4e2a\u4e92\u65a5\u9501\u3002<\/li>\n\n\n\n<li>\u56e0\u6b64wait\u7cfb\u5217\u51fd\u6570\u5b9e\u9645\u4e0a\u6709\u4e24\u4e2a\u529f\u80fd\uff0c\u4e00\u4e2a\u662f\u8ba9\u7ebf\u7a0b\u5728\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\u8fdb\u884c\u963b\u585e\u7b49\u5f85\uff0c\u53e6\u4e00\u4e2a\u662f\u8ba9\u7ebf\u7a0b\u5c06\u5bf9\u5e94\u7684\u4e92\u65a5\u9501\u8fdb\u884c\u89e3\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p>wait_for\u548cwait_until\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u5f0f\u4e0ewait\u51fd\u6570\u7c7b\u4f3c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>wait_for\u51fd\u6570\u4e5f\u63d0\u4f9b\u4e86\u4e24\u4e2a\u7248\u672c\u7684\u63a5\u53e3\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u4e24\u4e2a\u7248\u672c\u7684\u63a5\u53e3\u90fd\u6bd4wait\u51fd\u6570\u5bf9\u5e94\u7684\u63a5\u53e3\u591a\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u65f6\u95f4\u6bb5\uff0c\u8868\u793a\u8ba9\u7ebf\u7a0b\u5728\u8be5\u65f6\u95f4\u6bb5\u5185\u8fdb\u884c\u963b\u585e\u7b49\u5f85\uff0c\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u6bb5\u5219\u7ebf\u7a0b\u88ab\u81ea\u52a8\u5524\u9192\u3002<\/li>\n\n\n\n<li>wait_until\u51fd\u6570\u4e5f\u63d0\u4f9b\u4e86\u4e24\u4e2a\u7248\u672c\u7684\u63a5\u53e3\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u4e24\u4e2a\u7248\u672c\u7684\u63a5\u53e3\u90fd\u6bd4wait\u51fd\u6570\u5bf9\u5e94\u7684\u63a5\u53e3\u591a\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u65f6\u95f4\u70b9\uff0c\u8868\u793a\u8ba9\u7ebf\u7a0b\u5728\u8be5\u65f6\u95f4\u70b9\u4e4b\u524d\u8fdb\u884c\u963b\u585e\u7b49\u5f85\uff0c\u5982\u679c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u70b9\u5219\u7ebf\u7a0b\u88ab\u81ea\u52a8\u5524\u9192\u3002<\/li>\n\n\n\n<li>\u7ebf\u7a0b\u8c03\u7528wait_for\u6216wait_until\u51fd\u6570\u5728\u963b\u585e\u7b49\u5f85\u671f\u95f4\uff0c\u5176\u4ed6\u7ebf\u7a0b\u8c03\u7528notify\u7cfb\u5217\u51fd\u6570\u4e5f\u53ef\u4ee5\u5c06\u5176\u5524\u9192\u3002\u6b64\u5916\uff0c\u5982\u679c\u8c03\u7528\u7684\u662fwait_for\u6216wait_until\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u7248\u672c\u7684\u63a5\u53e3\uff0c\u90a3\u4e48\u5f53\u7ebf\u7a0b\u88ab\u5524\u9192\u540e\u8fd8\u9700\u8981\u8c03\u7528\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u5982\u679c\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u4e3afalse\uff0c\u90a3\u4e48\u5f53\u524d\u7ebf\u7a0b\u8fd8\u9700\u8981\u7ee7\u7eed\u88ab\u963b\u585e\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u8c03\u7528wait\u7cfb\u5217\u51fd\u6570\u65f6\uff0c\u4f20\u5165\u4e92\u65a5\u9501\u7684\u7c7b\u578b\u5fc5\u987b\u662funique_lock\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>notify\u7cfb\u5217\u6210\u5458\u51fd\u6570<\/p>\n<\/blockquote>\n\n\n\n<p>notify\u7cfb\u5217\u6210\u5458\u51fd\u6570\u7684\u4f5c\u7528\u5c31\u662f\u5524\u9192\u7b49\u5f85\u7684\u7ebf\u7a0b\uff0c\u5305\u62ec<code>notify_one<\/code>\u548c<code>notify_all<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>notify_one<\/code>\uff1a\u5524\u9192\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u9996\u4e2a\u7ebf\u7a0b\uff0c\u5982\u679c\u7b49\u5f85\u961f\u5217\u4e3a\u7a7a\u5219\u4ec0\u4e48\u4e5f\u4e0d\u505a\u3002<\/li>\n\n\n\n<li><code>notify_all<\/code>\uff1a\u5524\u9192\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\uff0c\u5982\u679c\u7b49\u5f85\u961f\u5217\u4e3a\u7a7a\u5219\u4ec0\u4e48\u4e5f\u4e0d\u505a\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u6761\u4ef6\u53d8\u91cf\u4e0b\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u7ebf\u7a0b\u5728\u8fdb\u884c\u963b\u585e\u7b49\u5f85\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u4e2d\u8fdb\u884c\u6392\u961f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u73b0\u4e24\u4e2a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u53701-100<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5c1d\u8bd5\u7528\u4e24\u4e2a\u7ebf\u7a0b\u4ea4\u66ff\u6253\u53701-100\u7684\u6570\u5b57\uff0c\u8981\u6c42\u4e00\u4e2a\u7ebf\u7a0b\u6253\u5370\u5947\u6570\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6253\u5370\u5076\u6570\uff0c\u5e76\u4e14\u6253\u5370\u6570\u5b57\u4ece\u5c0f\u5230\u5927\u4f9d\u6b21\u9012\u589e\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u8be5\u9898\u76ee\u4e3b\u8981\u8003\u5bdf\u7684\u5c31\u662f\u7ebf\u7a0b\u7684\u540c\u6b65\u548c\u4e92\u65a5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e92\u65a5\uff1a\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u5728\u5411\u63a7\u5236\u53f0\u6253\u5370\u6570\u636e\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u4e24\u4e2a\u7ebf\u7a0b\u7684\u6253\u5370\u6570\u636e\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\uff0c\u56e0\u6b64\u9700\u8981\u5bf9\u7ebf\u7a0b\u7684\u6253\u5370\u8fc7\u7a0b\u8fdb\u884c\u52a0\u9501\u4fdd\u62a4\u3002<\/li>\n\n\n\n<li>\u540c\u6b65\uff1a\u4e24\u4e2a\u7ebf\u7a0b\u5fc5\u987b\u4ea4\u66ff\u8fdb\u884c\u6253\u5370\uff0c\u56e0\u6b64\u9700\u8981\u7528\u5230\u6761\u4ef6\u53d8\u91cf\u8ba9\u4e24\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u540c\u6b65\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6253\u5370\u5b8c\u518d\u5524\u9192\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u6253\u5370\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f46\u5982\u679c\u53ea\u6709\u540c\u6b65\u548c\u4e92\u65a5\u662f\u65e0\u6cd5\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148\uff0c\u6211\u4eec\u65e0\u6cd5\u4fdd\u8bc1\u54ea\u4e00\u4e2a\u7ebf\u7a0b\u4f1a\u5148\u8fdb\u884c\u6253\u5370\uff0c\u4e0d\u80fd\u8bf4\u5148\u521b\u5efa\u7684\u7ebf\u7a0b\u5c31\u4e00\u5b9a\u5148\u6253\u5370\uff0c\u540e\u521b\u5efa\u7684\u7ebf\u7a0b\u5148\u6253\u5370\u4e5f\u662f\u6709\u53ef\u80fd\u7684\u3002<\/li>\n\n\n\n<li>\u6b64\u5916\uff0c\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u67d0\u4e2a\u7ebf\u7a0b\u8fde\u7eed\u591a\u6b21\u6253\u5370\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u7ebf\u7a0b1\u5148\u521b\u5efa\u5e76\u6253\u5370\u4e86\u4e00\u4e2a\u6570\u5b57\uff0c\u5f53\u7ebf\u7a0b1\u51c6\u5907\u6253\u5370\u7b2c\u4e8c\u4e2a\u6570\u5b57\u7684\u65f6\u5019\u7ebf\u7a0b2\u53ef\u80fd\u8fd8\u6ca1\u6709\u521b\u5efa\u51fa\u6765\uff0c\u6216\u662f\u7ebf\u7a0b2\u8fd8\u6ca1\u6709\u5728\u4e92\u65a5\u9501\u4e0a\u8fdb\u884c\u7b49\u5f85\uff0c\u8fd9\u65f6\u7ebf\u7a0b1\u5c31\u4f1a\u518d\u6b21\u83b7\u53d6\u5230\u9501\u8fdb\u884c\u6253\u5370\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9274\u4e8e\u6b64\uff0c\u8fd9\u91cc\u8fd8\u9700\u8981\u5b9a\u4e49\u4e00\u4e2aflag\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u7684\u521d\u59cb\u503c\u8bbe\u7f6e\u4e3atrue\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5047\u8bbe\u8ba9\u7ebf\u7a0b1\u6253\u5370\u5947\u6570\uff0c\u7ebf\u7a0b2\u6253\u5370\u5076\u6570\u3002\u90a3\u4e48\u5c31\u8ba9\u7ebf\u7a0b1\u8c03\u7528wait\u51fd\u6570\u963b\u585e\u7b49\u5f85\u65f6\uff0c\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u8fd4\u56de<code>flag<\/code>\u7684\u503c\uff0c\u800c\u8ba9\u7ebf\u7a0b2\u8c03\u7528wait\u51fd\u6570\u963b\u585e\u7b49\u5f85\u65f6\uff0c\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u8fd4\u56de<code>!flag<\/code>\u7684\u503c\u3002<\/li>\n\n\n\n<li>\u7531\u4e8eflag\u7684\u521d\u59cb\u503c\u662ftrue\uff0c\u5c31\u7b97\u7ebf\u7a0b2\u5148\u83b7\u53d6\u5230\u4e92\u65a5\u9501\u4e5f\u4e0d\u80fd\u8fdb\u884c\u6253\u5370\uff0c\u56e0\u4e3a\u6700\u5f00\u59cb\u7ebf\u7a0b2\u8c03\u7528wait\u51fd\u6570\u65f6\uff0c\u4f1a\u56e0\u4e3a\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u4e3afalse\u800c\u88ab\u963b\u585e\uff0c\u8fd9\u5c31\u4fdd\u8bc1\u4e86\u7ebf\u7a0b1\u4e00\u5b9a\u5148\u8fdb\u884c\u6253\u5370\u3002<\/li>\n\n\n\n<li>\u4e3a\u4e86\u8ba9\u4e24\u4e2a\u7ebf\u7a0b\u4ea4\u66ff\u8fdb\u884c\u6253\u5370\uff0c\u56e0\u6b64\u4e24\u4e2a\u7ebf\u7a0b\u6bcf\u6b21\u6253\u5370\u540e\u90fd\u9700\u8981\u66f4\u6539flag\u7684\u503c\uff0c\u7ebf\u7a0b1\u6253\u5370\u5b8c\u540e\u5c06flag\u7684\u503c\u6539\u4e3afalse\u5e76\u5524\u9192\u7ebf\u7a0b2\uff0c\u8fd9\u65f6\u7ebf\u7a0b2\u88ab\u5524\u9192\u65f6\u5176\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u5c31\u53d8\u6210\u4e86true\uff0c\u8fd9\u65f6\u7ebf\u7a0b2\u5c31\u53ef\u4ee5\u8fdb\u884c\u6253\u5370\u4e86\u3002<\/li>\n\n\n\n<li>\u5f53\u7ebf\u7a0b2\u6253\u5370\u5b8c\u540e\u518d\u5c06flag\u7684\u503c\u6539\u4e3atrue\u5e76\u5524\u9192\u7ebf\u7a0b1\uff0c\u8fd9\u65f6\u7ebf\u7a0b1\u5c31\u53c8\u53ef\u4ee5\u6253\u5370\u4e86\uff0c\u5c31\u7b97\u7ebf\u7a0b2\u60f3\u8981\u8fde\u7eed\u6253\u5370\u4e5f\u4e0d\u884c\uff0c\u56e0\u4e3a\u5982\u679c\u7ebf\u7a0b1\u4e0d\u6253\u5370\uff0c\u90a3\u4e48\u7ebf\u7a0b2\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u5c31\u4e00\u76f4\u4e3afalse\uff0c\u5bf9\u4e8e\u7ebf\u7a0b1\u4e5f\u662f\u4e00\u6837\u7684\u9053\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n    int n = 100;\n    mutex mtx;\n    condition_variable cv;\n    bool flag = true;\n    \/\/\u5947\u6570\n    thread t1(&#91;&amp;]{\n        int i = 1;\n        while (i &lt;= 100)\n        {\n            unique_lock&lt;mutex&gt; ul(mtx);\n            cv.wait(ul, &#91;&amp;flag]()-&gt;bool{return flag; }); \/\/\u7b49\u5f85\u6761\u4ef6\u53d8\u91cf\u6ee1\u8db3\n            cout &lt;&lt; this_thread::get_id() &lt;&lt; \":\" &lt;&lt; i &lt;&lt; endl;\n            i += 2;\n            flag = false;\n            cv.notify_one(); \/\/\u5524\u9192\u6761\u4ef6\u53d8\u91cf\u4e0b\u7b49\u5f85\u7684\u4e00\u4e2a\u7ebf\u7a0b\n        }\n    });\n    \/\/\u5076\u6570\n    thread t2(&#91;&amp;]{\n        int j = 2;\n        while (j &lt;= 100)\n        {\n            unique_lock&lt;mutex&gt; ul(mtx);\n            cv.wait(ul, &#91;&amp;flag]()-&gt;bool{return !flag; }); \/\/\u7b49\u5f85\u6761\u4ef6\u53d8\u91cf\u6ee1\u8db3\n            cout &lt;&lt; this_thread::get_id() &lt;&lt; \":\" &lt;&lt; j &lt;&lt; endl;\n            j += 2;\n            flag = true;\n            cv.notify_one(); \/\/\u5524\u9192\u6761\u4ef6\u53d8\u91cf\u4e0b\u7b49\u5f85\u7684\u4e00\u4e2a\u7ebf\u7a0b\n        }\n    });\n\n    t1.join();\n    t2.join();\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u7a0b\u5e93 \u5728C++11\u4e4b\u524d\uff0c\u6d89\u53ca\u5230\u591a\u7ebf\u7a0b\u95ee\u9898\uff0c\u90fd\u662f\u548c\u5e73\u53f0\u76f8\u5173\u7684\uff0c\u6bd4\u5982Windows\u548cLinux\u4e0b\u5404\u6709\u81ea\u5df1\u7684\u63a5\u53e3\uff0c [&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,1],"tags":[22],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-cpp","category-learn","tag-c11"],"_links":{"self":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":1,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":101,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts\/100\/revisions\/101"}],"wp:attachment":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}