{"id":124732,"date":"2026-11-19T12:16:33","date_gmt":"2026-11-19T04:16:33","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/124732.html"},"modified":"2026-11-19T12:16:33","modified_gmt":"2026-11-19T04:16:33","slug":"c-%e7%ae%a1%e7%90%86%e4%ba%92%e6%96%a5%e9%87%8f%e9%94%81%e6%89%80%e6%9c%89%e6%9d%83-stdlock_guard%e3%80%81stdunique_lock%e3%80%81stdshared_lock%e3%80%81stdscoped_lock","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/124732.html","title":{"rendered":"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock"},"content":{"rendered":"<h2>C&#043;&#043; \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock<\/h2>\n<p>flyfish<\/p>\n<h3>\u4e00\u3001lock_guard&#xff1a;\u6700\u7b80\u5355\u7684\u4f5c\u7528\u57df\u4fdd\u62a4<\/h3>\n<p>\u6f14\u793a\u591a\u7ebf\u7a0b\u5e76\u53d1\u7d2f\u52a0\u4e00\u4e2a\u5168\u5c40\u8ba1\u6570\u5668&#xff0c;\u7528 lock_guard \u4fdd\u62a4\u4e34\u754c\u533a\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<\/p>\n<p>long counter &#061; 0;<br \/>\nstd::mutex mtx;<\/p>\n<p>void increment(int n) {<br \/>\n    for (int i &#061; 0; i &lt; n; i&#043;&#043;) {<br \/>\n        std::lock_guard&lt;std::mutex&gt; guard(mtx);<br \/>\n        counter&#043;&#043;;<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    std::thread t1(increment, 100000);<br \/>\n    std::thread t2(increment, 100000);<br \/>\n    t1.join();<br \/>\n    t2.join();<br \/>\n    std::cout &lt;&lt; &#034;counter &#061; &#034; &lt;&lt; counter &lt;&lt; std::endl;<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8f93\u51fa<\/p>\n<p>counter <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">200000<\/span><\/p>\n<p>lock_guard \u5728\u8fdb\u5165 for \u5faa\u73af\u4f53\u7684\u65f6\u5019\u6784\u9020&#xff0c;\u8c03\u7528 mtx.lock()&#xff0c;\u5728\u5faa\u73af\u4f53\u7ed3\u675f\u3001\u79bb\u5f00\u82b1\u62ec\u53f7\u7684\u65f6\u5019\u6790\u6784&#xff0c;\u8c03\u7528 mtx.unlock()\u3002\u6bcf\u4e00\u8f6e\u5faa\u73af\u52a0\u9501\u4e00\u6b21\u3001\u89e3\u9501\u4e00\u6b21&#xff0c;\u7c92\u5ea6\u521a\u597d\u5305\u4f4f counter&#043;&#043; \u8fd9\u4e00\u884c\u3002<\/p>\n<p>\u5b83\u7684\u7279\u70b9\u662f\u5b8c\u5168\u4e0d\u80fd\u5e72\u9884\u9501\u7684\u8fc7\u7a0b\u3002\u6784\u9020\u5b8c\u5c31\u62ff\u7740\u9501&#xff0c;\u4e2d\u95f4\u6ca1\u6709\u4efb\u4f55\u63a5\u53e3\u63d0\u524d\u89e3\u9501&#xff0c;\u4e5f\u4e0d\u80fd\u628a\u9501\u8f6c\u79fb\u7ed9\u522b\u4eba\u3002\u8fd9\u79cd\u4e25\u683c\u53cd\u800c\u662f\u4f18\u70b9&#xff1a;\u4ee3\u7801\u610f\u56fe\u975e\u5e38\u6e05\u6670&#xff0c;\u5c31\u662f\u8fd9\u6bb5\u4ee3\u7801\u5fc5\u987b\u4e92\u65a5\u6267\u884c&#xff0c;\u6ca1\u6709\u591a\u4f59\u7684\u7075\u6d3b\u5ea6&#xff0c;\u4e5f\u5c31\u4e0d\u4f1a\u51fa\u9519\u3002<\/p>\n<h3>\u4e8c\u3001unique_lock&#xff1a;\u914d\u5408\u6761\u4ef6\u53d8\u91cf\u505a\u7ebf\u7a0b\u7b49\u5f85\u901a\u77e5<\/h3>\n<p>\u6f14\u793a\u4e00\u4e2a\u7ecf\u5178\u7684\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u961f\u5217&#xff1a;\u4e00\u4e2a\u7ebf\u7a0b\u5f80\u961f\u5217\u653e\u6570\u636e&#xff0c;\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4ece\u961f\u5217\u53d6\u6570\u636e&#xff0c;\u961f\u5217\u4e3a\u7a7a\u65f6\u6d88\u8d39\u8005\u963b\u585e\u7b49\u5f85&#xff0c;\u751f\u4ea7\u8005\u653e\u5165\u6570\u636e\u540e\u5524\u9192\u6d88\u8d39\u8005\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;condition_variable&gt;<br \/>\n#include &lt;queue&gt;<\/p>\n<p>std::queue&lt;int&gt; q;<br \/>\nstd::mutex mtx;<br \/>\nstd::condition_variable cv;<\/p>\n<p>void producer() {<br \/>\n    for (int i &#061; 1; i &lt;&#061; 5; i&#043;&#043;) {<br \/>\n        {<br \/>\n            std::unique_lock&lt;std::mutex&gt; lock(mtx);<br \/>\n            q.push(i);<br \/>\n            std::cout &lt;&lt; &#034;produced &#034; &lt;&lt; i &lt;&lt; std::endl;<br \/>\n        }<br \/>\n        cv.notify_one();<br \/>\n        std::this_thread::sleep_for(std::chrono::milliseconds(100));<br \/>\n    }<br \/>\n}<\/p>\n<p>void consumer() {<br \/>\n    for (int i &#061; 0; i &lt; 5; i&#043;&#043;) {<br \/>\n        std::unique_lock&lt;std::mutex&gt; lock(mtx);<br \/>\n        cv.wait(lock, [](){ return !q.empty(); });<br \/>\n        int val &#061; q.front();<br \/>\n        q.pop();<br \/>\n        std::cout &lt;&lt; &#034;consumed &#034; &lt;&lt; val &lt;&lt; std::endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    std::thread t1(producer);<br \/>\n    std::thread t2(consumer);<br \/>\n    t1.join();<br \/>\n    t2.join();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u8f93\u51fa<\/p>\n<p>produced <span class=\"token number\">1<\/span><br \/>\nconsumed <span class=\"token number\">1<\/span><br \/>\nproduced <span class=\"token number\">2<\/span><br \/>\nconsumed <span class=\"token number\">2<\/span><br \/>\nproduced <span class=\"token number\">3<\/span><br \/>\nconsumed <span class=\"token number\">3<\/span><br \/>\nproduced <span class=\"token number\">4<\/span><br \/>\nconsumed <span class=\"token number\">4<\/span><br \/>\nproduced <span class=\"token number\">5<\/span><br \/>\nconsumed <span class=\"token number\">5<\/span><\/p>\n<p>\u5173\u952e\u5728 cv.wait(lock, &#8230;) \u8fd9\u4e00\u884c\u3002\u8fd9\u4e2a\u51fd\u6570\u505a\u4e09\u4ef6\u4e8b&#xff1a;\u5148\u628a\u5f53\u524d\u7ebf\u7a0b\u6302\u8d77&#xff0c;\u7136\u540e\u4e34\u65f6\u91ca\u653e mutex&#xff0c;\u8ba9\u751f\u4ea7\u8005\u7ebf\u7a0b\u80fd\u62ff\u5230\u9501\u5f80\u961f\u5217\u91cc\u653e\u6570\u636e&#xff1b;\u7b49\u88ab notify \u5524\u9192\u540e&#xff0c;\u518d\u91cd\u65b0\u83b7\u53d6 mutex&#xff0c;\u7136\u540e\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u6ee1\u8db3&#xff0c;\u4e0d\u6ee1\u8db3\u5c31\u7ee7\u7eed\u6302\u8d77\u3002<\/p>\n<p>\u8fd9\u4e2a&#034;\u7b49\u5f85\u65f6\u91ca\u653e\u9501\u3001\u5524\u9192\u540e\u91cd\u65b0\u62ff\u9501&#034;\u7684\u8fc7\u7a0b&#xff0c;\u8981\u6c42\u9501\u5305\u88c5\u5668\u5fc5\u987b\u652f\u6301\u624b\u52a8\u89e3\u9501\u548c\u91cd\u65b0\u52a0\u9501&#xff0c;\u8fd8\u8981\u80fd\u8bb0\u5f55\u5f53\u524d\u662f\u5426\u6301\u6709\u9501\u3002lock_guard \u505a\u4e0d\u5230&#xff0c;\u56e0\u4e3a\u5b83\u4e00\u65e6\u6784\u9020\u5c31\u9501\u6b7b&#xff0c;\u4e2d\u95f4\u6ca1\u6709\u63a5\u53e3\u8ba9 wait \u51fd\u6570\u628a\u9501\u89e3\u5f00\u3002\u800c unique_lock \u63d0\u4f9b\u4e86 unlock() \u548c\u91cd\u65b0 lock() \u7684\u80fd\u529b&#xff0c;\u5185\u90e8\u8fd8\u6709\u4e00\u4e2a\u6807\u5fd7\u4f4d\u8bb0\u5f55\u9501\u7684\u6301\u6709\u72b6\u6001&#xff0c;\u6761\u4ef6\u53d8\u91cf\u7684 wait \u51fd\u6570\u6b63\u662f\u4f9d\u8d56\u8fd9\u4e9b\u63a5\u53e3\u5de5\u4f5c\u7684\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 C&#043;&#043; \u6807\u51c6\u89c4\u5b9a condition_variable::wait \u53ea\u63a5\u53d7 unique_lock&lt;std::mutex&gt;&#xff0c;\u4e0d\u63a5\u53d7 lock_guard\u3002unique_lock \u7684\u7075\u6d3b\u6027\u4e0d\u662f\u591a\u4f59\u7684&#xff0c;\u800c\u662f\u6761\u4ef6\u53d8\u91cf\u673a\u5236\u672c\u8eab\u5c31\u9700\u8981\u5b83\u3002<\/p>\n<p>C&#043;&#043; \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::unique_lock \u4ee3\u7801\u89e3\u91ca<\/p>\n<h3>\u4e09\u3001shared_lock&#xff1a;\u8bfb\u5199\u5206\u79bb\u7684\u5171\u4eab\u6570\u636e\u4fdd\u62a4<\/h3>\n<p>\u6f14\u793a\u4e00\u4e2a\u591a\u8bfb\u8005\u5355\u5199\u8005\u7684\u7f13\u5b58\u573a\u666f\u3002\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u8bfb\u7f13\u5b58\u5185\u5bb9&#xff0c;\u4f46\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u8981\u5199\u7f13\u5b58\u65f6&#xff0c;\u5fc5\u987b\u72ec\u5360&#xff0c;\u4e0d\u80fd\u548c\u5176\u4ed6\u8bfb\u6216\u5199\u540c\u65f6\u8fdb\u884c\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;shared_mutex&gt;<br \/>\n#include &lt;chrono&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>std::shared_mutex cache_mtx;<br \/>\nint cache_value &#061; 0;<br \/>\nstd::mutex print_mtx;<\/p>\n<p>void log(const std::string&amp; msg) {<br \/>\n    std::lock_guard&lt;std::mutex&gt; guard(print_mtx);<br \/>\n    std::cout &lt;&lt; msg &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>void reader(int id) {<br \/>\n    for (int i &#061; 0; i &lt; 3; i&#043;&#043;) {<br \/>\n        std::shared_lock&lt;std::shared_mutex&gt; lock(cache_mtx);<br \/>\n        log(&#034;reader &#034; &#043; std::to_string(id) &#043; &#034; reads value &#061; &#034; &#043; std::to_string(cache_value));<br \/>\n        std::this_thread::sleep_for(std::chrono::milliseconds(50));<br \/>\n    }<br \/>\n}<\/p>\n<p>void writer(int id) {<br \/>\n    for (int i &#061; 0; i &lt; 3; i&#043;&#043;) {<br \/>\n        std::unique_lock&lt;std::shared_mutex&gt; lock(cache_mtx);<br \/>\n        cache_value&#043;&#043;;<br \/>\n        log(&#034;writer &#034; &#043; std::to_string(id) &#043; &#034; writes value &#061; &#034; &#043; std::to_string(cache_value));<br \/>\n        std::this_thread::sleep_for(std::chrono::milliseconds(100));<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    std::thread r1(reader, 1);<br \/>\n    std::thread r2(reader, 2);<br \/>\n    std::thread r3(reader, 3);<br \/>\n    std::thread w1(writer, 1);<br \/>\n    r1.join();<br \/>\n    r2.join();<br \/>\n    r3.join();<br \/>\n    w1.join();<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u6ce8\u610f\u8bfb\u7ebf\u7a0b\u7528\u7684\u662f shared_lock&#xff0c;\u5199\u7ebf\u7a0b\u7528\u7684\u662f unique_lock&#xff0c;\u4e24\u8005\u4fdd\u62a4\u7684\u662f\u540c\u4e00\u4e2a std::shared_mutex\u3002<\/p>\n<p>shared_lock \u6784\u9020\u65f6\u8c03\u7528\u7684\u4e0d\u662f lock()&#xff0c;\u800c\u662f lock_shared()\u3002\u8fd9\u4e2a\u8c03\u7528\u7684\u542b\u4e49\u662f&#034;\u6211\u8981\u8bfb&#xff0c;\u591a\u4e2a\u8bfb\u8005\u53ef\u4ee5\u540c\u65f6\u8fdb\u6765&#034;\u3002\u53ea\u8981\u5f53\u524d\u6ca1\u6709\u5199\u8005\u6301\u6709\u9501&#xff0c;\u591a\u4e2a shared_lock \u53ef\u4ee5\u540c\u65f6\u6210\u529f&#xff0c;\u8bfb\u64cd\u4f5c\u771f\u6b63\u5e76\u884c\u6267\u884c\u3002<\/p>\n<p>\u800c\u5199\u7ebf\u7a0b\u7684 unique_lock \u8c03\u7528\u7684\u662f\u666e\u901a\u7684 lock()&#xff0c;\u542b\u4e49\u662f&#034;\u6211\u8981\u5199&#xff0c;\u6240\u6709\u4eba\u90fd\u5f97\u8ba9\u5f00&#034;\u3002\u5199\u8005\u62ff\u5230\u9501\u7684\u65f6\u5019&#xff0c;\u6240\u6709\u8bfb\u7ebf\u7a0b\u90fd\u5fc5\u987b\u7b49\u5199\u8005\u91ca\u653e\u540e\u624d\u80fd\u7ee7\u7eed\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u7528\u666e\u901a\u7684 lock_guard \u6216 unique_lock \u6765\u4fdd\u62a4\u8bfb\u64cd\u4f5c&#xff1f;\u56e0\u4e3a\u90a3\u6837\u8bfb\u7ebf\u7a0b\u4e4b\u95f4\u4e5f\u4f1a\u4e92\u65a5&#xff0c;\u8bfb\u591a\u5199\u5c11\u7684\u573a\u666f\u4e0b\u6027\u80fd\u4f1a\u5f88\u5dee\u3002shared_lock \u7684\u4ef7\u503c\u5c31\u662f\u628a&#034;\u8bfb&#034;\u548c&#034;\u5199&#034;\u533a\u5206\u5f00&#xff0c;\u8bfb\u64cd\u4f5c\u5171\u4eab\u5e76\u884c&#xff0c;\u5199\u64cd\u4f5c\u72ec\u5360\u4e32\u884c\u3002<\/p>\n<p>\u8fd9\u4e5f\u662f std::shared_mutex \u548c\u666e\u901a std::mutex \u7684\u672c\u8d28\u533a\u522b&#xff1a;\u666e\u901a mutex \u4e0d\u7ba1\u8bfb\u5199\u90fd\u72ec\u5360&#xff0c;shared_mutex \u533a\u5206\u8bfb\u5171\u4eab\u548c\u5199\u72ec\u5360\u3002<\/p>\n<p>C&#043;&#043; \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::shared_lock \u4ee3\u7801\u89e3\u91ca<\/p>\n<h3>\u56db\u3001scoped_lock&#xff1a;\u540c\u65f6\u6301\u6709\u591a\u628a\u9501\u907f\u514d\u6b7b\u9501<\/h3>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e24\u4e2a\u94f6\u884c\u8d26\u6237\u4e4b\u95f4\u8f6c\u8d26\u3002\u8f6c\u8d26\u64cd\u4f5c\u9700\u8981\u540c\u65f6\u9501\u4f4f\u8f6c\u51fa\u8d26\u6237\u548c\u8f6c\u5165\u8d26\u6237&#xff0c;\u4e24\u4e2a\u65b9\u5411\u7684\u8f6c\u8d26\u540c\u65f6\u53d1\u751f\u65f6&#xff0c;\u5982\u679c\u6309\u987a\u5e8f\u52a0\u9501\u5c31\u4f1a\u6b7b\u9501\u3002<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;mutex&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>struct Account {<br \/>\n    int balance;<br \/>\n    std::mutex mtx;<br \/>\n};<\/p>\n<p>std::mutex print_mtx;<\/p>\n<p>void log(const std::string&amp; msg) {<br \/>\n    std::lock_guard&lt;std::mutex&gt; guard(print_mtx);<br \/>\n    std::cout &lt;&lt; msg &lt;&lt; std::endl;<br \/>\n}<\/p>\n<p>void transfer(Account&amp; from, Account&amp; to, int amount) {<br \/>\n    std::scoped_lock&lt;std::mutex, std::mutex&gt; lock(from.mtx, to.mtx);<br \/>\n    from.balance -&#061; amount;<br \/>\n    to.balance &#043;&#061; amount;<br \/>\n    log(&#034;transfer &#034; &#043; std::to_string(amount) &#043; &#034; done, from balance &#061; &#034;<br \/>\n        &#043; std::to_string(from.balance) &#043; &#034;, to balance &#061; &#034;<br \/>\n        &#043; std::to_string(to.balance));<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    Account a{ 100, {} };<br \/>\n    Account b{ 100, {} };<\/p>\n<p>    std::thread t1(transfer, std::ref(a), std::ref(b), 50);<br \/>\n    std::thread t2(transfer, std::ref(b), std::ref(a), 30);<\/p>\n<p>    t1.join();<br \/>\n    t2.join();<\/p>\n<p>    std::scoped_lock&lt;std::mutex, std::mutex&gt; final_lock(a.mtx, b.mtx);<br \/>\n    log(&#034;final: a.balance &#061; &#034; &#043; std::to_string(a.balance)<br \/>\n        &#043; &#034;, b.balance &#061; &#034; &#043; std::to_string(b.balance));<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u8f93\u51fa<\/p>\n<p>transfer <span class=\"token number\">50<\/span> done, from balance <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">50<\/span>, to balance <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">150<\/span><br \/>\ntransfer <span class=\"token number\">30<\/span> done, from balance <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">120<\/span>, to balance <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">80<\/span><br \/>\nfinal: a.balance <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">80<\/span>, b.balance <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">120<\/span><\/p>\n<p>scoped_lock \u63a5\u6536\u4e86\u4e24\u4e2a\u4e92\u65a5\u91cf&#xff1a;from.mtx \u548c to.mtx\u3002\u5b83\u5728\u6784\u9020\u7684\u65f6\u5019&#xff0c;\u5185\u90e8\u7528 std::lock \u7684\u6b7b\u9501\u907f\u514d\u7b97\u6cd5\u628a\u8fd9\u4e24\u4e2a\u9501\u4e00\u6b21\u6027\u5168\u90e8\u62ff\u5230&#xff0c;\u800c\u4e0d\u662f\u7b80\u5355\u5730\u5148\u9501 from \u518d\u9501 to\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8fd9\u5f88\u91cd\u8981&#xff1f;\u5047\u8bbe\u4e0d\u7528 scoped_lock&#xff0c;\u4f60\u81ea\u5df1\u5199&#xff1a;<\/p>\n<p>std::lock_guard&lt;std::mutex&gt; l1(from.mtx);<br \/>\nstd::lock_guard&lt;std::mutex&gt; l2(to.mtx);<\/p>\n<p>\u5f53 t1 \u4ece a \u8f6c b \u65f6&#xff0c;\u5b83\u5148\u9501 a.mtx \u518d\u9501 b.mtx\u3002\u5f53 t2 \u4ece b \u8f6c a \u65f6&#xff0c;\u5b83\u5148\u9501 b.mtx \u518d\u9501 a.mtx\u3002\u4e24\u4e2a\u7ebf\u7a0b\u5404\u62ff\u4e00\u628a\u9501&#xff0c;\u4e92\u76f8\u7b49\u5bf9\u65b9\u624b\u91cc\u7684\u90a3\u628a&#xff0c;\u5c31\u6b7b\u9501\u4e86\u3002<\/p>\n<p>scoped_lock \u5185\u90e8\u7684\u505a\u6cd5\u662f&#xff0c;\u5b83\u4e0d\u4f1a\u56fa\u5b9a\u6309\u4f20\u5165\u987a\u5e8f\u52a0\u9501&#xff0c;\u800c\u662f\u7528\u4e00\u79cd\u907f\u514d\u6b7b\u9501\u7684\u7b56\u7565&#xff08;\u901a\u5e38\u662f\u5c1d\u8bd5\u9501\u5b9a\u3001\u5931\u8d25\u5c31\u56de\u9000\u6362\u987a\u5e8f&#xff0c;\u6216\u8005\u6309\u5730\u5740\u6392\u5e8f&#xff09;\u6765\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u7ba1\u4ee5\u4ec0\u4e48\u987a\u5e8f\u4f20\u5165\u8fd9\u4e24\u4e2a\u9501&#xff0c;\u90fd\u4e0d\u4f1a\u51fa\u73b0\u5faa\u73af\u7b49\u5f85\u3002<\/p>\n<p>\u5b83\u548c lock_guard \u7684\u884c\u4e3a\u5f88\u50cf&#xff1a;\u6784\u9020\u5373\u9501\u5b9a\u5168\u90e8&#xff0c;\u6790\u6784\u5373\u89e3\u9501\u5168\u90e8&#xff0c;\u4e2d\u9014\u4e0d\u80fd\u64cd\u4f5c\u3002\u533a\u522b\u53ea\u662f\u5b83\u80fd\u540c\u65f6\u5305\u591a\u4e2a\u4e92\u65a5\u91cf&#xff0c;\u5e76\u4e14\u5185\u90e8\u5904\u7406\u4e86\u591a\u9501\u7684\u6b7b\u9501\u95ee\u9898\u3002C&#043;&#043;17 \u4e4b\u524d&#xff0c;\u4f60\u9700\u8981\u624b\u52a8\u7528 std::lock \u52a0\u9501\u518d\u914d\u5408 lock_guard \u7684 adopt_lock \u53c2\u6570\u6765\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c&#xff0c;scoped_lock \u628a\u8fd9\u4e2a\u6d41\u7a0b\u6253\u5305\u6210\u4e86\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u63a5\u53e3\u3002<\/p>\n<h3>\u5dee\u5f02\u5bf9\u7167<\/h3>\n<table>\n<tr>\u7ef4\u5ea6lock_guardunique_lockshared_lockscoped_lock<\/tr>\n<tbody>\n<tr>\n<td>\u4e00\u6b21\u7ba1\u7406\u51e0\u4e2a\u9501<\/td>\n<td>1 \u4e2a<\/td>\n<td>1 \u4e2a<\/td>\n<td>1 \u4e2a<\/td>\n<td>\u4efb\u610f\u591a\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\u4e2d\u9014\u80fd\u5426\u624b\u52a8\u89e3\u9501<\/td>\n<td>\u4e0d\u80fd<\/td>\n<td>\u80fd<\/td>\n<td>\u80fd<\/td>\n<td>\u4e0d\u80fd<\/td>\n<\/tr>\n<tr>\n<td>\u80fd\u5426\u8f6c\u79fb\u9501\u6240\u6709\u6743<\/td>\n<td>\u4e0d\u80fd<\/td>\n<td>\u80fd&#xff08;\u79fb\u52a8&#xff09;<\/td>\n<td>\u80fd&#xff08;\u79fb\u52a8&#xff09;<\/td>\n<td>\u4e0d\u80fd<\/td>\n<\/tr>\n<tr>\n<td>\u9501\u7684\u7c7b\u578b<\/td>\n<td>\u72ec\u5360\u5199\u9501<\/td>\n<td>\u72ec\u5360\u5199\u9501<\/td>\n<td>\u5171\u4eab\u8bfb\u9501<\/td>\n<td>\u591a\u628a\u72ec\u5360\u5199\u9501<\/td>\n<\/tr>\n<tr>\n<td>\u5178\u578b\u642d\u6863<\/td>\n<td>\u7b80\u5355\u4e34\u754c\u533a<\/td>\n<td>\u6761\u4ef6\u53d8\u91cf<\/td>\n<td>shared_mutex \u8bfb\u4fa7<\/td>\n<td>\u591a\u5bf9\u8c61\u540c\u65f6\u52a0\u9501<\/td>\n<\/tr>\n<tr>\n<td>\u6807\u51c6\u7248\u672c<\/td>\n<td>C&#043;&#043;11<\/td>\n<td>C&#043;&#043;11<\/td>\n<td>C&#043;&#043;14<\/td>\n<td>C&#043;&#043;17<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>            <span id=\"quality-guide-article-end\" aria-hidden=\"true\" style=\"display:block;height:1px;margin-bottom:-1px\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock<br \/>\nflyfish<br \/>\n\u4e00\u3001lock_guard&#xff1a;\u6700\u7b80\u5355\u7684\u4f5c\u7528\u57df\u4fdd\u62a4<br \/>\n\u6f14\u793a\u591a\u7ebf\u7a0b\u5e76\u53d1\u7d2f\u52a0\u4e00\u4e2a\u5168\u5c40\u8ba1\u6570\u5668&#xff0c;\u7528 lock_guard \u4fdd\u62a4\u4e34\u754c\u533a\u3002<br \/>\n#include<br \/>\n#include<br \/>\n#include long counter  0;<br \/>\nstd:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-124732","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/124732.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"C \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock flyfish \u4e00\u3001lock_guard&#xff1a;\u6700\u7b80\u5355\u7684\u4f5c\u7528\u57df\u4fdd\u62a4 \u6f14\u793a\u591a\u7ebf\u7a0b\u5e76\u53d1\u7d2f\u52a0\u4e00\u4e2a\u5168\u5c40\u8ba1\u6570\u5668&#xff0c;\u7528 lock_guard \u4fdd\u62a4\u4e34\u754c\u533a\u3002 #include #include #include long counter 0; std:\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/124732.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-19T04:16:33+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124732.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/124732.html\",\"name\":\"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-19T04:16:33+00:00\",\"dateModified\":\"2026-11-19T04:16:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/124732.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/124732.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124732.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/124732.html","og_locale":"zh_CN","og_type":"article","og_title":"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"C \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock flyfish \u4e00\u3001lock_guard&#xff1a;\u6700\u7b80\u5355\u7684\u4f5c\u7528\u57df\u4fdd\u62a4 \u6f14\u793a\u591a\u7ebf\u7a0b\u5e76\u53d1\u7d2f\u52a0\u4e00\u4e2a\u5168\u5c40\u8ba1\u6570\u5668&#xff0c;\u7528 lock_guard \u4fdd\u62a4\u4e34\u754c\u533a\u3002 #include #include #include long counter 0; std:","og_url":"https:\/\/www.wsisp.com\/helps\/124732.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-19T04:16:33+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/124732.html","url":"https:\/\/www.wsisp.com\/helps\/124732.html","name":"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-19T04:16:33+00:00","dateModified":"2026-11-19T04:16:33+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/124732.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/124732.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/124732.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++ \u7ba1\u7406\u4e92\u65a5\u91cf\u9501\u6240\u6709\u6743 std::lock_guard\u3001std::unique_lock\u3001std::shared_lock\u3001std::scoped_lock"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=124732"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124732\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=124732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=124732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=124732"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=124732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}