{"id":57876,"date":"2024-10-10T21:52:07","date_gmt":"2024-10-10T13:52:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57876\/"},"modified":"2024-10-10T21:52:07","modified_gmt":"2024-10-10T13:52:07","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e4%ba%92%e6%96%a5%e9%8e%96%ef%bc%88sync-mutex%ef%bc%89%e5%92%8c%e8%ae%80%e5%af%ab%e4%ba%92%e6%96%a5%e9%8e%96%ef%bc%88sync-rwmutex%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57876\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u4e92\u65a5\u9396\uff08sync.Mutex\uff09\u548c\u8b80\u5beb\u4e92\u65a5\u9396\uff08sync.RWMutex\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e4%ba%92%e6%96%a5%e9%8e%96%ef%bc%88sync-mutex%ef%bc%89%e5%92%8c%e8%ae%80%e5%af%ab%e4%ba%92%e6%96%a5%e9%8e%96%ef%bc%88sync-rwmutex%ef%bc%89-yrOqRXdKwT\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u4e92\u65a5\u9396\uff08sync.Mutex\uff09\u548c\u8b80\u5beb\u4e92\u65a5\u9396\uff08sync.RWMutex\uff09<\/h1>\n<p>\u5728\u591a\u7dda\u7a0b\u7de8\u7a0b\u4e2d\uff0c\u8cc7\u6e90\u7684\u5171\u4eab\u548c\u540c\u6b65\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u591a\u7a2e\u5de5\u5177\u4f86\u8655\u7406\u9019\u4e9b\u554f\u984c\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5c31\u662f\u4e92\u65a5\u9396\uff08Mutex\uff09\u548c\u8b80\u5beb\u4e92\u65a5\u9396\uff08RWMutex\uff09\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u9019\u5169\u7a2e\u9396\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u9069\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e4%ba%92%e6%96%a5%e9%8e%96%ef%bc%88sync-mutex%ef%bc%89-yrOqRXdKwT\">\u4e92\u65a5\u9396\uff08sync.Mutex\uff09<\/h2>\n<p>\u4e92\u65a5\u9396\u662f\u4e00\u7a2e\u57fa\u672c\u7684\u540c\u6b65\u539f\u8a9e\uff0c\u7528\u65bc\u4fdd\u8b77\u5171\u4eab\u8cc7\u6e90\uff0c\u78ba\u4fdd\u5728\u540c\u4e00\u6642\u9593\u53ea\u6709\u4e00\u500b\u7dda\u7a0b\u53ef\u4ee5\u8a2a\u554f\u8a72\u8cc7\u6e90\u3002Go \u8a9e\u8a00\u4e2d\u7684 <code>sync.Mutex<\/code> \u63d0\u4f9b\u4e86\u5169\u500b\u4e3b\u8981\u7684\u65b9\u6cd5\uff1a<code>Lock()<\/code> \u548c <code>Unlock()<\/code>\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-yrOqRXdKwT\">\u4f7f\u7528\u7bc4\u4f8b<\/h3>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nvar (\n    counter int\n    mu      sync.Mutex\n)\n\nfunc increment(wg *sync.WaitGroup) {\n    defer wg.Done()\n    mu.Lock()\n    counter++\n    mu.Unlock()\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n    for i := 0; i &lt; 1000; i++ {\n        wg.Add(1)\n        go increment(&amp;wg)\n    }\n    wg.Wait()\n    fmt.Println(&quot;Final counter:&quot;, counter)\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u5168\u5c40\u8b8a\u91cf <code>counter<\/code>\uff0c\u4e26\u4f7f\u7528\u4e92\u65a5\u9396\u4f86\u4fdd\u8b77\u5c0d\u8a72\u8b8a\u91cf\u7684\u8a2a\u554f\u3002\u6bcf\u7576\u4e00\u500b goroutine \u60f3\u8981\u589e\u52a0\u8a08\u6578\u5668\u6642\uff0c\u5b83\u5fc5\u9808\u5148\u7372\u53d6\u9396\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6578\u64da\u7af6\u722d\u7684\u554f\u984c\u3002<\/p>\n<h2 id=\"%e8%ae%80%e5%af%ab%e4%ba%92%e6%96%a5%e9%8e%96%ef%bc%88sync-rwmutex%ef%bc%89-yrOqRXdKwT\">\u8b80\u5beb\u4e92\u65a5\u9396\uff08sync.RWMutex\uff09<\/h2>\n<p>\u8b80\u5beb\u4e92\u65a5\u9396\u662f\u4e00\u7a2e\u66f4\u70ba\u9748\u6d3b\u7684\u9396\uff0c\u5141\u8a31\u591a\u500b\u8b80\u53d6\u64cd\u4f5c\u540c\u6642\u9032\u884c\uff0c\u4f46\u5728\u5beb\u5165\u64cd\u4f5c\u6642\u6703\u963b\u6b62\u6240\u6709\u8b80\u53d6\u548c\u5176\u4ed6\u5beb\u5165\u64cd\u4f5c\u3002\u9019\u4f7f\u5f97 <code>sync.RWMutex<\/code> \u5728\u8b80\u53d6\u64cd\u4f5c\u9060\u591a\u65bc\u5beb\u5165\u64cd\u4f5c\u7684\u5834\u666f\u4e2d\u975e\u5e38\u6709\u6548\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-yrOqRXdKwT\">\u4f7f\u7528\u7bc4\u4f8b<\/h3>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nvar (\n    data  int\n    rwMu  sync.RWMutex\n)\n\nfunc read(wg *sync.WaitGroup) {\n    defer wg.Done()\n    rwMu.RLock()\n    fmt.Println(\"Reading data:\", data)\n    rwMu.RUnlock()\n}\n\nfunc write(wg *sync.WaitGroup, value int) {\n    defer wg.Done()\n    rwMu.Lock()\n    data = value\n    rwMu.Unlock()\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n    for i := 0; i &lt; 5; i++ {\n        wg.Add(1)\n        go read(&amp;wg)\n    }\n    for i := 0; i &lt; 5; i++ {\n        wg.Add(1)\n        go write(&amp;wg, i)\n    }\n    wg.Wait()\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>RWMutex<\/code> \u4f86\u4fdd\u8b77\u8b8a\u91cf <code>data<\/code>\u3002\u591a\u500b goroutine \u53ef\u4ee5\u540c\u6642\u8b80\u53d6\u6578\u64da\uff0c\u4f46\u5728\u5beb\u5165\u6642\uff0c\u6240\u6709\u7684\u8b80\u53d6\u548c\u5beb\u5165\u90fd\u6703\u88ab\u963b\u585e\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<h2 id=\"%e9%81%b8%e6%93%87%e5%90%88%e9%81%a9%e7%9a%84%e9%8e%96-yrOqRXdKwT\">\u9078\u64c7\u5408\u9069\u7684\u9396<\/h2>\n<p>\u5728\u9078\u64c7\u4f7f\u7528 <code>sync.Mutex<\/code> \u9084\u662f <code>sync.RWMutex<\/code> \u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u8003\u616e\u5230\u61c9\u7528\u7684\u7279\u6027\u3002\u5982\u679c\u61c9\u7528\u4e2d\u8b80\u53d6\u64cd\u4f5c\u9060\u591a\u65bc\u5beb\u5165\u64cd\u4f5c\uff0c\u5247\u4f7f\u7528\u8b80\u5beb\u4e92\u65a5\u9396\u6703\u66f4\u6709\u6548\u7387\uff1b\u53cd\u4e4b\uff0c\u5247\u53ef\u4ee5\u9078\u64c7\u666e\u901a\u7684\u4e92\u65a5\u9396\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-yrOqRXdKwT\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u4e92\u65a5\u9396\u548c\u8b80\u5beb\u4e92\u65a5\u9396\u662f\u8655\u7406\u4e26\u767c\u7de8\u7a0b\u4e2d\u8cc7\u6e90\u5171\u4eab\u7684\u91cd\u8981\u5de5\u5177\u3002\u6b63\u78ba\u4f7f\u7528\u9019\u4e9b\u9396\u53ef\u4ee5\u6709\u6548\u907f\u514d\u6578\u64da\u7af6\u722d\u548c\u4e0d\u4e00\u81f4\u6027\u554f\u984c\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u8655\u7406\u4e26\u767c\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u9396\u985e\u578b\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u5176\u4ed6\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Go \u8a9e\u8a00\u4e2d\u7684\u4e92\u65a5\u9396\uff08sync.Mutex\uff09\u548c\u8b80\u5beb\u4e92\u65a5\u9396\uff08sync.RWMutex\uff09\uff0c\u638c\u63e1\u4e26\u767c\u7de8\u7a0b\u4e2d\u7684\u6578\u64da\u5b89\u5168\u8207\u540c\u6b65\u6280\u5de7\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57876","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57876"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57876\/revisions"}],"predecessor-version":[{"id":57877,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57876\/revisions\/57877"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}