{"id":1600,"date":"2020-09-30T05:26:16","date_gmt":"2020-09-29T21:26:16","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1600"},"modified":"2022-02-16T21:55:23","modified_gmt":"2022-02-16T13:55:23","slug":"go-mutex-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/09\/30\/go-mutex-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u57f7\u884c\u5e8f\u7684\u9396"},"content":{"rendered":"<h1>\u591a\u57f7\u884c\u5e8f\u554f\u984c\u7684\u89e3\u6c7a<\/h1>\n<p>\u5728\u4e0a\u4e00\u7bc7\u4ecb\u7d39\u4e86\u591a\u57f7\u884c\u5e8f\u7684\u597d\u8655\u4ee5\u53ca\u64b0\u5beb\u65b9\u5f0f\uff0c\u4f46\u662f\u4e5f\u63d0\u5230\u4e86\u591a\u57f7\u884c\u5e8f\u7684\u554f\u984c\uff0c\u6240\u4ee5\u9019\u7bc7\u91dd\u5c0d\u591a\u57f7\u884c\u5e8f\u9047\u5230\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Race_condition\" title=\"\u7af6\u722d\u5371\u5bb3\" rel=\"nofollow noopener\" target=\"_blank\">race condition<\/a>\u5728 go \u8a9e\u8a00\u5982\u4f55\u89e3\u6c7a\u5b83\u4f86\u8aaa\u660e\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u591a\u57f7\u884c\u5e8f\u7684\u9396<\/h2>\n<p>\u5728\u7a0b\u5f0f\u57f7\u884c\u7684\u904e\u7a0b\u4e2d\u53ef\u80fd\u6703\u505a\u4e00\u500b\u6539\u8b8a\u7269\u4ef6\u5c6c\u6027\u7684\u52d5\u4f5c\uff0c\u7136\u800c\u5728\u591a\u57f7\u884c\u5e8f\u4e0b\u6703\u56e0\u70ba\u9019\u500b\u52d5\u4f5c\u7522\u751f\u4e00\u500b\u554f\u984c\uff0c\u5c31\u662f\u5f8c\u9762\u57f7\u884c\u7684\u6703\u628a\u524d\u9762\u57f7\u884c\u7684\u7d50\u679c\u7d66\u84cb\u6389\u3002<br \/>\n\u6240\u4ee5\u901a\u5e38\u7684\u89e3\u6c7a\u65b9\u5f0f\u5c31\u662f\u52a0\u4e00\u500b\u9396 <code>mutex<\/code> \u8b93\u8981\u66f4\u6539\u9019\u7269\u4ef6\u7684\u57f7\u884c\u5e8f\u6392\u968a\u4f86\u66f4\u6539\uff0c\u63a5\u4e0b\u4f86\u5c31\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u4f55\u64b0\u5beb\u9019\u500b\u9396\u3002<\/p>\n<h3>\u9396\u5982\u4f55\u64b0\u5beb<\/h3>\n<p>\u9019\u6642\u9084\u662f\u4e00\u6a23\u8acb\u5927\u5bb6\u5148\u770b\u770b<a href=\"https:\/\/tour.golang.org\/concurrency\/9\" title=\"Go Tour\" rel=\"nofollow noopener\" target=\"_blank\">Go Tour<\/a>\u7684\u7bc4\u4f8b<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;sync&quot;\n    &quot;time&quot;\n)\n\n\/\/ SafeCounter is safe to use concurrently.\ntype SafeCounter struct {\n    v   map[string]int\n    mux sync.Mutex\n}\n\n\/\/ Inc increments the counter for the given key.\nfunc (c *SafeCounter) Inc(key string) {\n    c.mux.Lock()\n    \/\/ Lock so only one goroutine at a time can access the map c.v.\n    c.v[key]++\n    c.mux.Unlock()\n}\n\n\/\/ Value returns the current value of the counter for the given key.\nfunc (c *SafeCounter) Value(key string) int {\n    c.mux.Lock()\n    \/\/ Lock so only one goroutine at a time can access the map c.v.\n    defer c.mux.Unlock()\n    return c.v[key]\n}\n\nfunc main() {\n    c := SafeCounter{v: make(map[string]int)}\n    for i := 0; i &lt; 1000; i++ {\n        go c.Inc(&quot;somekey&quot;)\n    }\n\n    time.Sleep(time.Second)\n    fmt.Println(c.Value(&quot;somekey&quot;))\n}<\/code><\/pre>\n<p>\u76f8\u4fe1\u6709\u95b1\u8b80\u7b46\u8005\u524d\u5e7e\u7bc7\u6587\u7ae0\u53ef\u4ee5\u7406\u89e3\u4e0a\u9762\u7684\u7a0b\u5f0f\u5728\u505a\u751a\u9ebc\uff0c\u9019\u908a\u91dd\u5c0d <code>make<\/code> \u95dc\u9375\u5b57\u52a0\u4ee5\u8aaa\u660e\u3002<br \/>\n\u9019\u500b <code>make<\/code> \u4e3b\u8981\u7684\u7528\u9014\u5c31\u662f\u7522\u751f\u4e00\u500b\u8a18\u61b6\u9ad4\u7a7a\u9593\u7d66\u8b8a\u6578\uff0c\u6240\u4ee5\u7c21\u55ae\u8b1b\u4e0a\u9762\u7684\u4f8b\u5b50\u5c31\u662f\u7522\u751f\u4e00\u500b<code>key<\/code>\u662f\u5b57\u4e32<code>value<\/code>\u662f\u6574\u6578\u7684<code>map<\/code>\u7d66\u8b8a\u6578 c \u7684\u5c6c\u6027  v \u3002<br \/>\n\u63a5\u4e0b\u4f86\u5728\u4e0b\u4e00\u500b\u6bb5\u843d\u8aaa\u660e\u4e00\u4e0b <code>mutex<\/code> \u7684\u904b\u4f5c\u65b9\u5f0f\u3002<\/p>\n<h3>\u9396\u7684\u904b\u4f5c\u65b9\u5f0f<\/h3>\n<p>\u642d\u914d\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u4e3b\u8981\u662f\u5728 Inc \u8ddf Value \u9019\u5169\u500b\u65b9\u6cd5\uff0c\u56e0\u70ba\u8981\u907f\u514d<a href=\"https:\/\/en.wikipedia.org\/wiki\/Race_condition\" title=\"\u7af6\u722d\u5371\u5bb3\" rel=\"nofollow noopener\" target=\"_blank\">race condition<\/a>\u7684\u554f\u984c\u6240\u4ee5\u4e0d\u7ba1\u5728\u5b58\u53d6\u524d\u90fd\u8981\u52a0\u4e0a\u4e00\u500b\u9396\uff0c\u9650\u5236\u53ea\u80fd\u6709\u4e00\u500b\u57f7\u884c\u5e8f\u4f86\u5b58\u53d6\u8b8a\u6578\uff0c\u800c\u5176\u4ed6\u672a\u53d6\u5f97\u9396\u7684\u76f4\u884c\u5e8f\u6703\u7b49\u5f85\u9396\u89e3\u958b\u4e4b\u5f8c\u53d6\u5f97\u9396\u5728\u57f7\u884c\u3002<br \/>\n\u6240\u4ee5\u5728\u65b0\u589e\u4e4b\u524d\u8981\u5148\u547c\u53eb <code>c.Mutex.Lock()<\/code> \u4f86\u53d6\u5f97\u9396\uff0c\u4e26\u9396\u4f4f\u57f7\u884c\u5e8f\u4f86\u66f4\u65b0\u8a72\u8b8a\u6578\uff0c\u7b49\u5230\u66f4\u65b0\u5b8c\u4e86\u4e4b\u5f8c\u4e00\u5b9a\u8981\u8a18\u5f97\u547c\u53eb <code>c.mux.Unlock()<\/code> \u4f86\u89e3\u9396\uff0c\u4e0d\u7136\u5c31\u6c38\u9060\u6c92\u6709\u53ef\u4ee5\u57f7\u884c\u5b58\u53d6\u8b8a\u6578\u7684\u6b0a\u9650\u4e86\uff0c\u800c\u5176\u4ed6\u57f7\u884c\u5e8f\u4e5f\u4e0d\u80fd\u7e7c\u7e8c\u57f7\u884c\u4e0b\u53bb\u3002<br \/>\n\u56e0\u6b64\u5728 Value \u9019\u65b9\u6cd5\u4e5f\u662f\u9700\u8981\u53d6\u7684\u9396\u4f86\u53d6\u7684\u8b8a\u6578\u7684\u503c\uff0c\u6b63\u56e0\u70ba\u9700\u8981\u56de\u50b3\u503c\uff0c\u6240\u4ee5\u9019\u908a\u7528\u5230\u4e86 <code>defer<\/code> \u5f85\u56de\u50b3\u503c\u4e4b\u5f8c\u6703\u547c\u53eb <code>c.mux.Unlock()<\/code> \u4f86\u89e3\u9396\u3002<br \/>\n\u5176\u5be6\u4e5f\u53ef\u4ee5\u5728\u524d\u9762\u5148\u5beb\u4e0b <code>defer c.mux.Unlock()<\/code> \u4f86\u907f\u514d\u65b9\u6cd5\u7d50\u675f\u5f8c\u6c92\u6709\u89e3\u9396\u7684\u7a98\u5883\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u9019\u7bc7\u8ddf\u5927\u5bb6\u4ecb\u7d39\u591a\u57f7\u884c\u5e8f\u5982\u4f55\u5229\u7528 <code>mutex<\/code> \u4f86\u89e3\u6c7a<a href=\"https:\/\/en.wikipedia.org\/wiki\/Race_condition\" title=\"\u7af6\u722d\u5371\u5bb3\" rel=\"nofollow noopener\" target=\"_blank\">race condition<\/a>\u7684\u554f\u984c\uff0c\u4e5f\u91dd\u5c0d\u76f8\u95dc\u8a9e\u6cd5\u505a\u9032\u4e00\u6b65\u8aaa\u660e\uff0c\u5e0c\u671b\u5c0d\u5927\u5bb6\u7684 go \u591a\u57f7\u884c\u5e8f\u7de8\u7a0b\u80fd\u6709\u4e9b\u5e6b\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u57f7\u884c\u5e8f\u554f\u984c\u7684\u89e3\u6c7a \u5728\u4e0a\u4e00\u7bc7\u4ecb\u7d39\u4e86\u591a\u57f7\u884c\u5e8f\u7684\u597d\u8655\u4ee5\u53ca\u64b0\u5beb\u65b9\u5f0f\uff0c\u4f46\u662f\u4e5f\u63d0\u5230\u4e86\u591a\u57f7\u884c\u5e8f\u7684\u554f\u984c\uff0c\u6240\u4ee5\u9019\u7bc7\u91dd\u5c0d\u591a\u57f7\u884c\u5e8f &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/09\/30\/go-mutex-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-\u57f7\u884c\u5e8f\u7684\u9396\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81],"class_list":["post-1600","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1600"}],"version-history":[{"count":4,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1600\/revisions"}],"predecessor-version":[{"id":2675,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1600\/revisions\/2675"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}