{"id":441665,"date":"2025-05-21T14:00:59","date_gmt":"2025-05-21T06:00:59","guid":{"rendered":"https:\/\/www.idc.net\/help\/?p=441665"},"modified":"2025-05-21T14:00:59","modified_gmt":"2025-05-21T06:00:59","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-go-%e8%af%ad%e8%a8%80-sync-mutex%ef%bc%9a%e9%ab%98%e5%b9%b6%e5%8f%91%e4%b8%8b%e7%9a%84%e9%94%81%e6%9c%ba%e5%88%b6%e4%bc%98%e5%8c%96%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/441665\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3 Go \u8bed\u8a00 sync.Mutex\uff1a\u9ad8\u5e76\u53d1\u4e0b\u7684\u9501\u673a\u5236\u4f18\u5316\u89e3\u6790"},"content":{"rendered":"<p>&nbsp;<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u5728\u9ad8\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u9501\u7684\u6027\u80fd\u76f4\u63a5\u5f71\u54cd\u7cfb\u7edf\u7684\u6574\u4f53\u541e\u5410\u548c\u54cd\u5e94\u901f\u5ea6\u3002Go \u8bed\u8a00\u4e2d\u7684 <code>sync.Mutex<\/code> \u662f\u4e00\u79cd\u5e38\u7528\u7684\u4e92\u65a5\u9501\u673a\u5236\uff0c\u5176\u5e95\u5c42\u5b9e\u73b0\u4e0d\u65ad\u4f18\u5316\uff0c\u5728\u5904\u7406\u4f4e\u7ade\u4e89\u4e0e\u9ad8\u7ade\u4e89\u73af\u5883\u4e2d\u90fd\u80fd\u4fdd\u6301\u9ad8\u6548\u3002\u672c\u6587\u5c06\u7ed3\u5408\u6e90\u7801\u5256\u6790 <code>sync.Mutex<\/code> \u7684\u8bbe\u8ba1\u539f\u7406\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u638c\u63e1\u5176\u5de5\u4f5c\u673a\u5236\uff0c\u540c\u65f6\u5206\u4eab\u4e3a\u4f55\u9009\u62e9<strong>\u9999\u6e2f\u4e91\u670d\u52a1\u5668<\/strong>\u5bf9\u90e8\u7f72\u9ad8\u5e76\u53d1\u670d\u52a1\u5177\u6709\u4f18\u52bf\u3002<\/p>\n<h2>\u4e00\u3001sync.Mutex \u7b80\u4ecb\u4e0e\u6838\u5fc3\u7ed3\u6784<\/h2>\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c<code>sync.Mutex<\/code> \u662f\u6838\u5fc3\u7684\u5e76\u53d1\u63a7\u5236\u5de5\u5177\uff0c\u5176\u7ed3\u6784\u8bbe\u8ba1\u975e\u5e38\u7d27\u51d1\uff0c\u4ec5\u5305\u542b\u4e24\u4e2a\u5b57\u6bb5\uff1a<\/p>\n<pre><code class=\"language-go\">type Mutex struct {\r\n  state int32\r\n  sema  uint32\r\n}\r\n<\/code><\/pre>\n<ul>\n<li><strong>state<\/strong>\uff1a\u4f7f\u7528\u591a\u4e2a\u4f4d\uff08bit\uff09\u6807\u8bc6\u9501\u7684\u72b6\u6001\uff0c\u4f8b\u5982\u662f\u5426\u5df2\u9501\u5b9a\u3001\u662f\u5426\u5904\u4e8e\u9965\u997f\u6a21\u5f0f\u4ee5\u53ca\u7b49\u5f85\u534f\u7a0b\u6570\u91cf\u3002<\/li>\n<li><strong>sema<\/strong>\uff1a\u4e3a\u4fe1\u53f7\u91cf\uff0c\u914d\u5408 Go runtime \u5b9e\u73b0\u534f\u7a0b\u7684\u963b\u585e\u4e0e\u5524\u9192\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u867d\u7b80\u5355\uff0c\u5374\u6784\u6210\u4e86\u9ad8\u6548\u9501\u673a\u5236\u7684\u57fa\u7840\u3002<\/p>\n<h2>\u4e8c\u3001state \u5b57\u6bb5\u7684\u591a\u6001\u72b6\u6001\u7ba1\u7406<\/h2>\n<p><code>state<\/code> \u5b57\u6bb5\u5e76\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5e03\u5c14\u503c\uff0c\u800c\u662f\u4e00\u4e2a\u901a\u8fc7\u4f4d\u8fd0\u7b97\u590d\u7528\u7684\u6574\u578b\u53d8\u91cf\uff0c\u5176\u72b6\u6001\u7ec4\u5408\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go\">const (\r\n  mutexLocked = 1 &lt;&lt; iota  \/\/ \u5df2\u52a0\u9501\r\n  mutexWoken               \/\/ \u6709\u534f\u7a0b\u88ab\u5524\u9192\r\n  mutexStarving            \/\/ \u8fdb\u5165\u9965\u997f\u6a21\u5f0f\r\n  mutexWaiterShift         \/\/ \u7b49\u5f85\u534f\u7a0b\u6570\u91cf\u504f\u79fb\u4f4d\r\n  starvationThresholdNs = 1e6 \/\/ \u9965\u997f\u9608\u503c\uff081ms\uff09\r\n)\r\n<\/code><\/pre>\n<ul>\n<li><strong>mutexLocked<\/strong>\uff1a\u9501\u662f\u5426\u5df2\u88ab\u5360\u7528\uff1b<\/li>\n<li><strong>mutexWoken<\/strong>\uff1a\u9632\u6b62\u591a\u4e2a\u534f\u7a0b\u88ab\u540c\u65f6\u5524\u9192\u5bfc\u81f4\u4e89\u9501\uff1b<\/li>\n<li><strong>mutexStarving<\/strong>\uff1a\u957f\u65f6\u95f4\u672a\u83b7\u5f97\u9501\u65f6\u89e6\u53d1\u9965\u997f\u6a21\u5f0f\uff1b<\/li>\n<li><strong>mutexWaiterShift<\/strong>\uff1a\u901a\u8fc7\u53f3\u79fb\u8bb0\u5f55\u7b49\u5f85\u534f\u7a0b\u6570\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u57fa\u4e8e\u4f4d\u64cd\u4f5c\u7684\u8bbe\u8ba1\u5728\u6027\u80fd\u4e0a\u5177\u5907\u663e\u8457\u4f18\u52bf\uff0c\u7279\u522b\u9002\u7528\u4e8e\u90e8\u7f72\u5728\u9ad8\u6027\u80fd\u73af\u5883\u4e2d\u7684\u540e\u7aef\u670d\u52a1\u3002<\/p>\n<h2>\u4e09\u3001\u81ea\u65cb\u9501\u4e0e CAS \u64cd\u4f5c\uff1a\u9501\u83b7\u53d6\u903b\u8f91\u7684\u5173\u952e<\/h2>\n<p>\u5728 <code>lockSlow()<\/code> \u65b9\u6cd5\u4e2d\uff0cGo \u4f7f\u7528\u4e86\u4e24\u79cd\u4f18\u5316\u673a\u5236\u6765\u63d0\u5347\u9501\u7ade\u4e89\u4e0b\u7684\u6548\u7387\uff1a<\/p>\n<ul>\n<li><strong>\u81ea\u65cb\u9501\uff08Spin Lock\uff09<\/strong>\uff1a\u77ed\u65f6\u95f4\u5185\u53ef\u80fd\u91ca\u653e\u7684\u9501\u4e0d\u4f1a\u7acb\u523b\u963b\u585e\u534f\u7a0b\uff0c\u800c\u662f\u539f\u5730\u81ea\u65cb\u7b49\u5f85\uff0c\u964d\u4f4e\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3002<\/li>\n<li><strong>CAS \u64cd\u4f5c\uff08Compare And Swap\uff09<\/strong>\uff1a\u5229\u7528\u539f\u5b50\u64cd\u4f5c\u4fee\u6539 <code>state<\/code> \u72b6\u6001\uff0c\u786e\u4fdd\u5e76\u53d1\u5b89\u5168\u3002<\/li>\n<\/ul>\n<p>\u9700\u8981\u6ce8\u610f\uff0c\u81ea\u65cb\u4f1a\u6d88\u8017 CPU \u8d44\u6e90\uff0c\u4e0d\u9002\u5408\u9501\u957f\u65f6\u95f4\u6301\u6709\u7684\u573a\u666f\uff0c\u56e0\u6b64\u670d\u52a1\u5668\u8d44\u6e90\u5206\u914d\u5e94\u5408\u7406\uff0c\u4f8b\u5982\u4f7f\u7528\u5177\u5907\u72ec\u7acb\u8ba1\u7b97\u8d44\u6e90\u7684<strong>\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668<\/strong>\u66f4\u5177\u4f18\u52bf\u3002<\/p>\n<h2>\u56db\u3001\u4fe1\u53f7\u91cf sema \u4e0e\u534f\u7a0b\u8c03\u5ea6\u673a\u5236<\/h2>\n<p>\u5f53\u9501\u7ade\u4e89\u6fc0\u70c8\u65f6\uff0c\u81ea\u65cb\u7b56\u7565\u65e0\u6cd5\u89e3\u51b3\u95ee\u9898\uff0c\u8fd9\u65f6 <code>sema<\/code> \u5b57\u6bb5\u5c31\u627f\u62c5\u8d77\u63a7\u5236\u534f\u7a0b\u963b\u585e\u4e0e\u5524\u9192\u7684\u804c\u8d23\uff1a<\/p>\n<ul>\n<li><strong>runtime_SemacquireMutex()<\/strong>\uff1a\u5c06\u5f53\u524d\u534f\u7a0b\u963b\u585e\uff1b<\/li>\n<li><strong>runtime_Semrelease()<\/strong>\uff1a\u91ca\u653e\u9501\u5e76\u5524\u9192\u4e00\u4e2a\u7b49\u5f85\u534f\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u4e0e\u81ea\u65cb\u9501\u4e0d\u540c\uff0c\u4fe1\u53f7\u91cf\u673a\u5236\u4e0d\u5360\u7528 CPU\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u534f\u7a0b\u7684\u6302\u8d77\u4e0e\u6062\u590d\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u5927\u89c4\u6a21\u5e76\u53d1\u7684\u5fae\u670d\u52a1\u67b6\u6784\u90e8\u7f72\u5728<strong>\u9999\u6e2fvps<\/strong>\u4e0a\uff0c\u65e2\u7a33\u5b9a\u53c8\u8282\u80fd\u3002<\/p>\n<h2>\u4e94\u3001\u666e\u901a\u6a21\u5f0f\u4e0e\u9965\u997f\u6a21\u5f0f\u7684\u5207\u6362\u903b\u8f91<\/h2>\n<p><code>sync.Mutex<\/code> \u5728\u4e0d\u540c\u5e76\u53d1\u7a0b\u5ea6\u4e0b\u91c7\u7528\u4e0d\u540c\u7684\u9501\u7b56\u7565\uff1a<\/p>\n<ul>\n<li><strong>\u666e\u901a\u6a21\u5f0f<\/strong>\uff1a\u534f\u7a0b\u6309 FIFO \u6392\u961f\uff0c\u65b0\u6765\u7684\u534f\u7a0b\u53ef\u4e0e\u5524\u9192\u534f\u7a0b\u7ade\u4e89\u9501\u3002<\/li>\n<li><strong>\u9965\u997f\u6a21\u5f0f<\/strong>\uff1a\u7b49\u5f85\u65f6\u95f4\u8d85\u8fc7 1ms\uff0c\u9501\u5c06\u76f4\u63a5\u4ea4\u7531\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u4e0b\u4e00\u4e2a\u534f\u7a0b\uff0c\u907f\u514d\u957f\u65f6\u95f4\u5f97\u4e0d\u5230\u9501\u7684\u534f\u7a0b\u201c\u997f\u6b7b\u201d\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u72b6\u6001\u68c0\u6d4b\u4e0e\u5207\u6362\u673a\u5236\uff0cGo \u5b9e\u73b0\u4e86\u517c\u987e\u6027\u80fd\u4e0e\u516c\u5e73\u6027\u7684\u9501\u7b56\u7565\u3002\u8fd9\u79cd\u673a\u5236\u5728\u5e76\u53d1\u7528\u6237\u8bbf\u95ee\u91cf\u5927\u7684\u573a\u666f\u4e0b\u8868\u73b0\u5c24\u4e3a\u51fa\u8272\uff0c\u975e\u5e38\u9002\u5408\u90e8\u7f72\u5728<strong>\u9999\u6e2f\u4e91\u670d\u52a1\u5668<\/strong>\u4e0a\u8fdb\u884c\u6027\u80fd\u8c03\u4f18\u4e0e\u53ef\u6269\u5c55\u6027\u6d4b\u8bd5\u3002<\/p>\n<h2>\u516d\u3001\u603b\u7ed3\u4e0e\u90e8\u7f72\u5efa\u8bae<\/h2>\n<p>\u901a\u8fc7\u6e90\u7801\u5206\u6790\u53ef\u4ee5\u770b\u51fa\uff0c<code>sync.Mutex<\/code> \u7684\u6838\u5fc3\u8bbe\u8ba1\u7406\u5ff5\u662f\u4ee5\u6700\u5c0f\u4ee3\u4ef7\u5b9e\u73b0\u6700\u5927\u5e76\u53d1\u63a7\u5236\u6548\u679c\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7\u4f4d\u8fd0\u7b97\u4e0e CAS \u64cd\u4f5c\u8fdb\u884c\u5feb\u901f\u72b6\u6001\u5207\u6362\uff1b<\/li>\n<li>\u5229\u7528\u81ea\u65cb\u4e0e\u4fe1\u53f7\u91cf\u63d0\u5347\u4e0d\u540c\u8d1f\u8f7d\u4e0b\u7684\u54cd\u5e94\u6027\u80fd\uff1b<\/li>\n<li>\u81ea\u52a8\u5207\u6362\u9501\u6a21\u5f0f\u4fdd\u969c\u9ad8\u516c\u5e73\u6027\u548c\u541e\u5410\u91cf\u3002<\/li>\n<\/ul>\n<p>\u5728\u5b9e\u9645\u90e8\u7f72\u4e2d\uff0c\u5efa\u8bae\u5c06\u9ad8\u5e76\u53d1\u7684 Go \u670d\u52a1\u4f18\u5148\u9009\u62e9\u9ad8\u5e26\u5bbd\u3001\u4f4e\u5ef6\u8fdf\u7684<a href=\"https:\/\/idc.net\/\"><strong>\u9999\u6e2fvps<\/strong><\/a>\u8fdb\u884c\u8fd0\u884c\uff0c\u65e2\u80fd\u63d0\u5347\u9501\u7ade\u4e89\u54cd\u5e94\u901f\u5ea6\uff0c\u4e5f\u4fbf\u4e8e\u9760\u8fd1\u4e9a\u6d32\u5e02\u573a\u4f18\u5316\u7f51\u7edc\u8bbf\u95ee\u3002\u7acb\u5373\u8bbf\u95ee\u6211\u4eec<a href=\"https:\/\/idc.net\/\"><strong>\u5b98\u7f51<\/strong><\/a>\u83b7\u53d6\u9ad8\u6027\u80fd\u7684\u9999\u6e2f\u4e91\u8ba1\u7b97\u4ea7\u54c1\uff0c\u5305\u62ec<strong>\u9999\u6e2fvps<\/strong>\u3001<strong>\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668<\/strong>\uff0c\u4e3a\u60a8\u7684 Go \u9879\u76ee\u4fdd\u9a7e\u62a4\u822a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u524d\u8a00 \u5728\u9ad8\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u9501\u7684\u6027\u80fd\u76f4\u63a5\u5f71\u54cd\u7cfb\u7edf\u7684\u6574\u4f53\u541e\u5410\u548c\u54cd\u5e94\u901f\u5ea6\u3002Go \u8bed\u8a00\u4e2d\u7684 sync.Mut [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182396],"tags":[],"class_list":["post-441665","post","type-post","status-publish","format-standard","hentry","category-go"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=441665"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441665\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=441665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=441665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=441665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}