{"id":442,"date":"2025-08-31T21:21:30","date_gmt":"2025-08-31T13:21:30","guid":{"rendered":"http:\/\/yangnanbei.top\/?p=442"},"modified":"2025-08-31T21:25:25","modified_gmt":"2025-08-31T13:25:25","slug":"%e9%94%81%e7%9a%84%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"http:\/\/yangnanbei.top\/index.php\/2025\/08\/31\/%e9%94%81%e7%9a%84%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86\/","title":{"rendered":"\u9501\u7684\u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"\n<p>\u4e3b\u8981\u8ba8\u8bba \u9501\u7684\u5206\u7c7b\uff0c\u9501\u7684\u7279\u70b9\uff0c\u9501\u7684\u5e94\u7528\u573a\u666f<\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u8981\u6709\u9501\uff1f\u4e3a\u4e86\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u6570\u636e\u7ade\u4e89\u95ee\u9898\uff0c\u4fdd\u8bc1\u5171\u4eab\u8d44\u6e90\u5728\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u89c1\u7684\u9501<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u9501\u7c7b\u578b<\/td><td>\u7279\u70b9<\/td><td>\u9002\u7528\u573a\u666f<\/td><td>\u4f18\u70b9<\/td><td>\u7f3a\u70b9<\/td><\/tr><tr><td>\u4e92\u65a5\u9501 (Mutex)<\/td><td>\u4e00\u6b21\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee<\/td><td>\u6700\u5e38\u89c1\u7684\u7ebf\u7a0b\u540c\u6b65<\/td><td>\u7b80\u5355\u3001\u53ef\u9760<\/td><td>\u7ebf\u7a0b\u963b\u585e\u3001\u5207\u6362\u5f00\u9500\u5927<\/td><\/tr><tr><td>\u81ea\u65cb\u9501 (Spinlock)<\/td><td>\u7ebf\u7a0b\u5fd9\u7b49\uff08\u4e0d\u505c\u5faa\u73af\u68c0\u67e5\u9501\uff09<\/td><td>\u9501\u6301\u6709\u65f6\u95f4\u975e\u5e38\u77ed<\/td><td>\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u5c0f<\/td><td>CPU\u7a7a\u8f6c\u6d6a\u8d39\u8d44\u6e90<\/td><\/tr><tr><td>\u8bfb\u5199\u9501 (RWLock)<\/td><td>\u591a\u8bfb\u5355\u5199<\/td><td>\u8bfb\u591a\u5199\u5c11\u7684\u60c5\u51b5<\/td><td>\u8bfb\u64cd\u4f5c\u5e76\u53d1\u9ad8\u6548<\/td><td>\u5199\u65f6\u4ecd\u9700\u963b\u585e\u5168\u90e8\u8bfb<\/td><\/tr><tr><td>\u9012\u5f52\u9501 (Recursive Mutex)<\/td><td>\u540c\u4e00\u7ebf\u7a0b\u53ef\u591a\u6b21\u52a0\u9501<\/td><td>\u907f\u514d\u6b7b\u9501\u7684\u7279\u5b9a\u573a\u666f<\/td><td>\u7f16\u7801\u65b9\u4fbf<\/td><td>\u6ee5\u7528\u6613\u9690\u85cf\u8bbe\u8ba1\u95ee\u9898<\/td><\/tr><tr><td>\u4fe1\u53f7\u91cf (Semaphore)<\/td><td>\u5141\u8bb8\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u6709\u9650\u8d44\u6e90<\/td><td>\u9650\u6d41\u3001\u8fde\u63a5\u6c60<\/td><td>\u53ef\u63a7\u5236\u5e76\u53d1\u91cf<\/td><td>\u590d\u6742\u5ea6\u8f83\u9ad8<\/td><\/tr><tr><td>\u6761\u4ef6\u53d8\u91cf (Condition Variable)<\/td><td>\u7b49\u5f85\u67d0\u6761\u4ef6\u6ee1\u8db3\u624d\u7ee7\u7eed<\/td><td>\u7ebf\u7a0b\u95f4\u4e8b\u4ef6\u901a\u77e5<\/td><td>\u9ad8\u6548\u7b49\u5f85<\/td><td>\u914d\u5408\u9501\u4f7f\u7528\uff0c\u8bbe\u8ba1\u590d\u6742<\/td><\/tr><tr><td>\u8bfb\u590d\u5236\u66f4\u65b0\u9501 (RCU)<\/td><td>\u8bfb\u51e0\u4e4e\u65e0\u9501\uff0c\u5199\u5ef6\u8fdf\u66f4\u65b0<\/td><td>\u9ad8\u5e76\u53d1\u8bfb\u591a\u5199\u5c11\u7cfb\u7edf<\/td><td>\u6781\u9ad8\u8bfb\u6027\u80fd<\/td><td>\u5b9e\u73b0\u590d\u6742\uff0c\u9002\u5408\u5185\u6838\/\u5e95\u5c42<\/td><\/tr><tr><td>\u4e50\u89c2\u9501 (Optimistic Lock)<\/td><td>\u5047\u8bbe\u51b2\u7a81\u5c11\uff0c\u51b2\u7a81\u518d\u91cd\u8bd5<\/td><td>\u6570\u636e\u5e93\u3001CAS\u573a\u666f<\/td><td>\u65e0\u9501\u5f00\u9500\u4f4e<\/td><td>\u51b2\u7a81\u591a\u4f1a\u9891\u7e41\u91cd\u8bd5<\/td><\/tr><tr><td>\u60b2\u89c2\u9501 (Pessimistic Lock)<\/td><td>\u5047\u8bbe\u51b2\u7a81\u591a\uff0c\u5148\u9501\u518d\u64cd\u4f5c<\/td><td>\u94f6\u884c\u8f6c\u8d26\u3001\u5e93\u5b58\u6263\u51cf<\/td><td>\u7b80\u5355\u5b89\u5168<\/td><td>\u6027\u80fd\u53ef\u80fd\u5dee<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e92\u65a5\u9501\u548c\u81ea\u65cb\u9501<\/h2>\n\n\n\n<p><strong>\u4e0a\u4e0b\u6587\u5207\u6362\uff08Context <\/strong><strong>Switch<\/strong><strong>\uff09<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u662f\u6307\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u628a CPU \u4ece\u4e00\u4e2a\u7ebf\u7a0b\/\u8fdb\u7a0b\u5207\u6362\u5230\u53e6\u4e00\u4e2a\u7ebf\u7a0b\/\u8fdb\u7a0b\u65f6\uff0c\u4fdd\u5b58\u548c\u6062\u590d\u5bc4\u5b58\u5668\u3001\u6808\u6307\u9488\u7b49\u4e0a\u4e0b\u6587\u7684\u8fc7\u7a0b\u3002<\/li>\n\n\n\n<li><strong>\u4e0a\u4e0b\u6587\u5207\u6362<\/strong>\u662f<strong>\u6602\u8d35<\/strong>\u7684\uff08\u5f00\u9500\u5728\u5fae\u79d2\u7ea7\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4e92\u65a5\u9501<\/strong><strong>\uff08<\/strong><strong>mutex<\/strong><strong>\uff09<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u52a0\u9501\u65f6\u9501\u5df2\u88ab\u5360\u7528\uff0c\u7ebf\u7a0b\u4f1a<strong>\u4e3b\u52a8\u8ba9\u51fa <\/strong><strong>CPU<\/strong>\uff0c\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff0c\u7b49\u5f85\u88ab\u5524\u9192\u3002<\/li>\n\n\n\n<li>\u6d89\u53ca<strong>\u5185\u6838\u6001\u8c03\u5ea6<\/strong>\uff0c\u5fc5\u7136\u4f1a\u6709\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u81ea\u65cb\u9501<\/strong><strong>\uff08spinlock\uff09<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u9501\u88ab\u5360\u7528\uff0c\u7ebf\u7a0b\u4f1a<strong>\u5fd9\u7b49\uff08<\/strong><strong>\u81ea\u65cb<\/strong><strong>\uff09<\/strong>\uff0c\u4e0d\u4f1a\u7acb\u523b\u8ba9\u51fa CPU\u3002<\/li>\n\n\n\n<li>\u65e0\u963b\u585e\u3001\u65e0\u7cfb\u7edf\u8c03\u5ea6\uff0c\u6240\u4ee5\u6b63\u5e38\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u6709\u4e0a\u4e0b\u6587\u5207\u6362\uff08\u9664\u975e\u5185\u6838\u62a2\u5360\u6216\u8005\u7ebf\u7a0b\u7528\u5c3d\u65f6\u95f4\u7247\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u52a0\u89e3\u9501\u65f6\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5206\u6790<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u9501\u7c7b\u578b<\/td><td>\u60c5\u51b5<\/td><td>\u4e0a\u9501\uff08lock\uff09\u65f6\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570<\/td><td>\u89e3\u9501\uff08unlock\uff09\u65f6\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570<\/td><td>\u8bf4\u660e<\/td><\/tr><tr><td>\u4e92\u65a5\u9501<\/td><td>\u9501\u7a7a\u95f2<\/td><td>0 \u6b21<\/td><td>0 \u6b21<\/td><td>\u7528\u6237\u6001\u5feb\u901f\u83b7\u53d6\u548c\u91ca\u653e\u9501\uff0c\u65e0\u9700\u8c03\u5ea6\u3002<\/td><\/tr><tr><td><\/td><td>\u9501\u88ab\u5360\u7528<\/td><td>2 \u6b21\uff08\u5f53\u524d\u7ebf\u7a0b\u963b\u585e\uff09<\/td><td>0~1 \u6b21\uff08\u5524\u9192\u7b49\u5f85\u7ebf\u7a0b\uff09<\/td><td>\u5f53\u524d\u7ebf\u7a0b\u7761\u7720\u65f6\uff1a<br>1. \u7ebf\u7a0b\u5207\u6362\u5230\u5185\u6838\uff0c\u963b\u585e\u5f53\u524d\u7ebf\u7a0b \u2192 1 \u6b21<br>2. \u5207\u6362\u5230\u5176\u4ed6\u53ef\u8fd0\u884c\u7ebf\u7a0b \u2192 1 \u6b21<br>\u89e3\u9501\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5524\u9192\u7b49\u5f85\u7ebf\u7a0b\uff0c\u8c03\u5ea6\u5668\u51b3\u5b9a\u662f\u5426\u5207\u6362 \u2192 0 \u6216 1 \u6b21\u3002<\/td><\/tr><tr><td>\u81ea\u65cb\u9501<\/td><td>\u9501\u7a7a\u95f2<\/td><td>0 \u6b21<\/td><td>0 \u6b21<\/td><td>\u81ea\u65cb\u9501\u53ea\u7528\u539f\u5b50\u64cd\u4f5c\u3002<\/td><\/tr><tr><td><\/td><td>\u9501\u88ab\u5360\u7528<\/td><td>0 \u6b21\uff08\u81ea\u65cb\u7b49\u5f85\uff09<\/td><td>0 \u6b21<\/td><td>\u81ea\u65cb\u9501\u4e0d\u4f1a\u4e3b\u52a8\u8ba9\u51fa CPU\uff0c\u7ebf\u7a0b\u4f1a\u4e00\u76f4\u5fd9\u7b49\uff0c\u4e0d\u53d1\u751f\u4e0a\u4e0b\u6587\u5207\u6362\uff08\u9664\u975e\u65f6\u95f4\u7247\u8017\u5c3d\u6216\u5185\u6838\u62a2\u5360\uff09\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\u89c4\u5f8b<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e92\u65a5\u9501<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6210\u529f\u83b7\u53d6\u9501\u65f6\u51e0\u4e4e\u6ca1\u5f00\u9500\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u9501\u88ab\u5360\u7528\uff0c\u7ebf\u7a0b\u4f1a\u8fdb\u5165\u5185\u6838\u963b\u585e\uff0c<strong>\u81f3\u5c11\u53d1\u751f 2 \u6b21\u4e0a\u4e0b\u6587\u5207\u6362<\/strong>\uff08\u963b\u585e\u81ea\u5df1\u3001\u8c03\u5ea6\u522b\u4eba(\u82e5\u7ed1\u5b9a\u7684cpu\u4eb2\u548c\u5219\u53e6\u8bf4)\uff1b\u7b49\u9501\u91ca\u653e\u518d\u5524\u9192\u81ea\u5df1\uff09\u3002<\/li>\n\n\n\n<li>\u6700\u901a\u7528\u4e14\u6700\u57fa\u7840\u7684\u9501\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u81ea\u65cb\u9501<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u4f1a\u56e0\u4e3a\u9501\u88ab\u5360\u7528\u800c\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u7ebf\u7a0b\u4f1a\u5fd9\u7b49\u3002<\/li>\n\n\n\n<li>\u4f46\u4f1a\u6d6a\u8d39 CPU \u65f6\u95f4\u7247\u3002<\/li>\n\n\n\n<li>\u5e38\u7528\u4e8e\u9ad8\u6027\u80fd\u7f16\u7a0b\u4e2d<strong>\u9884\u671f\u7b49\u5f85\u65f6\u95f4\u8f83\u5c11\u7684\u7ade\u4e89\u573a\u666f<\/strong>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u9a8c\u8bc1<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;iostream>\n#include &lt;thread>\n#include &lt;mutex>\n#include &lt;atomic>\n#include &lt;fstream>\n#include &lt;chrono>\n#include &lt;string>\n\n\/\/ \u8bfb\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570\nvoid print_context_switches(const std::string&amp; tag) {\n    std::ifstream status(\"\/proc\/self\/status\");\n    std::string line;\n    std::string voluntary, nonvoluntary;\n\n    while (std::getline(status, line)) {\n        if (line.find(\"voluntary_ctxt_switches\") != std::string::npos) {\n            voluntary = line;\n        } else if (line.find(\"nonvoluntary_ctxt_switches\") != std::string::npos) {\n            nonvoluntary = line;\n        }\n    }\n\n    std::cout &lt;&lt; \"&#91;\" &lt;&lt; tag &lt;&lt; \"&#93; \" &lt;&lt; voluntary &lt;&lt; \", \" &lt;&lt; nonvoluntary &lt;&lt; std::endl;\n}\n\n\/\/ \u4e92\u65a5\u9501\u6d4b\u8bd5\nvoid test_mutex() {\n    std::mutex mtx;\n    bool ready = false;\n\n    auto worker = &#91;&amp;&#93;() {\n        while (!ready) std::this_thread::yield();\n        auto start = std::chrono::high_resolution_clock::now();\n\n        std::lock_guard&lt;std::mutex> lock(mtx);\n        auto end = std::chrono::high_resolution_clock::now();\n        std::cout &lt;&lt; \"&#91;Mutex&#93; Lock acquired after \"\n                  &lt;&lt; std::chrono::duration_cast&lt;std::chrono::microseconds>(end - start).count()\n                  &lt;&lt; \" us\\n\";\n        print_context_switches(\"Mutex thread\");\n    };\n\n    std::thread t(worker);\n\n    mtx.lock();\n    ready = true;\n    std::this_thread::sleep_for(std::chrono::milliseconds(100)); \/\/ \u6301\u9501100ms\n    mtx.unlock();\n    t.join();\n}\n\n\/\/ \u81ea\u65cb\u9501\u6d4b\u8bd5\nvoid test_spinlock() {\n    std::atomic_flag spin = ATOMIC_FLAG_INIT;\n    bool ready = false;\n\n    auto worker = &#91;&amp;&#93;() {\n        while (!ready) std::this_thread::yield();\n        auto start = std::chrono::high_resolution_clock::now();\n\n        while (spin.test_and_set(std::memory_order_acquire)) {\n            \/\/ busy wait\n        }\n\n        auto end = std::chrono::high_resolution_clock::now();\n        std::cout &lt;&lt; \"&#91;Spinlock&#93; Lock acquired after \"\n                  &lt;&lt; std::chrono::duration_cast&lt;std::chrono::microseconds>(end - start).count()\n                  &lt;&lt; \" us\\n\";\n        spin.clear(std::memory_order_release);\n        print_context_switches(\"Spinlock thread\");\n    };\n\n    std::thread t(worker);\n\n    spin.test_and_set(std::memory_order_acquire);\n    ready = true;\n    std::this_thread::sleep_for(std::chrono::milliseconds(100)); \/\/ \u6301\u9501100ms\n    spin.clear(std::memory_order_release);\n    t.join();\n}\n\nint main() {\n    std::cout &lt;&lt; \"=== Mutex test ===\" &lt;&lt; std::endl;\n    test_mutex();\n    std::cout &lt;&lt; \"\\n=== Spinlock test ===\" &lt;&lt; std::endl;\n    test_spinlock();\n    return 0;\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">iostream<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">thread<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">mutex<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">atomic<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">fstream<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">chrono<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">string<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">\/\/ \u8bfb\u53d6\u5f53\u524d\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u6b21\u6570<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">print_context_switches<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">const<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #97E1F1; font-style: italic\">string<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #FFB86C; font-style: italic\">tag<\/span><span style=\"color: #F6F6F4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">ifstream <\/span><span style=\"color: #62E884\">status<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">\/proc\/self\/status<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">string line;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">string voluntary, nonvoluntary;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">while<\/span><span style=\"color: #F6F6F4\"> (std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">getline<\/span><span style=\"color: #F6F6F4\">(status, line)) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> (line.<\/span><span style=\"color: #62E884\">find<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">voluntary_ctxt_switches<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">!=<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">string<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">npos) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            voluntary <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> line;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        } <\/span><span style=\"color: #F286C4\">else<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> (line.<\/span><span style=\"color: #62E884\">find<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">nonvoluntary_ctxt_switches<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #F286C4\">!=<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">string<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">npos) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">            nonvoluntary <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> line;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">&#91;<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> tag <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">&#93; <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> voluntary <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">, <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> nonvoluntary <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">\/\/ \u4e92\u65a5\u9501\u6d4b\u8bd5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">test_mutex<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">mutex mtx;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">bool<\/span><span style=\"color: #F6F6F4\"> ready <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">false<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> worker <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> &#91;<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">&#93;() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">while<\/span><span style=\"color: #F6F6F4\"> (<\/span><span style=\"color: #F286C4\">!<\/span><span style=\"color: #F6F6F4\">ready) std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">this_thread<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">yield<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> start <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">high_resolution_clock<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">now<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #97E1F1; font-style: italic\">lock_guard<\/span><span style=\"color: #F6F6F4\">&lt;std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #97E1F1; font-style: italic\">mutex<\/span><span style=\"color: #F6F6F4\">&gt; <\/span><span style=\"color: #62E884\">lock<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #97E1F1; font-style: italic\">mtx<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> end <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">high_resolution_clock<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">now<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">&#91;Mutex&#93; Lock acquired after <\/span><span style=\"color: #DEE492\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                  <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">duration_cast<\/span><span style=\"color: #F6F6F4\">&lt;std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #97E1F1; font-style: italic\">microseconds<\/span><span style=\"color: #F6F6F4\">&gt;(end <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> start).<\/span><span style=\"color: #62E884\">count<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                  <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\"> us<\/span><span style=\"color: #F286C4\">\\n<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #62E884\">print_context_switches<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">Mutex thread<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    };<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">thread <\/span><span style=\"color: #62E884\">t<\/span><span style=\"color: #F6F6F4\">(worker);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    mtx.<\/span><span style=\"color: #62E884\">lock<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    ready <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">true<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">this_thread<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">sleep_for<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">milliseconds<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">100<\/span><span style=\"color: #F6F6F4\">));<\/span><span style=\"color: #7B7F8B\"> \/\/ \u6301\u9501100ms<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    mtx.<\/span><span style=\"color: #62E884\">unlock<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    t.<\/span><span style=\"color: #62E884\">join<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">\/\/ \u81ea\u65cb\u9501\u6d4b\u8bd5<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">test_spinlock<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">atomic_flag spin <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> ATOMIC_FLAG_INIT;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">bool<\/span><span style=\"color: #F6F6F4\"> ready <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">false<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> worker <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> &#91;<\/span><span style=\"color: #F286C4\">&amp;<\/span><span style=\"color: #F6F6F4\">&#93;() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">while<\/span><span style=\"color: #F6F6F4\"> (<\/span><span style=\"color: #F286C4\">!<\/span><span style=\"color: #F6F6F4\">ready) std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">this_thread<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">yield<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> start <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">high_resolution_clock<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">now<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">while<\/span><span style=\"color: #F6F6F4\"> (spin.<\/span><span style=\"color: #62E884\">test_and_set<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">memory_order_acquire)) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">            \/\/ busy wait<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #F286C4\">auto<\/span><span style=\"color: #F6F6F4\"> end <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">high_resolution_clock<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">now<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">&#91;Spinlock&#93; Lock acquired after <\/span><span style=\"color: #DEE492\">&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                  <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">duration_cast<\/span><span style=\"color: #F6F6F4\">&lt;std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #97E1F1; font-style: italic\">microseconds<\/span><span style=\"color: #F6F6F4\">&gt;(end <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> start).<\/span><span style=\"color: #62E884\">count<\/span><span style=\"color: #F6F6F4\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">                  <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\"> us<\/span><span style=\"color: #F286C4\">\\n<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        spin.<\/span><span style=\"color: #62E884\">clear<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">memory_order_release);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #62E884\">print_context_switches<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">Spinlock thread<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    };<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">thread <\/span><span style=\"color: #62E884\">t<\/span><span style=\"color: #F6F6F4\">(worker);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    spin.<\/span><span style=\"color: #62E884\">test_and_set<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">memory_order_acquire);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    ready <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">true<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">this_thread<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">sleep_for<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">chrono<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">milliseconds<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">100<\/span><span style=\"color: #F6F6F4\">));<\/span><span style=\"color: #7B7F8B\"> \/\/ \u6301\u9501100ms<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    spin.<\/span><span style=\"color: #62E884\">clear<\/span><span style=\"color: #F6F6F4\">(std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">memory_order_release);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    t.<\/span><span style=\"color: #62E884\">join<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">=== Mutex test ===<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">test_mutex<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F286C4\">\\n<\/span><span style=\"color: #E7EE98\">=== Spinlock test ===<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #62E884\">test_spinlock<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">return<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>g++ -std=c++17 -O2 -pthread lock_test.cpp -o lock_test<\/p>\n\n\n\n<p>.\/lock_test<\/p>\n<\/blockquote>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>getrusage<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684\u8c03\u5ea6\u4fe1\u606f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>voluntary<\/code>: \u7ebf\u7a0b\u4e0a\u4e0b\u6587\u4e3b\u52a8\u5207\u6362\u8ba1\u6570<\/li>\n\n\n\n<li><code>nonvoluntary<\/code>: \u7531\u5185\u6838\u8c03\u5ea6\u7684\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u8ba1\u6570<\/li>\n<\/ul>\n\n\n\n<p>\u5728\u4e92\u65a5\u9501\u7684\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u671f\u671b\u5176\u662f\u4e3b\u8c03\u8c03\u5ea6\u8fdb\u5185\u6838\uff0c\u800c\u6d4b\u8bd5\u81ea\u65cb\u9501\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\u5b83\u53ea\u4f1a\u88ab\u5185\u6838\u8c03\u5ea6\u51fa\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-13.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"160\" data-original=\"http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-13.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-443\"  sizes=\"(max-width: 518px) 100vw, 518px\" \/><\/div><\/figure>\n\n\n\n<p>\u6d4b\u8bd5\u7ed3\u679c\u4e0e\u9884\u671f\u76f8\u7b26<\/p>\n\n\n\n<p>\u5728\u591a\u7ebf\u7a0b\u81ea\u65cb\u9501\u7684\u6d4b\u8bd5\u4e2d\uff0c\u5176\u786e\u5b9e\u4f1a\u6d88\u8017\u5927\u91cf\u7684cpu\u65f6\u95f4<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>taskset -c 6 .\/lock_test_mt<\/p>\n\n\n\n<p>perf top -C 6 -g<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-14.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"921\" height=\"206\" data-original=\"http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-14.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-444\"  sizes=\"(max-width: 921px) 100vw, 921px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bfb\u5199\u9501\u548cRCU<\/h2>\n\n\n\n<p>\u8bfb\u5199\u9501(RWLock) \u548c RCU(read-copy-update), \u90fd\u9002\u7528\u4e8e\u8bfb\u591a\u5199\u5c11\u7684\u573a\u666f\uff0c\u5176\u4e3b\u8981\u7684\u533a\u522b\u5728\u4e8e<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>RWLock<\/code>: \u5141\u8bb8\u540c\u65f6\u8bfb\uff0c\u4f46\u662f\u5199\u8005\u4e0e\u4efb\u4f55\u8bfb\u8005\u90fd\u4e92\u65a5\uff0c\u5199\u65f6\u4f1a\u963b\u585e<\/li>\n\n\n\n<li><code>RCU<\/code>: \u8bfb\u65f6\u51e0\u4e4e\u65e0\u9501\uff0c\u5199\u8005<strong>\u590d\u5236\u65b0\u7248\u672c\u2014\u2014\u539f\u5b50\u53d1\u5e03\u2014\u2014\u7b49\u5f85\u5bbd\u9650\u671f\u56de\u6536\u65e7\u7248\u672c<\/strong>\uff0c\u8bfb\u8005\u53ef\u80fd\u8bfb\u5230\u7a0d\u65e7\u7684\u5feb\u7167\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ef4\u5ea6\u5bf9\u6bd4<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u7ef4\u5ea6<\/td><td>RWLock<\/td><td>RCU<\/td><\/tr><tr><td>\u8bfb\u8def\u5f84<\/td><td>\u83b7\u53d6\u5171\u4eab\u9501\uff08\u6709\u9501\u64cd\u4f5c\uff09<\/td><td>\u8fdb\u5165\u8bfb\u6bb5\uff08\u8f7b\u91cf\u6807\u8bb0\/\u5c4f\u969c\uff0c\u65e0\u9501\u6216\u8fd1\u4f3c\u65e0\u9501\uff09<\/td><\/tr><tr><td>\u5199\u8def\u5f84<\/td><td>\u72ec\u5360\u9501\uff0c\u7b49\u5f85\u6240\u6709\u8bfb\u8005\u9000\u51fa\u540e\u5199<\/td><td>\u590d\u5236\u65b0\u6570\u636e\u5e76\u539f\u5b50\u5207\u6362\u6307\u9488\uff1b\u4e0d\u963b\u585e\u5728\u8bfb\u8005\u4e0a<\/td><\/tr><tr><td>\u8bfb\u53ef\u89c1\u6027<\/td><td>\u8bfb\u5230\u6700\u65b0\u4e14\u4e00\u81f4\u7684\u6570\u636e<\/td><td>\u8bfb\u5230\u4e00\u81f4\u4f46\u53ef\u80fd\u8fc7\u671f\u7684\u5feb\u7167<\/td><\/tr><tr><td>\u5ef6\u8fdf\u7279\u6027<\/td><td>\u5199\u4f1a\u963b\u585e\u65b0\u8bfb\u8005\uff1b\u5927\u91cf\u8bfb\u8005\u65f6\u5199\u8005\u53ef\u80fd\u9965\u997f<\/td><td>\u5199\u7684\u53d1\u5e03\u5f88\u5feb\uff0c\u4f46\u56de\u6536\u8981\u7b49\u5bbd\u9650\u671f\uff1b\u8bfb\u57fa\u672c\u6052\u5b9a\u4f4e\u5ef6\u8fdf<\/td><\/tr><tr><td>\u6269\u5c55\u6027\uff08\u591a\u6838\u591a\u8bfb\uff09<\/td><td>\u9501\u5143\u6570\u636e\u4e89\u7528\u663e\u8457\uff0c\u6269\u5c55\u6027\u4e00\u822c<\/td><td>\u6781\u4f73\uff0c\u8bfb\u8def\u5f84\u51e0\u4e4e\u4e0d\u89e6\u78b0\u5171\u4eab\u72b6\u6001<\/td><\/tr><tr><td>\u5b9e\u73b0\u590d\u6742\u5ea6<\/td><td>\u4f4e\uff08\u5e93\u73b0\u6210\uff0c\u6613\u7528\uff09<\/td><td>\u9ad8\uff08\u7248\u672c\u7ba1\u7406\u3001\u5185\u5b58\u56de\u6536\u3001\u5185\u5b58\u5c4f\u969c\uff09<\/td><\/tr><tr><td>\u5185\u5b58\u5f00\u9500<\/td><td>\u57fa\u672c\u65e0\u989d\u5916\u526f\u672c<\/td><td>\u5199\u65f6\u9700\u8981\u526f\u672c\/\u591a\u7248\u672c\uff0c\u5e76\u6709\u5ef6\u8fdf\u56de\u6536<\/td><\/tr><tr><td>\u9002\u7528\u6570\u636e\u7ed3\u6784<\/td><td>\u901a\u7528<\/td><td>\u6307\u9488\/\u5f15\u7528\u4e3a\u4e3b\u7684\u7ed3\u6784\uff08\u8868\u3001\u94fe\u8868\u3001\u6811\u3001map \u7b49\uff09\u66f4\u81ea\u7136<\/td><\/tr><tr><td>\u4e00\u81f4\u6027\u9700\u6c42<\/td><td>\u5f3a\u4e00\u81f4\uff08\u5199\u540e\u8bfb\u7acb\u5373\u53ef\u89c1\uff09<\/td><td>\u6700\u7ec8\u4e00\u81f4\uff08\u5141\u8bb8\u77ed\u6682\u9648\u65e7\uff09<\/td><\/tr><tr><td>\u5199\u6bd4\u4f8b\u8f83\u9ad8\u65f6<\/td><td>\u5bb9\u6613\u9000\u5316\uff08\u5199\u963b\u585e\u8bfb\uff09<\/td><td>\u590d\u5236\u6210\u672c\u9ad8\u3001\u56de\u6536\u538b\u529b\u5927\uff0c\u6536\u76ca\u53d8\u5c0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f<\/h3>\n\n\n\n<p><strong>RWLock<\/strong> \u5f53\u4f60\u9700\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bfb\u5fc5\u987b\u770b\u5230<strong>\u6700\u65b0<\/strong>\u7ed3\u679c\uff08\u5f3a\u4e00\u81f4\uff09<\/li>\n\n\n\n<li>\u5199\u4e0d\u9891\u7e41\u4e14\u53ef\u63a5\u53d7\u5199\u671f\u95f4<strong>\u6682\u505c\u8bfb<\/strong><\/li>\n\n\n\n<li>\u4ee3\u7801\u8981<strong>\u7b80\u5355\u53ef\u7ef4\u62a4<\/strong>\uff0c\u56e2\u961f\u5bf9\u5e76\u53d1\u6280\u5de7\u4e0d\u719f<\/li>\n<\/ul>\n\n\n\n<p><strong>RCU<\/strong> \u5f53\u4f60\u9700\u8981\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6781\u9ad8\u8bfb\u541e\u5410\u4e0e\u4f4e\u8bfb\u5ef6\u8fdf<\/strong>\uff08\u70ed\u70b9\u8bfb\u3001\u591a\u6838\u6269\u5c55\uff09<\/li>\n\n\n\n<li>\u8bfb\u80fd\u63a5\u53d7**\u201c\u7a0d\u65e7\u4e5f\u884c\u201d**\u7684\u5feb\u7167\u4e00\u81f4\u6027<\/li>\n\n\n\n<li>\u6570\u636e\u7ed3\u6784\u4ee5<strong>\u6307\u9488\/\u5f15\u7528<\/strong>\u4e3a\u4e3b\uff0c\u66f4\u65b0\u53ef\u4ee5<strong>\u590d\u5236\u65b0\u7248\u672c\u518d\u5207\u6362<\/strong><\/li>\n\n\n\n<li>\u7cfb\u7edf\u5141\u8bb8<strong>\u5ef6\u8fdf\u56de\u6536<\/strong>\u4e0e\u9002\u5ea6\u7684\u5185\u5b58\u653e\u5927<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">RWLock<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bfb\u4f18\u5148\u548c\u5199\u4f18\u5148<\/h4>\n\n\n\n<p>\u8bfb\u5199\u9501\u53ef\u4ee5\u8bbe\u7f6e\u8bfb\u4f18\u5148\u548c\u5199\u4f18\u5148<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u7b56\u7565<\/td><td>\u884c\u4e3a\u7279\u70b9<\/td><td>\u4f18\u70b9<\/td><td>\u7f3a\u70b9<\/td><td>\u9002\u7528\u573a\u666f<\/td><\/tr><tr><td>\u8bfb\u4f18\u5148<\/td><td>\u6709\u5199\u8005\u7b49\u5f85\u65f6\u4ecd\u5141\u8bb8\u65b0\u8bfb\u8005\u8fdb\u5165<\/td><td>\u8bfb\u5ef6\u8fdf\u4f4e\uff0c\u8bfb\u541e\u5410\u91cf\u6781\u9ad8<\/td><td>\u5199\u8005\u53ef\u80fd\u957f\u65f6\u95f4\u9965\u997f<\/td><td>\u8bfb\u6781\u591a\u5199\u6781\u5c11\u7684\u573a\u666f<\/td><\/tr><tr><td>\u5199\u4f18\u5148<\/td><td>\u6709\u5199\u8005\u7b49\u5f85\u65f6\u963b\u585e\u65b0\u8bfb\u8005<\/td><td>\u907f\u514d\u5199\u9965\u997f\uff0c\u5199\u5ef6\u8fdf\u53ef\u63a7<\/td><td>\u8bfb\u8005\u5ef6\u8fdf\u53d8\u9ad8\uff0c\u8bfb\u541e\u5410\u4e0b\u964d<\/td><td>\u5199\u64cd\u4f5c\u65f6\u6548\u6027\u8981\u6c42\u9ad8<\/td><\/tr><tr><td>\u516c\u5e73\u7b56\u7565<\/td><td>\u6309\u5230\u8fbe\u987a\u5e8f\u6392\u961f<\/td><td>\u65e0\u9965\u997f\uff0c\u6027\u80fd\u5747\u8861<\/td><td>\u541e\u5410\u91cf\u7565\u4f4e\u4e8e\u504f\u5411\u7b56\u7565<\/td><td>\u9700\u8981\u6574\u4f53\u516c\u5e73\u8c03\u5ea6\u7684\u7cfb\u7edf<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u603b\u7ed3\u9009\u62e9\u5efa\u8bae<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bfb\u6781\u591a\u5199\u6781\u5c11<\/strong>\uff1a\u8bfb\u4f18\u5148\uff0c\u6700\u5927\u5316\u8bfb\u541e\u5410\u3002<\/li>\n\n\n\n<li><strong>\u5199\u9700\u8981\u53ca\u65f6\u751f\u6548<\/strong>\uff1a\u5199\u4f18\u5148\uff0c\u907f\u514d\u5199\u8005\u9965\u997f\u3002<\/li>\n\n\n\n<li><strong>\u516c\u5e73\u6027\u91cd\u8981\u6216\u8bfb\u5199\u6bd4\u4f8b\u5747\u8861<\/strong>\uff1a\u516c\u5e73\u7b56\u7565\uff08\u4f8b\u5982 Java \u7684 <code>ReentrantReadWriteLock(true)<\/code> \u6784\u9020\u516c\u5e73\u9501\uff09\u3002<\/li>\n\n\n\n<li>\u5b9e\u9645\u5e94\u7528\uff1a\u5f88\u591a\u6807\u51c6\u5e93\uff08\u5982 <code>std::shared_mutex<\/code>\uff09\u662f<strong>\u8bfb\u4f18\u5148<\/strong>\uff0c\u4f46\u6570\u636e\u5e93\u548c\u9ad8\u5b9e\u65f6\u7cfb\u7edf\u5f80\u5f80\u503e\u5411<strong>\u5199\u4f18\u5148<\/strong>\u6216\u516c\u5e73\u7b56\u7565\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u9012\u5f52\u9501<\/h2>\n\n\n\n<p>\u9012\u5f52\u9501\u7684\u7279\u70b9\u662f\uff1a<strong>\u540c\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u591a\u6b21\u83b7\u5f97\u540c\u4e00\u628a\u9501<\/strong>\uff0c\u6bcf\u6b21\u83b7\u5f97\u90fd\u4f1a\u589e\u52a0\u4e00\u4e2a\u8ba1\u6570\uff1b\u53ea\u6709\u5f53\u76f8\u540c\u7ebf\u7a0b\u8c03\u7528\u5bf9\u5e94\u6b21\u6570\u7684 <code>unlock<\/code> \u540e\uff0c\u9501\u624d\u771f\u6b63\u91ca\u653e\u7ed9\u5176\u4ed6\u7ebf\u7a0b\u3002 \u6362\u53e5\u8bdd\u8bf4\uff1a\u9012\u5f52\u9501\u5141\u8bb8<code>\u91cd\u5165<\/code>\u2014\u2014\u5f53\u6301\u9501\u7ebf\u7a0b\u5728\u4e34\u754c\u533a\u5185\u8c03\u7528\u4f1a\u518d\u6b21\u5c1d\u8bd5\u52a0\u540c\u4e00\u628a\u9501\u7684\u51fd\u6570\u65f6\uff0c\u4e0d\u4f1a\u6b7b\u9501\uff0c\u800c\u662f\u589e\u52a0\u8ba1\u6570\u5e76\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u6bd4\u8f83\u7f55\u89c1\u7684\u9501\u7c7b\u578b<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u9700\u8981\uff08\u5178\u578b\u573a\u666f\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A \u51fd\u6570\u52a0\u9501\u540e\u8c03\u7528 B\uff0cB \u4e5f\u53ef\u80fd\u95f4\u63a5\u8c03\u7528\u5230 A\uff08\u6216\u5176\u4ed6\u4e5f\u52a0\u540c\u4e00\u628a\u9501\u7684\u51fd\u6570\uff09\uff0c\u5982\u679c\u4f7f\u7528\u666e\u901a\u4e92\u65a5\u9501\uff0c\u4f1a\u81ea\u6b7b\u9501\uff1b\u9012\u5f52\u9501\u80fd\u907f\u514d\u8fd9\u79cd\u201c\u81ea\u5df1\u7ed9\u81ea\u5df1\u9501\u4f4f\u201d\u7684\u60c5\u51b5\u3002<\/li>\n\n\n\n<li>\u5e38\u89c1\u4e8e\u9762\u5411\u5bf9\u8c61\u7684\u5b9e\u4f8b\u65b9\u6cd5\u76f8\u4e92\u8c03\u7528\uff0c\u4e14\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u4fdd\u62a4\u540c\u4e00\u4efd\u72b6\u6001\u65f6\uff08e.g. \u5bf9\u8c61\u65b9\u6cd5\u7528\u9501\u4fdd\u62a4\u6210\u5458\u53d8\u91cf\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u9a8c\u8bc1<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>#include &lt;iostream>\n#include &lt;thread>\n#include &lt;mutex>\n\n\/\/std::mutex mtx; \/\/ \u666e\u901a\u4e92\u65a5\u9501\uff0c\u66ff\u6362\u4e3a std::recursive_mutex \u53ef\u89e3\u51b3\u91cd\u5165\nstd::recursive_mutex mtx;\n\nvoid inner(int n) {\n    std::lock_guard&lt;std::recursive_mutex> lk(mtx); \/\/ \u82e5 mtx \u662f std::mutex\uff0c\u7b2c\u4e8c\u6b21 lock \u5c06\u6b7b\u9501\n    std::cout &lt;&lt; \"inner \" &lt;&lt; n &lt;&lt; \" locked by thread \" &lt;&lt; std::this_thread::get_id() &lt;&lt; \"\\n\";\n    if (n > 0) inner(n - 1);\n}\n\nint main() {\n    std::thread t([]{\n        \/\/ \u5982\u679c mtx \u662f std::mutex\uff0c\u8fd9\u91cc\u7b2c\u4e00\u6b21 lock \u540e\uff0cinner \u518d\u6b21 lock \u4f1a\u6b7b\u9501\u3002\n        \/\/ \u5c06\u4e0a\u65b9 mtx \u6539\u6210 std::recursive_mutex \u5e76\u628a lock_guard \u7c7b\u578b\u4e5f\u6539\u4e3a std::lock_guard&lt;std::recursive_mutex>\n        inner(2);\n    });\n    t.join();\n    std::cout &lt;&lt; \"done\\n\";\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">iostream<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">thread<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">#include<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&lt;<\/span><span style=\"color: #E7EE98\">mutex<\/span><span style=\"color: #DEE492\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">\/\/std::mutex mtx; \/\/ \u666e\u901a\u4e92\u65a5\u9501\uff0c\u66ff\u6362\u4e3a std::recursive_mutex \u53ef\u89e3\u51b3\u91cd\u5165<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">recursive_mutex mtx;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">void<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">inner<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #FFB86C; font-style: italic\">n<\/span><span style=\"color: #F6F6F4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">lock_guard<\/span><span style=\"color: #F286C4\">&lt;<\/span><span style=\"color: #F6F6F4\">std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">recursive_mutex<\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">lk<\/span><span style=\"color: #F6F6F4\">(mtx);<\/span><span style=\"color: #7B7F8B\"> \/\/ \u82e5 mtx \u662f std::mutex\uff0c\u7b2c\u4e8c\u6b21 lock \u5c06\u6b7b\u9501<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">inner <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> n <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\"> locked by thread <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">this_thread<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #62E884\">get_id<\/span><span style=\"color: #F6F6F4\">() <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F286C4\">\\n<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><span style=\"color: #F286C4\">if<\/span><span style=\"color: #F6F6F4\"> (n <\/span><span style=\"color: #F286C4\">&gt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">0<\/span><span style=\"color: #F6F6F4\">) <\/span><span style=\"color: #62E884\">inner<\/span><span style=\"color: #F6F6F4\">(n <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">int<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">main<\/span><span style=\"color: #F6F6F4\">() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">thread <\/span><span style=\"color: #62E884\">t<\/span><span style=\"color: #F6F6F4\">([]{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">        \/\/ \u5982\u679c mtx \u662f std::mutex\uff0c\u8fd9\u91cc\u7b2c\u4e00\u6b21 lock \u540e\uff0cinner \u518d\u6b21 lock \u4f1a\u6b7b\u9501\u3002<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">        \/\/ \u5c06\u4e0a\u65b9 mtx \u6539\u6210 std::recursive_mutex \u5e76\u628a lock_guard \u7c7b\u578b\u4e5f\u6539\u4e3a std::lock_guard&lt;std::recursive_mutex&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #62E884\">inner<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">2<\/span><span style=\"color: #F6F6F4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    t.<\/span><span style=\"color: #62E884\">join<\/span><span style=\"color: #F6F6F4\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    std<\/span><span style=\"color: #F286C4\">::<\/span><span style=\"color: #F6F6F4\">cout <\/span><span style=\"color: #F286C4\">&lt;&lt;<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #E7EE98\">done<\/span><span style=\"color: #F286C4\">\\n<\/span><span style=\"color: #DEE492\">&quot;<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4e92\u65a5\u9501\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-15.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"78\" data-original=\"http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-15.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-446\"  sizes=\"(max-width: 478px) 100vw, 478px\" \/><\/div><\/figure>\n\n\n\n<p>\u9012\u5f52\u9501\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-16.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"464\" height=\"104\" data-original=\"http:\/\/yangnanbei.top\/wp-content\/uploads\/2025\/08\/image-16.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-447\"  sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u4fe1\u53f7\u91cf\u548c\u6761\u4ef6\u53d8\u91cf<\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u6211\u8ba4\u4e3a\u4e25\u683c\u610f\u4e49\u4e0a\u4e0d\u7b97\u9501\u5427\uff0c\u4e00\u8005\u662f\u8d44\u6e90\u7684\u9650\u5236\uff0c\u4e00\u8005\u662f\u4e8b\u4ef6\u7684\u901a\u77e5\u673a\u5236\u3002\u6682\u4e14\u4e0d\u8868\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e50\u89c2\u9501\u548c\u60b2\u89c2\u9501<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\u4e50\u89c2\u9501<\/code>\uff1a\u5047\u8bbe\u51b2\u7a81\u5f88\u5c11\uff0c\u8981\u64cd\u4f5c\u6570\u636e\u65f6\u5148\u4e0d\u4e0a\u9501\u3002\u7531\u4e8e\u4e50\u89c2\u9501\u5168\u7a0b\u4e0d\u52a0\u9501\uff0c\u4e5f\u53ef\u53eb\u505a\u65e0\u9501\u7f16\u7a0b<\/li>\n\n\n\n<li><code>\u60b2\u89c2\u9501<\/code>: \u5047\u8bbe\u51b2\u7a81\u5f88\u591a\uff0c\u64cd\u4f5c\u524d\u5148\u4e0a\u9501<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u7ef4\u5ea6<\/td><td>\u60b2\u89c2\u9501<\/td><td>\u4e50\u89c2\u9501<\/td><\/tr><tr><td>\u6210\u672c\uff08\u8bfb\u8def\u5f84\uff09<\/td><td>\u8bfb\u53ef\u80fd\u88ab\u963b\u585e\uff0c\u9501\u5f00\u9500<\/td><td>\u8bfb\u65e0\u9501\uff0c\u4f4e\u5ef6\u8fdf<\/td><\/tr><tr><td>\u6210\u672c\uff08\u5199\u8def\u5f84\uff09<\/td><td>\u5199\u65f6\u72ec\u5360\uff0c\u5f00\u9500\u76f4\u89c2<\/td><td>\u5199\u8981\u505a\u51b2\u7a81\u68c0\u6d4b\u4e0e\u91cd\u8bd5<\/td><\/tr><tr><td>\u5e76\u53d1\u541e\u5410<\/td><td>\u5199\u591a\/\u51b2\u7a81\u591a\u65f6\u66f4\u7a33\u5065<\/td><td>\u5199\u5c11\u65f6\u541e\u5410\u9ad8\uff0c\u5199\u591a\u65f6\u9000\u5316<\/td><\/tr><tr><td>\u4e00\u81f4\u6027<\/td><td>\u5f3a\uff08\u5199\u671f\u95f4\u65e0\u5e76\u53d1\u5e72\u6270\uff09<\/td><td>\u6700\u7ec8\u4e00\u81f4\/\u57fa\u4e8e\u7248\u672c\u6821\u9a8c<\/td><\/tr><tr><td>\u5b9e\u73b0\u590d\u6742\u5ea6<\/td><td>\u7b80\u5355\u76f4\u63a5<\/td><td>\u903b\u8f91\u4e0a\u9700\u8981\u91cd\u8bd5\/\u56de\u9000\u548c\u7248\u672c\u7ba1\u7406<\/td><\/tr><tr><td>\u6b7b\u9501\u98ce\u9669<\/td><td>\u5b58\u5728\uff08\u8981\u6ce8\u610f\u9501\u987a\u5e8f\uff09<\/td><td>\u65e0\u9501\u6216\u5c11\u9501\uff0c\u6b7b\u9501\u98ce\u9669\u4f4e<\/td><\/tr><tr><td>\u8d44\u6e90\u5360\u7528<\/td><td>\u53ef\u80fd\u957f\u65f6\u95f4\u6301\u6709\u9501\u963b\u585e\u4ed6\u4eba<\/td><td>\u53ef\u80fd\u591a\u6b21\u590d\u5236\/\u91cd\u8bd5\u5bfc\u81f4 CPU\/\u5185\u5b58\u5f00\u9500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f8\u5173\u573a\u666f<\/h3>\n\n\n\n<p>\u60b2\u89c2\u9501\uff1a\u9002\u5408\u5e76\u53d1\u51b2\u7a81\u591a\uff0c\u5199\u591a\u8bfb\u5c11\u7684\u573a\u666f<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>START TRANSACTION;\n-- \u9501\u5b9a\u884c\uff0c\u76f4\u5230\u4e8b\u52a1\u63d0\u4ea4\/\u56de\u6eda\nSELECT balance FROM account WHERE id = 123 FOR UPDATE;\n\n-- \u8fdb\u884c\u66f4\u65b0\nUPDATE account SET balance = balance - 100 WHERE id = 123;\n\nCOMMIT;\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F286C4\">START TRANSACTION<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">-- \u9501\u5b9a\u884c\uff0c\u76f4\u5230\u4e8b\u52a1\u63d0\u4ea4\/\u56de\u6eda<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">SELECT<\/span><span style=\"color: #F6F6F4\"> balance <\/span><span style=\"color: #F286C4\">FROM<\/span><span style=\"color: #F6F6F4\"> account <\/span><span style=\"color: #F286C4\">WHERE<\/span><span style=\"color: #F6F6F4\"> id <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">123<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">FOR<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">UPDATE<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">-- \u8fdb\u884c\u66f4\u65b0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">UPDATE<\/span><span style=\"color: #F6F6F4\"> account <\/span><span style=\"color: #F286C4\">SET<\/span><span style=\"color: #F6F6F4\"> balance <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> balance <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">100<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">WHERE<\/span><span style=\"color: #F6F6F4\"> id <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">123<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">COMMIT<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u4e50\u89c2\u9501\uff1a\u6570\u636e\u5e93\uff0c\u5728\u7ebf\u6587\u6863\uff0csvn\uff0cgit<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#f6f6f4;display:none\" aria-label=\"\u590d\u5236\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>-- \u8bfb\nSELECT balance, version FROM account WHERE id = 123;\n\n-- \u5047\u8bbe\u8bfb\u5230 version = 4\uff0c\u5c1d\u8bd5\u66f4\u65b0\u65f6\u5e26\u7248\u672c\u6761\u4ef6\nUPDATE account\nSET balance = balance - 100, version = version + 1\nWHERE id = 123 AND version = 4;\n\n-- \u82e5\u53d7\u5f71\u54cd\u884c\u6570 == 0 => \u6709\u5e76\u53d1\u5199\uff0c\u68c0\u6d4b\u5230\u51b2\u7a81\uff0c\u9700\u8981\u91cd\u8bd5\u6216\u62a5\u9519\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #7B7F8B\">-- \u8bfb<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">SELECT<\/span><span style=\"color: #F6F6F4\"> balance, <\/span><span style=\"color: #F286C4\">version<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">FROM<\/span><span style=\"color: #F6F6F4\"> account <\/span><span style=\"color: #F286C4\">WHERE<\/span><span style=\"color: #F6F6F4\"> id <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">123<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">-- \u5047\u8bbe\u8bfb\u5230 version = 4\uff0c\u5c1d\u8bd5\u66f4\u65b0\u65f6\u5e26\u7248\u672c\u6761\u4ef6<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">UPDATE<\/span><span style=\"color: #F6F6F4\"> account<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">SET<\/span><span style=\"color: #F6F6F4\"> balance <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> balance <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">100<\/span><span style=\"color: #F6F6F4\">, <\/span><span style=\"color: #F286C4\">version<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">version<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">+<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F286C4\">WHERE<\/span><span style=\"color: #F6F6F4\"> id <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">123<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">AND<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">version<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">4<\/span><span style=\"color: #F6F6F4\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #7B7F8B\">-- \u82e5\u53d7\u5f71\u54cd\u884c\u6570 == 0 =&gt; \u6709\u5e76\u53d1\u5199\uff0c\u68c0\u6d4b\u5230\u51b2\u7a81\uff0c\u9700\u8981\u91cd\u8bd5\u6216\u62a5\u9519<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\u89c4\u5f8b<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>\u51b2\u7a81\u5c11\uff0c\u5c31\u7528\u4e50\u89c2\u9501. \u4e0d\u52a0\u9501\uff0c\u7528\u7248\u672c\u53f7\u6216\u8005\u65f6\u95f4\u6233\u6821\u9a8c\uff0c\u6027\u80fd\u9ad8\u3002<\/li>\n\n\n\n<li>\u51b2\u7a81\u591a\uff0c\u5c31\u7528\u60b2\u89c2\u9501. \u76f4\u63a5\u4e0a\u9501\uff0c\u4e0d\u4f1a\u5931\u8d25\uff0c\u5c31\u662f\u6027\u80fd\u4f4e\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">stl\u4e2d\u6709\u54ea\u4e9b\u9501<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u540d\u79f0<\/td><td>\u5934\u6587\u4ef6<\/td><td>\u7279\u70b9<\/td><td>\u7528\u9014<\/td><\/tr><tr><td>std::mutex<\/td><td>&lt;mutex&gt;<\/td><td>\u666e\u901a\u4e92\u65a5\u9501\uff0c\u4e0d\u53ef\u9012\u5f52<\/td><td>\u57fa\u672c\u7ebf\u7a0b\u540c\u6b65\uff0c\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90<\/td><\/tr><tr><td>std::recursive_mutex<\/td><td>&lt;mutex&gt;<\/td><td>\u5141\u8bb8\u540c\u4e00\u7ebf\u7a0b\u591a\u6b21\u52a0\u9501<\/td><td>\u907f\u514d\u9012\u5f52\u8c03\u7528\u65f6\u6b7b\u9501<\/td><\/tr><tr><td>std::timed_mutex<\/td><td>&lt;mutex&gt;<\/td><td>\u652f\u6301 try_lock_for \u548c try_lock_until<\/td><td>\u9650\u65f6\u5c1d\u8bd5\u52a0\u9501<\/td><\/tr><tr><td>std::recursive_timed_mutex<\/td><td>&lt;mutex&gt;<\/td><td>\u9012\u5f52 + \u9650\u65f6\u52a0\u9501<\/td><td>\u7075\u6d3b\u8d85\u65f6\u63a7\u5236<\/td><\/tr><tr><td>std::shared_mutex (C++17)<\/td><td>&lt;shared_mutex&gt;<\/td><td>\u591a\u8bfb\u5355\u5199\u9501<\/td><td>\u8bfb\u591a\u5199\u5c11\u573a\u666f<\/td><\/tr><tr><td>std::shared_timed_mutex (C++14)<\/td><td>&lt;shared_mutex&gt;<\/td><td>\u5171\u4eab\u9501+\u9650\u65f6\u9501<\/td><td>C++17\u524d\u7684\u8fc7\u6e21\u7248\u672c<\/td><\/tr><tr><td>std::lock_guard<\/td><td>&lt;mutex&gt;<\/td><td>\u7b80\u5355\u7684RAII\u52a0\u9501\u5668<\/td><td>\u4f5c\u7528\u57df\u9501\uff0c\u81ea\u52a8\u89e3\u9501<\/td><\/tr><tr><td>std::unique_lock<\/td><td>&lt;mutex&gt;<\/td><td>\u66f4\u7075\u6d3b\u7684RAII\u9501<\/td><td>\u53ef\u5ef6\u8fdf\u4e0a\u9501\u3001\u89e3\u9501\u3001\u8f6c\u79fb<\/td><\/tr><tr><td>std::scoped_lock (C++17)<\/td><td>&lt;mutex&gt;<\/td><td>\u540c\u65f6\u9501\u591a\u4e2a\u4e92\u65a5\u91cf<\/td><td>\u6b7b\u9501\u5b89\u5168\u7684\u591a\u9501\u65b9\u6848<\/td><\/tr><tr><td>std::condition_variable<\/td><td>&lt;condition_variable&gt;<\/td><td>\u7ebf\u7a0b\u7b49\u5f85\/\u901a\u77e5<\/td><td>\u7ed3\u5408 std::unique_lock \u7528<\/td><\/tr><tr><td>std::condition_variable_any<\/td><td>&lt;condition_variable&gt;<\/td><td>\u652f\u6301\u4efb\u610f\u9501\u7c7b\u578b<\/td><td>\u6bd4 condition_variable \u7075\u6d3b<\/td><\/tr><tr><td>std::once_flag + std::call_once<\/td><td>&lt;mutex&gt;<\/td><td>\u4fdd\u8bc1\u53ea\u6267\u884c\u4e00\u6b21<\/td><td>\u5355\u4f8b\u6a21\u5f0f\u521d\u59cb\u5316<\/td><\/tr><tr><td>std::future \/ std::promise<\/td><td>&lt;future&gt;<\/td><td>\u5f02\u6b65\u7ed3\u679c\u540c\u6b65<\/td><td>\u4efb\u52a1\u7ed3\u679c\u4f20\u9012<\/td><\/tr><tr><td>std::atomic<\/td><td>&lt;atomic&gt;<\/td><td>\u539f\u5b50\u64cd\u4f5c<\/td><td>\u65e0\u9501\u5e76\u53d1<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u8981\u8ba8\u8bba \u9501\u7684\u5206\u7c7b\uff0c\u9501\u7684\u7279\u70b9\uff0c\u9501\u7684\u5e94\u7528\u573a\u666f \u4e3a\u4ec0\u4e48\u8981\u6709\u9501\uff1f\u4e3a\u4e86\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u6570\u636e\u7ade\u4e89\u95ee\u9898\uff0c\u4fdd\u8bc1\u5171\u4eab\u8d44\u6e90\u5728\u540c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,74,73,3],"tags":[40,77,49,76,75],"class_list":["post-442","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-os","category-basic","category-coding_language","tag-c","tag-os","tag-49","tag-76","tag-75"],"_links":{"self":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts\/442"}],"collection":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":3,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"predecessor-version":[{"id":451,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/posts\/442\/revisions\/451"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/media\/399"}],"wp:attachment":[{"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yangnanbei.top\/index.php\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}