{"id":2882,"date":"2019-02-26T14:43:51","date_gmt":"2019-02-26T06:43:51","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2882"},"modified":"2019-02-26T14:43:51","modified_gmt":"2019-02-26T06:43:51","slug":"go-1-9-sync-map%e6%8f%ad%e7%a7%98","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2882","title":{"rendered":"Go 1.9 sync.Map\u63ed\u79d8"},"content":{"rendered":"<div class=\"post-topheader custom- pt0\">\n<div class=\"mb20\">\n<div class=\"block-for-right-border\">\n<div class=\"row\">\n<div class=\"col-md-12 col-sm-12 col-xs-12\">\n<div class=\"post-topheader__info\" data-username=\"Kotexy\" data-userslug=\"kotexy\" data-useravatar=\"https:\/\/avatar-static.segmentfault.com\/142\/271\/1422711347-5a421232ed786_big64\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"article fmt article__content\" data-id=\"1190000010294041\" data-license=\"\">\n<p>\u5728Go 1.6\u4e4b\u524d\uff0c \u5185\u7f6e\u7684map\u7c7b\u578b\u662f\u90e8\u5206goroutine\u5b89\u5168\u7684\uff0c\u5e76\u53d1\u7684\u8bfb\u6ca1\u6709\u95ee\u9898\uff0c\u5e76\u53d1\u7684\u5199\u53ef\u80fd\u6709\u95ee\u9898\u3002\u81eago 1.6\u4e4b\u540e\uff0c \u5e76\u53d1\u5730\u8bfb\u5199map\u4f1a\u62a5\u9519\uff0c\u8fd9\u5728\u4e00\u4e9b\u77e5\u540d\u7684\u5f00\u6e90\u5e93\u4e2d\u90fd\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u6240\u4ee5go 1.9\u4e4b\u524d\u7684\u89e3\u51b3\u65b9\u6848\u662f\u989d\u5916\u7ed1\u5b9a\u4e00\u4e2a\u9501\uff0c\u5c01\u88c5\u6210\u4e00\u4e2a\u65b0\u7684struct\u6216\u8005\u5355\u72ec\u4f7f\u7528\u9501\u90fd\u53ef\u4ee5\u3002<\/p>\n<p>\u672c\u6587\u5e26\u4f60\u6df1\u5165\u5230sync.Map\u7684\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c\u770b\u770b\u4e3a\u4e86\u589e\u52a0\u4e00\u4e2a\u529f\u80fd\uff0c\u4ee3\u7801\u662f\u5982\u4f55\u53d8\u7684\u590d\u6742\u7684,\u4ee5\u53ca\u4f5c\u8005\u5728\u5b9e\u73b0sync.Map\u7684\u4e00\u4e9b\u601d\u60f3\u3002<\/p>\n<h2 id=\"articleHeader0\">\u6709\u5e76\u53d1\u95ee\u9898\u7684map<\/h2>\n<p>\u5b98\u65b9\u7684faq\u5df2\u7ecf\u63d0\u5230\u5185\u5efa\u7684map\u4e0d\u662f\u7ebf\u7a0b(goroutine)\u5b89\u5168\u7684\u3002<\/p>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u6bb5\u5e76\u53d1\u8bfb\u5199\u7684\u4ee3\u7801,\u4e0b\u5217\u7a0b\u5e8f\u4e2d\u4e00\u4e2agoroutine\u4e00\u76f4\u8bfb\uff0c\u4e00\u4e2agoroutine\u4e00\u53ea\u5199\u540c\u4e00\u4e2a\u952e\u503c\uff0c\u5373\u5373\u4f7f\u8bfb\u5199\u7684\u952e\u4e0d\u76f8\u540c\uff0c\u800c\u4e14map\u4e5f\u6ca1\u6709&#8221;\u6269\u5bb9&#8221;\u7b49\u64cd\u4f5c\uff0c\u4ee3\u7801\u8fd8\u662f\u4f1a\u62a5\u9519\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package main\r\nfunc main() {\r\n    m := make(map[int]int)\r\n    go func() {\r\n        for {\r\n            _ = m[1]\r\n        }\r\n    }()\r\n    go func() {\r\n        for {\r\n            m[2] = 2\r\n        }\r\n    }()\r\n    select {}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u9519\u8bef\u4fe1\u606f\u662f: fatal error: concurrent map read and map write\u3002<\/p>\n<p>\u5982\u679c\u4f60\u67e5\u770bGo\u7684\u6e90\u4ee3\u7801: hashmap_fast.go#L118,\u4f1a\u770b\u5230\u8bfb\u7684\u65f6\u5019\u4f1a\u68c0\u67e5hashWriting\u6807\u5fd7\uff0c \u5982\u679c\u6709\u8fd9\u4e2a\u6807\u5fd7\uff0c\u5c31\u4f1a\u62a5\u5e76\u53d1\u9519\u8bef\u3002<\/p>\n<p>\u5199\u7684\u65f6\u5019\u4f1a\u8bbe\u7f6e\u8fd9\u4e2a\u6807\u5fd7: hashmap.go#L542<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">h.flags |= hashWriting<\/pre>\n<p>&nbsp;<\/p>\n<p>hashmap.go#L628\u8bbe\u7f6e\u5b8c\u4e4b\u540e\u4f1a\u53d6\u6d88\u8fd9\u4e2a\u6807\u8bb0\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u4ee3\u7801\u4e2d\u8fd8\u6709\u597d\u51e0\u5904\u5e76\u53d1\u8bfb\u5199\u7684\u68c0\u67e5\uff0c \u6bd4\u5982\u5199\u7684\u65f6\u5019\u4e5f\u4f1a\u68c0\u67e5\u662f\u4e0d\u662f\u6709\u5e76\u53d1\u7684\u5199\uff0c\u5220\u9664\u952e\u7684\u65f6\u5019\u7c7b\u4f3c\u5199\uff0c\u904d\u5386\u7684\u65f6\u5019\u5e76\u53d1\u8bfb\u5199\u95ee\u9898\u7b49\u3002<\/p>\n<p>\u6709\u65f6\u5019\uff0cmap\u7684\u5e76\u53d1\u95ee\u9898\u4e0d\u662f\u90a3\u4e48\u5bb9\u6613\u88ab\u53d1\u73b0, \u4f60\u53ef\u4ee5\u5229\u7528-race\u53c2\u6570\u6765\u68c0\u67e5\u3002<\/p>\n<h2 id=\"articleHeader1\">Go 1.9\u4e4b\u524d\u7684\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u4f46\u662f\uff0c\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u4f1a\u5e76\u53d1\u5730\u4f7f\u7528map\u5bf9\u8c61\uff0c\u5c24\u5176\u662f\u5728\u4e00\u5b9a\u89c4\u6a21\u7684\u9879\u76ee\u4e2d\uff0cmap\u603b\u4f1a\u4fdd\u5b58goroutine\u5171\u4eab\u7684\u6570\u636e\u3002\u5728Go\u5b98\u65b9blog\u7684Go maps in action\u4e00\u6587\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u4fbf\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var counter = struct{\r\n    sync.RWMutex\r\n    m map[string]int\r\n}{m: make(map[string]int)}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5b83\u4f7f\u7528\u5d4c\u5165struct\u4e3amap\u589e\u52a0\u4e00\u4e2a\u8bfb\u5199\u9501\u3002<\/p>\n<p>\u8bfb\u6570\u636e\u7684\u65f6\u5019\u5f88\u65b9\u4fbf\u7684\u52a0\u9501\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">counter.RLock()\r\nn := counter.m[\"some_key\"]\r\ncounter.RUnlock()\r\nfmt.Println(\"some_key:\", n)<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5199\u6570\u636e\u7684\u65f6\u5019:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">unter.Lock()\r\ncounter.m[\"some_key\"]++\r\ncounter.Unlock()<\/pre>\n<p>&nbsp;<\/p>\n<h2 id=\"articleHeader2\">sync.Map<\/h2>\n<p>\u53ef\u4ee5\u8bf4\uff0c\u4e0a\u9762\u7684\u89e3\u51b3\u65b9\u6848\u76f8\u5f53\u7b80\u6d01\uff0c\u5e76\u4e14\u5229\u7528\u8bfb\u5199\u9501\u800c\u4e0d\u662fMutex\u53ef\u4ee5\u8fdb\u4e00\u6b65\u51cf\u5c11\u8bfb\u5199\u7684\u65f6\u5019\u56e0\u4e3a\u9501\u5e26\u6765\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5b83\u5728\u4e00\u4e9b\u573a\u666f\u4e0b\u4e5f\u6709\u95ee\u9898\uff0c\u5982\u679c\u719f\u6089Java\u7684\u540c\u5b66\uff0c\u53ef\u4ee5\u5bf9\u6bd4\u4e00\u4e0bjava\u7684ConcurrentHashMap\u7684\u5b9e\u73b0\uff0c\u5728map\u7684\u6570\u636e\u975e\u5e38\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u628a\u9501\u4f1a\u5bfc\u81f4\u5927\u5e76\u53d1\u7684\u5ba2\u6237\u7aef\u5171\u4e89\u4e00\u628a\u9501\uff0cJava\u7684\u89e3\u51b3\u65b9\u6848\u662fshard, \u5185\u90e8\u4f7f\u7528\u591a\u4e2a\u9501\uff0c\u6bcf\u4e2a\u533a\u95f4\u5171\u4eab\u4e00\u628a\u9501\uff0c\u8fd9\u6837\u51cf\u5c11\u4e86\u6570\u636e\u5171\u4eab\u4e00\u628a\u9501\u5e26\u6765\u7684\u6027\u80fd\u5f71\u54cd\uff0corcaman\u63d0\u4f9b\u4e86\u8fd9\u4e2a\u601d\u8def\u7684\u4e00\u4e2a\u5b9e\u73b0\uff1a concurrent-map\uff0c\u4ed6\u4e5f\u8be2\u95ee\u4e86Go\u76f8\u5173\u7684\u5f00\u53d1\u4eba\u5458\u662f\u5426\u5728Go\u4e2d\u4e5f\u5b9e\u73b0\u8fd9\u79cd\u65b9\u6848\uff0c\u7531\u4e8e\u5b9e\u73b0\u7684\u590d\u6742\u6027\uff0c\u7b54\u6848\u662fYes, we considered it.,\u4f46\u662f\u9664\u975e\u6709\u7279\u522b\u7684\u6027\u80fd\u63d0\u5347\u548c\u5e94\u7528\u573a\u666f\uff0c\u5426\u5219\u6ca1\u6709\u8fdb\u4e00\u6b65\u7684\u5f00\u53d1\u6d88\u606f\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5728Go 1.9\u4e2dsync.Map\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u5462\uff1f\u5b83\u662f\u5982\u4f55\u89e3\u51b3\u5e76\u53d1\u63d0\u5347\u6027\u80fd\u7684\u5462\uff1f<\/p>\n<p>sync.Map\u7684\u5b9e\u73b0\u6709\u51e0\u4e2a\u4f18\u5316\u70b9\uff0c\u8fd9\u91cc\u5148\u5217\u51fa\u6765\uff0c\u6211\u4eec\u540e\u9762\u6162\u6162\u5206\u6790\u3002<\/p>\n<p>\u7a7a\u95f4\u6362\u65f6\u95f4\u3002 \u901a\u8fc7\u5197\u4f59\u7684\u4e24\u4e2a\u6570\u636e\u7ed3\u6784(read\u3001dirty),\u5b9e\u73b0\u52a0\u9501\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u3002<br \/>\n\u4f7f\u7528\u53ea\u8bfb\u6570\u636e(read)\uff0c\u907f\u514d\u8bfb\u5199\u51b2\u7a81\u3002<br \/>\n\u52a8\u6001\u8c03\u6574\uff0cmiss\u6b21\u6570\u591a\u4e86\u4e4b\u540e\uff0c\u5c06dirty\u6570\u636e\u63d0\u5347\u4e3aread\u3002<br \/>\ndouble-checking\u3002<br \/>\n\u5ef6\u8fdf\u5220\u9664\u3002 \u5220\u9664\u4e00\u4e2a\u952e\u503c\u53ea\u662f\u6253\u6807\u8bb0\uff0c\u53ea\u6709\u5728\u63d0\u5347dirty\u7684\u65f6\u5019\u624d\u6e05\u7406\u5220\u9664\u7684\u6570\u636e\u3002<br \/>\n\u4f18\u5148\u4eceread\u8bfb\u53d6\u3001\u66f4\u65b0\u3001\u5220\u9664\uff0c\u56e0\u4e3a\u5bf9read\u7684\u8bfb\u53d6\u4e0d\u9700\u8981\u9501\u3002<br \/>\n\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecdsync.Map\u7684\u91cd\u70b9\u4ee3\u7801\uff0c\u4ee5\u4fbf\u7406\u89e3\u5b83\u7684\u5b9e\u73b0\u601d\u60f3\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u770b\u4e00\u4e0bsync.Map\u7684\u6570\u636e\u7ed3\u6784\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">type Map struct {\r\n    \/\/ \u5f53\u6d89\u53ca\u5230dirty\u6570\u636e\u7684\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u9700\u8981\u4f7f\u7528\u8fd9\u4e2a\u9501\r\n    mu Mutex\r\n    \/\/ \u4e00\u4e2a\u53ea\u8bfb\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u4e3a\u53ea\u8bfb\uff0c\u6240\u4ee5\u4e0d\u4f1a\u6709\u8bfb\u5199\u51b2\u7a81\u3002\r\n    \/\/ \u6240\u4ee5\u4ece\u8fd9\u4e2a\u6570\u636e\u4e2d\u8bfb\u53d6\u603b\u662f\u5b89\u5168\u7684\u3002\r\n    \/\/ \u5b9e\u9645\u4e0a\uff0c\u5b9e\u9645\u4e5f\u4f1a\u66f4\u65b0\u8fd9\u4e2a\u6570\u636e\u7684entries,\u5982\u679centry\u662f\u672a\u5220\u9664\u7684(unexpunged), \u5e76\u4e0d\u9700\u8981\u52a0\u9501\u3002\u5982\u679centry\u5df2\u7ecf\u88ab\u5220\u9664\u4e86\uff0c\u9700\u8981\u52a0\u9501\uff0c\u4ee5\u4fbf\u66f4\u65b0dirty\u6570\u636e\u3002\r\n    read atomic.Value \/\/ readOnly\r\n    \/\/ dirty\u6570\u636e\u5305\u542b\u5f53\u524d\u7684map\u5305\u542b\u7684entries,\u5b83\u5305\u542b\u6700\u65b0\u7684entries(\u5305\u62ecread\u4e2d\u672a\u5220\u9664\u7684\u6570\u636e,\u867d\u6709\u5197\u4f59\uff0c\u4f46\u662f\u63d0\u5347dirty\u5b57\u6bb5\u4e3aread\u7684\u65f6\u5019\u975e\u5e38\u5feb\uff0c\u4e0d\u7528\u4e00\u4e2a\u4e00\u4e2a\u7684\u590d\u5236\uff0c\u800c\u662f\u76f4\u63a5\u5c06\u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u4f5c\u4e3aread\u5b57\u6bb5\u7684\u4e00\u90e8\u5206),\u6709\u4e9b\u6570\u636e\u8fd8\u53ef\u80fd\u6ca1\u6709\u79fb\u52a8\u5230read\u5b57\u6bb5\u4e2d\u3002\r\n    \/\/ \u5bf9\u4e8edirty\u7684\u64cd\u4f5c\u9700\u8981\u52a0\u9501\uff0c\u56e0\u4e3a\u5bf9\u5b83\u7684\u64cd\u4f5c\u53ef\u80fd\u4f1a\u6709\u8bfb\u5199\u7ade\u4e89\u3002\r\n    \/\/ \u5f53dirty\u4e3a\u7a7a\u7684\u65f6\u5019\uff0c \u6bd4\u5982\u521d\u59cb\u5316\u6216\u8005\u521a\u63d0\u5347\u5b8c\uff0c\u4e0b\u4e00\u6b21\u7684\u5199\u64cd\u4f5c\u4f1a\u590d\u5236read\u5b57\u6bb5\u4e2d\u672a\u5220\u9664\u7684\u6570\u636e\u5230\u8fd9\u4e2a\u6570\u636e\u4e2d\u3002\r\n    dirty map[interface{}]*entry\r\n    \/\/ \u5f53\u4eceMap\u4e2d\u8bfb\u53d6entry\u7684\u65f6\u5019\uff0c\u5982\u679cread\u4e2d\u4e0d\u5305\u542b\u8fd9\u4e2aentry,\u4f1a\u5c1d\u8bd5\u4ecedirty\u4e2d\u8bfb\u53d6\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f1a\u5c06misses\u52a0\u4e00\uff0c\r\n    \/\/ \u5f53misses\u7d2f\u79ef\u5230 dirty\u7684\u957f\u5ea6\u7684\u65f6\u5019\uff0c \u5c31\u4f1a\u5c06dirty\u63d0\u5347\u4e3aread,\u907f\u514d\u4ecedirty\u4e2dmiss\u592a\u591a\u6b21\u3002\u56e0\u4e3a\u64cd\u4f5cdirty\u9700\u8981\u52a0\u9501\u3002\r\n    misses int\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5b83\u7684\u6570\u636e\u7ed3\u6784\u5f88\u7b80\u5355\uff0c\u503c\u5305\u542b\u56db\u4e2a\u5b57\u6bb5\uff1aread\u3001mu\u3001dirty\u3001misses\u3002<\/p>\n<p>\u5b83\u4f7f\u7528\u4e86\u5197\u4f59\u7684\u6570\u636e\u7ed3\u6784read\u3001dirty\u3002dirty\u4e2d\u4f1a\u5305\u542bread\u4e2d\u4e3a\u5220\u9664\u7684entries\uff0c\u65b0\u589e\u52a0\u7684entries\u4f1a\u52a0\u5165\u5230dirty\u4e2d\u3002<\/p>\n<p>read\u7684\u6570\u636e\u7ed3\u6784\u662f\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">type readOnly struct {\r\n    m       map[interface{}]*entry\r\n    amended bool \/\/ \u5982\u679cMap.dirty\u6709\u4e9b\u6570\u636e\u4e0d\u5728\u4e2d\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u503c\u4e3atrue\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>amended\u6307\u660eMap.dirty\u4e2d\u6709readOnly.m\u672a\u5305\u542b\u7684\u6570\u636e\uff0c\u6240\u4ee5\u5982\u679c\u4eceMap.read\u627e\u4e0d\u5230\u6570\u636e\u7684\u8bdd\uff0c\u8fd8\u8981\u8fdb\u4e00\u6b65\u5230Map.dirty\u4e2d\u67e5\u627e\u3002<\/p>\n<p>\u5bf9Map.read\u7684\u4fee\u6539\u662f\u901a\u8fc7\u539f\u5b50\u64cd\u4f5c\u8fdb\u884c\u7684\u3002<\/p>\n<p>\u867d\u7136read\u548cdirty\u6709\u5197\u4f59\u6570\u636e\uff0c\u4f46\u8fd9\u4e9b\u6570\u636e\u662f\u901a\u8fc7\u6307\u9488\u6307\u5411\u540c\u4e00\u4e2a\u6570\u636e\uff0c\u6240\u4ee5\u5c3d\u7ba1Map\u7684value\u4f1a\u5f88\u5927\uff0c\u4f46\u662f\u5197\u4f59\u7684\u7a7a\u95f4\u5360\u7528\u8fd8\u662f\u6709\u9650\u7684\u3002<\/p>\n<p>readOnly.m\u548cMap.dirty\u5b58\u50a8\u7684\u503c\u7c7b\u578b\u662f*entry,\u5b83\u5305\u542b\u4e00\u4e2a\u6307\u9488p, \u6307\u5411\u7528\u6237\u5b58\u50a8\u7684value\u503c\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">type entry struct {\r\n    p unsafe.Pointer \/\/ *interface{}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>p\u6709\u4e09\u79cd\u503c\uff1a<\/p>\n<p>nil: entry\u5df2\u88ab\u5220\u9664\u4e86\uff0c\u5e76\u4e14m.dirty\u4e3anil<br \/>\nexpunged: entry\u5df2\u88ab\u5220\u9664\u4e86\uff0c\u5e76\u4e14m.dirty\u4e0d\u4e3anil\uff0c\u800c\u4e14\u8fd9\u4e2aentry\u4e0d\u5b58\u5728\u4e8em.dirty\u4e2d<br \/>\n\u5176\u5b83\uff1a entry\u662f\u4e00\u4e2a\u6b63\u5e38\u7684\u503c<br \/>\n\u4ee5\u4e0a\u662fsync.Map\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e0b\u9762\u6211\u4eec\u91cd\u70b9\u770b\u770bLoad\u3001Store\u3001Delete\u3001Range\u8fd9\u56db\u4e2a\u65b9\u6cd5\uff0c\u5176\u5b83\u8f85\u52a9\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003\u8fd9\u56db\u4e2a\u65b9\u6cd5\u6765\u7406\u89e3\u3002<\/p>\n<h3 id=\"articleHeader3\">Load<\/h3>\n<p>\u52a0\u8f7d\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u63d0\u4f9b\u4e00\u4e2a\u952ekey,\u67e5\u627e\u5bf9\u5e94\u7684\u503cvalue,\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u901a\u8fc7ok\u53cd\u6620\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (m *Map) Load(key interface{}) (value interface{}, ok bool) {\r\n    \/\/ 1.\u9996\u5148\u4ecem.read\u4e2d\u5f97\u5230\u53ea\u8bfbreadOnly,\u4ece\u5b83\u7684map\u4e2d\u67e5\u627e\uff0c\u4e0d\u9700\u8981\u52a0\u9501\r\n    read, _ := m.read.Load().(readOnly)\r\n    e, ok := read.m[key]\r\n    \/\/ 2. \u5982\u679c\u6ca1\u627e\u5230\uff0c\u5e76\u4e14m.dirty\u4e2d\u6709\u65b0\u6570\u636e\uff0c\u9700\u8981\u4ecem.dirty\u67e5\u627e\uff0c\u8fd9\u4e2a\u65f6\u5019\u9700\u8981\u52a0\u9501\r\n    if !ok &amp;&amp; read.amended {\r\n        m.mu.Lock()\r\n        \/\/ \u53cc\u68c0\u67e5\uff0c\u907f\u514d\u52a0\u9501\u7684\u65f6\u5019m.dirty\u63d0\u5347\u4e3am.read,\u8fd9\u4e2a\u65f6\u5019m.read\u53ef\u80fd\u88ab\u66ff\u6362\u4e86\u3002\r\n        read, _ = m.read.Load().(readOnly)\r\n        e, ok = read.m[key]\r\n        \/\/ \u5982\u679cm.read\u4e2d\u8fd8\u662f\u4e0d\u5b58\u5728\uff0c\u5e76\u4e14m.dirty\u4e2d\u6709\u65b0\u6570\u636e\r\n        if !ok &amp;&amp; read.amended {\r\n            \/\/ \u4ecem.dirty\u67e5\u627e\r\n            e, ok = m.dirty[key]\r\n            \/\/ \u4e0d\u7ba1m.dirty\u4e2d\u5b58\u4e0d\u5b58\u5728\uff0c\u90fd\u5c06misses\u8ba1\u6570\u52a0\u4e00\r\n            \/\/ missLocked()\u4e2d\u6ee1\u8db3\u6761\u4ef6\u540e\u5c31\u4f1a\u63d0\u5347m.dirty\r\n            m.missLocked()\r\n        }\r\n        m.mu.Unlock()\r\n    }\r\n    if !ok {\r\n        return nil, false\r\n    }\r\n    return e.load()\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u91cc\u6709\u4e24\u4e2a\u503c\u7684\u5173\u6ce8\u7684\u5730\u65b9\u3002\u4e00\u4e2a\u662f\u9996\u5148\u4ecem.read\u4e2d\u52a0\u8f7d\uff0c\u4e0d\u5b58\u5728\u7684\u60c5\u51b5\u4e0b\uff0c\u5e76\u4e14m.dirty\u4e2d\u6709\u65b0\u6570\u636e\uff0c\u52a0\u9501\uff0c\u7136\u540e\u4ecem.dirty\u4e2d\u52a0\u8f7d\u3002<\/p>\n<p>\u4e8c\u662f\u8fd9\u91cc\u4f7f\u7528\u4e86\u53cc\u68c0\u67e5\u7684\u5904\u7406\uff0c\u56e0\u4e3a\u5728\u4e0b\u9762\u7684\u4e24\u4e2a\u8bed\u53e5\u4e2d\uff0c\u8fd9\u4e24\u884c\u8bed\u53e5\u5e76\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if !ok &amp;&amp; read.amended {\r\n        m.mu.Lock()<\/pre>\n<p>&nbsp;<\/p>\n<p>\u867d\u7136\u7b2c\u4e00\u53e5\u6267\u884c\u7684\u65f6\u5019\u6761\u4ef6\u6ee1\u8db3\uff0c\u4f46\u662f\u5728\u52a0\u9501\u4e4b\u524d\uff0cm.dirty\u53ef\u80fd\u88ab\u63d0\u5347\u4e3am.read,\u6240\u4ee5\u52a0\u9501\u540e\u8fd8\u5f97\u518d\u68c0\u67e5m.read\uff0c\u540e\u7eed\u7684\u65b9\u6cd5\u4e2d\u90fd\u4f7f\u7528\u4e86\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u53cc\u68c0\u67e5\u7684\u6280\u672fJava\u7a0b\u5e8f\u5458\u975e\u5e38\u719f\u6089\u4e86\uff0c\u5355\u4f8b\u6a21\u5f0f\u7684\u5b9e\u73b0\u4e4b\u4e00\u5c31\u662f\u5229\u7528\u53cc\u68c0\u67e5\u7684\u6280\u672f\u3002<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u6211\u4eec\u67e5\u8be2\u7684\u952e\u503c\u6b63\u597d\u5b58\u5728\u4e8em.read\u4e2d\uff0c\u65e0\u987b\u52a0\u9501\uff0c\u76f4\u63a5\u8fd4\u56de\uff0c\u7406\u8bba\u4e0a\u6027\u80fd\u4f18\u5f02\u3002\u5373\u4f7f\u4e0d\u5b58\u5728\u4e8em.read\u4e2d\uff0c\u7ecf\u8fc7miss\u51e0\u6b21\u4e4b\u540e\uff0cm.dirty\u4f1a\u88ab\u63d0\u5347\u4e3am.read\uff0c\u53c8\u4f1a\u4ecem.read\u4e2d\u67e5\u627e\u3002\u6240\u4ee5\u5bf9\u4e8e\u66f4\u65b0\uff0f\u589e\u52a0\u8f83\u5c11\uff0c\u52a0\u8f7d\u5b58\u5728\u7684key\u5f88\u591a\u7684case,\u6027\u80fd\u57fa\u672c\u548c\u65e0\u9501\u7684map\u7c7b\u4f3c\u3002<\/p>\n<p>\u4e0b\u9762\u770b\u770bm.dirty\u662f\u5982\u4f55\u88ab\u63d0\u5347\u7684\u3002 missLocked\u65b9\u6cd5\u4e2d\u53ef\u80fd\u4f1a\u5c06m.dirty\u63d0\u5347\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (m *Map) missLocked() {\r\n    m.misses++\r\n    if m.misses &lt; len(m.dirty) {\r\n        return\r\n    }\r\n    m.read.Store(readOnly{m: m.dirty})\r\n    m.dirty = nil\r\n    m.misses = 0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4e0a\u9762\u7684\u6700\u540e\u4e09\u884c\u4ee3\u7801\u5c31\u662f\u63d0\u5347m.dirty\u7684\uff0c\u5f88\u7b80\u5355\u7684\u5c06m.dirty\u4f5c\u4e3areadOnly\u7684m\u5b57\u6bb5\uff0c\u539f\u5b50\u66f4\u65b0m.read\u3002\u63d0\u5347\u540em.dirty\u3001m.misses\u91cd\u7f6e\uff0c \u5e76\u4e14m.read.amended\u4e3afalse\u3002<\/p>\n<h3 id=\"articleHeader4\">Store<\/h3>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u662f\u66f4\u65b0\u6216\u8005\u65b0\u589e\u4e00\u4e2aentry\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (m *Map) Store(key, value interface{}) {\r\n    \/\/ \u5982\u679cm.read\u5b58\u5728\u8fd9\u4e2a\u952e\uff0c\u5e76\u4e14\u8fd9\u4e2aentry\u6ca1\u6709\u88ab\u6807\u8bb0\u5220\u9664\uff0c\u5c1d\u8bd5\u76f4\u63a5\u5b58\u50a8\u3002\r\n    \/\/ \u56e0\u4e3am.dirty\u4e5f\u6307\u5411\u8fd9\u4e2aentry,\u6240\u4ee5m.dirty\u4e5f\u4fdd\u6301\u6700\u65b0\u7684entry\u3002\r\n    read, _ := m.read.Load().(readOnly)\r\n    if e, ok := read.m[key]; ok &amp;&amp; e.tryStore(&amp;value) {\r\n        return\r\n    }\r\n    \/\/ \u5982\u679c`m.read`\u4e0d\u5b58\u5728\u6216\u8005\u5df2\u7ecf\u88ab\u6807\u8bb0\u5220\u9664\r\n    m.mu.Lock()\r\n    read, _ = m.read.Load().(readOnly)\r\n    if e, ok := read.m[key]; ok {\r\n        if e.unexpungeLocked() { \/\/\u6807\u8bb0\u6210\u672a\u88ab\u5220\u9664\r\n            m.dirty[key] = e \/\/m.dirty\u4e2d\u4e0d\u5b58\u5728\u8fd9\u4e2a\u952e\uff0c\u6240\u4ee5\u52a0\u5165m.dirty\r\n        }\r\n        e.storeLocked(&amp;value) \/\/\u66f4\u65b0\r\n    } else if e, ok := m.dirty[key]; ok { \/\/ m.dirty\u5b58\u5728\u8fd9\u4e2a\u952e\uff0c\u66f4\u65b0\r\n        e.storeLocked(&amp;value)\r\n    } else { \/\/\u65b0\u952e\u503c\r\n        if !read.amended { \/\/m.dirty\u4e2d\u6ca1\u6709\u65b0\u7684\u6570\u636e\uff0c\u5f80m.dirty\u4e2d\u589e\u52a0\u7b2c\u4e00\u4e2a\u65b0\u952e\r\n            m.dirtyLocked() \/\/\u4ecem.read\u4e2d\u590d\u5236\u672a\u5220\u9664\u7684\u6570\u636e\r\n            m.read.Store(readOnly{m: read.m, amended: true})\r\n        }\r\n        m.dirty[key] = newEntry(value) \/\/\u5c06\u8fd9\u4e2aentry\u52a0\u5165\u5230m.dirty\u4e2d\r\n    }\r\n    m.mu.Unlock()\r\n}\r\nfunc (m *Map) dirtyLocked() {\r\n    if m.dirty != nil {\r\n        return\r\n    }\r\n    read, _ := m.read.Load().(readOnly)\r\n    m.dirty = make(map[interface{}]*entry, len(read.m))\r\n    for k, e := range read.m {\r\n        if !e.tryExpungeLocked() {\r\n            m.dirty[k] = e\r\n        }\r\n    }\r\n}\r\nfunc (e *entry) tryExpungeLocked() (isExpunged bool) {\r\n    p := atomic.LoadPointer(&amp;e.p)\r\n    for p == nil {\r\n        \/\/ \u5c06\u5df2\u7ecf\u5220\u9664\u6807\u8bb0\u4e3anil\u7684\u6570\u636e\u6807\u8bb0\u4e3aexpunged\r\n        if atomic.CompareAndSwapPointer(&amp;e.p, nil, expunged) {\r\n            return true\r\n        }\r\n        p = atomic.LoadPointer(&amp;e.p)\r\n    }\r\n    return p == expunged\r\n}<\/pre>\n<p><code class=\"golang\"><\/code><\/p>\n<p>\u4f60\u53ef\u4ee5\u770b\u5230\uff0c\u4ee5\u4e0a\u64cd\u4f5c\u90fd\u662f\u5148\u4ece\u64cd\u4f5cm.read\u5f00\u59cb\u7684\uff0c\u4e0d\u6ee1\u8db3\u6761\u4ef6\u518d\u52a0\u9501\uff0c\u7136\u540e\u64cd\u4f5cm.dirty\u3002<\/p>\n<p>Store\u53ef\u80fd\u4f1a\u5728\u67d0\u79cd\u60c5\u51b5\u4e0b(\u521d\u59cb\u5316\u6216\u8005m.dirty\u521a\u88ab\u63d0\u5347\u540e)\u4ecem.read\u4e2d\u590d\u5236\u6570\u636e\uff0c\u5982\u679c\u8fd9\u4e2a\u65f6\u5019m.read\u4e2d\u6570\u636e\u91cf\u975e\u5e38\u5927\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<h3 id=\"articleHeader5\">Delete<\/h3>\n<p>\u5220\u9664\u4e00\u4e2a\u952e\u503c\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (m *Map) Delete(key interface{}) {\r\n    read, _ := m.read.Load().(readOnly)\r\n    e, ok := read.m[key]\r\n    if !ok &amp;&amp; read.amended {\r\n        m.mu.Lock()\r\n        read, _ = m.read.Load().(readOnly)\r\n        e, ok = read.m[key]\r\n        if !ok &amp;&amp; read.amended {\r\n            delete(m.dirty, key)\r\n        }\r\n        m.mu.Unlock()\r\n    }\r\n    if ok {\r\n        e.delete()\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u540c\u6837\uff0c\u5220\u9664\u64cd\u4f5c\u8fd8\u662f\u4ecem.read\u4e2d\u5f00\u59cb\uff0c \u5982\u679c\u8fd9\u4e2aentry\u4e0d\u5b58\u5728\u4e8em.read\u4e2d\uff0c\u5e76\u4e14m.dirty\u4e2d\u6709\u65b0\u6570\u636e\uff0c\u5219\u52a0\u9501\u5c1d\u8bd5\u4ecem.dirty\u4e2d\u5220\u9664\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u8fd8\u662f\u8981\u53cc\u68c0\u67e5\u7684\u3002 \u4ecem.dirty\u4e2d\u76f4\u63a5\u5220\u9664\u5373\u53ef\uff0c\u5c31\u5f53\u5b83\u6ca1\u5b58\u5728\u8fc7\uff0c\u4f46\u662f\u5982\u679c\u662f\u4ecem.read\u4e2d\u5220\u9664\uff0c\u5e76\u4e0d\u4f1a\u76f4\u63a5\u5220\u9664\uff0c\u800c\u662f\u6253\u6807\u8bb0\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (e *entry) delete() (hadValue bool) {\r\n    for {\r\n        p := atomic.LoadPointer(&amp;e.p)\r\n        \/\/ \u5df2\u6807\u8bb0\u4e3a\u5220\u9664\r\n        if p == nil || p == expunged {\r\n            return false\r\n        }\r\n        \/\/ \u539f\u5b50\u64cd\u4f5c\uff0ce.p\u6807\u8bb0\u4e3anil\r\n        if atomic.CompareAndSwapPointer(&amp;e.p, p, nil) {\r\n            return true\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3 id=\"articleHeader6\">Range<\/h3>\n<p>\u56e0\u4e3afor &#8230; range map\u662f\u5185\u5efa\u7684\u8bed\u8a00\u7279\u6027\uff0c\u6240\u4ee5\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528for range\u904d\u5386sync.Map, \u4f46\u662f\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684Range\u65b9\u6cd5\uff0c\u901a\u8fc7\u56de\u8c03\u7684\u65b9\u5f0f\u904d\u5386\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (m *Map) Range(f func(key, value interface{}) bool) {\r\n    read, _ := m.read.Load().(readOnly)\r\n    \/\/ \u5982\u679cm.dirty\u4e2d\u6709\u65b0\u6570\u636e\uff0c\u5219\u63d0\u5347m.dirty,\u7136\u540e\u5728\u904d\u5386\r\n    if read.amended {\r\n        \/\/\u63d0\u5347m.dirty\r\n        m.mu.Lock()\r\n        read, _ = m.read.Load().(readOnly) \/\/\u53cc\u68c0\u67e5\r\n        if read.amended {\r\n            read = readOnly{m: m.dirty}\r\n            m.read.Store(read)\r\n            m.dirty = nil\r\n            m.misses = 0\r\n        }\r\n        m.mu.Unlock()\r\n    }\r\n    \/\/ \u904d\u5386, for range\u662f\u5b89\u5168\u7684\r\n    for k, e := range read.m {\r\n        v, ok := e.load()\r\n        if !ok {\r\n            continue\r\n        }\r\n        if !f(k, v) {\r\n            break\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Range\u65b9\u6cd5\u8c03\u7528\u524d\u53ef\u80fd\u4f1a\u505a\u4e00\u4e2am.dirty\u7684\u63d0\u5347\uff0c\u4e0d\u8fc7\u63d0\u5347m.dirty\u4e0d\u662f\u4e00\u4e2a\u8017\u65f6\u7684\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"articleHeader7\">sync.Map\u7684\u6027\u80fd<\/h2>\n<p>Go 1.9\u6e90\u4ee3\u7801\u4e2d\u63d0\u4f9b\u4e86\u6027\u80fd\u7684\u6d4b\u8bd5\uff1a map_bench_test.go\u3001map_reference_test.go<\/p>\n<p>\u6211\u4e5f\u57fa\u4e8e\u8fd9\u4e9b\u4ee3\u7801\u4fee\u6539\u4e86\u4e00\u4e0b\uff0c\u5f97\u5230\u4e0b\u9762\u7684\u6d4b\u8bd5\u6570\u636e\uff0c\u76f8\u6bd4\u8f83\u4ee5\u524d\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6027\u80fd\u591a\u5c11\u56de\u6709\u4e9b\u63d0\u5347\uff0c\u5982\u679c\u4f60\u7279\u522b\u5173\u6ce8\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651sync.Map\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">BenchmarkHitAll\/*sync.RWMutexMap-4       20000000            83.8 ns\/op\r\nBenchmarkHitAll\/*sync.Map-4              30000000            59.9 ns\/op\r\nBenchmarkHitAll_WithoutPrompting\/*sync.RWMutexMap-4             20000000            96.9 ns\/op\r\nBenchmarkHitAll_WithoutPrompting\/*sync.Map-4                    20000000            64.1 ns\/op\r\nBenchmarkHitNone\/*sync.RWMutexMap-4                             20000000            79.1 ns\/op\r\nBenchmarkHitNone\/*sync.Map-4                                    30000000            43.3 ns\/op\r\nBenchmarkHit_WithoutPrompting\/*sync.RWMutexMap-4                20000000            81.5 ns\/op\r\nBenchmarkHit_WithoutPrompting\/*sync.Map-4                       30000000            44.0 ns\/op\r\nBenchmarkUpdate\/*sync.RWMutexMap-4                               5000000           328 ns\/op\r\nBenchmarkUpdate\/*sync.Map-4                                     10000000           146 ns\/op\r\nBenchmarkUpdate_WithoutPrompting\/*sync.RWMutexMap-4              5000000           336 ns\/op\r\nBenchmarkUpdate_WithoutPrompting\/*sync.Map-4                     5000000           324 ns\/op\r\nBenchmarkDelete\/*sync.RWMutexMap-4                              10000000           155 ns\/op\r\nBenchmarkDelete\/*sync.Map-4                                     30000000            55.0 ns\/op\r\nBenchmarkDelete_WithoutPrompting\/*sync.RWMutexMap-4             10000000           173 ns\/op\r\nBenchmarkDelete_WithoutPrompting\/*sync.Map-4                    10000000           147 ns\/op<\/pre>\n<p>&nbsp;<\/p>\n<h2 id=\"articleHeader8\">\u5176\u5b83<\/h2>\n<p>sync.Map\u6ca1\u6709Len\u65b9\u6cd5\uff0c\u5e76\u4e14\u76ee\u524d\u6ca1\u6709\u8ff9\u8c61\u8981\u52a0\u4e0a (issue#20680),\u6240\u4ee5\u5982\u679c\u60f3\u5f97\u5230\u5f53\u524dMap\u4e2d\u6709\u6548\u7684entries\u7684\u6570\u91cf\uff0c\u9700\u8981\u4f7f\u7528Range\u65b9\u6cd5\u904d\u5386\u4e00\u6b21\uff0c \u6bd4\u8f83X\u75bc\u3002<\/p>\n<p>LoadOrStore\u65b9\u6cd5\u5982\u679c\u63d0\u4f9b\u7684key\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5df2\u5b58\u5728\u7684\u503c(Load)\uff0c\u5426\u5219\u4fdd\u5b58\u63d0\u4f9b\u7684\u952e\u503c(Store)\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Go 1.6\u4e4b\u524d\uff0c \u5185\u7f6e\u7684map\u7c7b\u578b\u662f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[122],"class_list":["post-2882","post","type-post","status-publish","format-standard","hentry","tag-06-godocker"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=2882"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2882\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}