{"id":207635,"date":"2025-07-08T12:18:38","date_gmt":"2025-07-08T04:18:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207635\/"},"modified":"2025-07-08T12:18:38","modified_gmt":"2025-07-08T04:18:38","slug":"%e5%b9%b6%e5%8f%91%e5%ae%89%e5%85%a8%e7%9a%84%e5%88%87%e7%89%87%e6%98%a0%e5%b0%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207635\/","title":{"rendered":"\u5e76\u53d1\u5b89\u5168\u7684\u5207\u7247\u6620\u5c04"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5e76\u53d1\u5b89\u5168\u7684\u5207\u7247\u6620\u5c04<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 10:15:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5e76\u53d1\u5b89\u5168\u7684\u5207\u7247\u6620\u5c04\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u5305\u542b <code>sync.map<\/code> \u7684\u7c7b\u578b\uff0c\u5176\u4e2d\u6620\u5c04\u4e2d\u7684\u952e\u662f\u5b57\u7b26\u4e32\uff0c\u503c\u662f\u5207\u7247\u3002\u6211\u5c06\u9879\u76ee\u63d2\u5165\u5730\u56fe\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>newlist := []*item{item}\nif result, ok := map.loadorstore(key, newlist); ok {\n    resultlist := result.([]*item)\n    resultlist = append(resultlist, item)\n    map.store(key, resultlist)\n}<\/pre>\n<p>\u8fd9\u4e0d\u662f\u5e76\u53d1\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5207\u7247\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u8c03\u7528\u540c\u65f6\u52a0\u8f7d\u548c\u4fee\u6539\u3002\u8fd9\u6bb5\u4ee3\u7801\u975e\u5e38\u8106\u5f31\uff0c\u6240\u4ee5\u6211\u5c1d\u8bd5\u5c06\u5176\u4fee\u6539\u4e3a\uff1a<\/p>\n<pre>newlist := []*item{item}\nif result, ok := map.loadorstore(key, &amp;newlist); ok {\n    resultlist := result.(*[]*item)\n    *resultlist = append(*resultlist, item)\n}<\/pre>\n<p>\u8fd9\u4e00\u5207\u90fd\u662f\u4e3a\u4e86\u8ba9\u95ee\u9898\u786e\u5b9a\u6027\u5730\u53d1\u751f\u3002\u56e0\u6b64\uff0c\u6211\u6b63\u5728\u5c1d\u8bd5\u627e\u5230\u4e00\u79cd\u65b9\u6cd5\u6765\u83b7\u5f97\u53ef\u4ee5\u540c\u65f6\u6dfb\u52a0\u7684\u5207\u7247\u56fe\u3002\u6211\u7684\u672c\u80fd\u662f\u5728\u6dfb\u52a0\u5217\u8868\u65f6\u4f7f\u7528 <code>sync.mutex<\/code> \u6765\u9501\u5b9a\u5217\u8868\uff0c\u4f46\u4e3a\u4e86\u4fdd\u6301\u5bf9 <code>sync.map<\/code> \u7684\u5e76\u53d1\u8bbf\u95ee\uff0c\u6211\u8fd8\u9700\u8981\u521b\u5efa <code>sync.mutex<\/code> \u5bf9\u8c61\u7684\u6620\u5c04\uff0c\u5982\u4e0b\u6240\u793a\uff1a <\/p>\n<pre>newLock := sync.Mutex{}\nraw, _ := lockMap.LoadOrStore(key, &amp;newLock)\nlock := raw.(*sync.Mutex)\n\nnewList := []*Item{item}\nif result, ok := map.LoadOrStore(key, &amp;newList); ok {\n    lock.Lock()\n    resultList := result.(*[]*Item)\n    *resultList = append(*resultList, item)\n    lock.Unlock()\n}<\/pre>\n<p>\u6709\u6ca1\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u4e0e\u60a8\u5f53\u524d\u7684\u8ba1\u5212\u6ca1\u6709\u592a\u5927\u4e0d\u540c\uff0c\u4f46\u60a8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5e26\u6709\u5d4c\u5165\u4e92\u65a5\u9501\u7684\u7ed3\u6784\u6765\u5b58\u50a8\u6620\u5c04\u7684\u503c\uff0c\u4ece\u800c\u7701\u53bb\u5904\u7406\u4e24\u4e2a\u6620\u5c04\u7684\u9ebb\u70e6\u3002<\/p>\n<p>\u7ed3\u6784\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>type safeitems struct {\n    sync.mutex\n    items []*item\n}<\/pre>\n<p>\u5b83\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre>newMapEntry := SafeItems{Items: itemPtrList}\nif result, ok := map.LoadOrStore(key, &amp;newMapEntry); ok {\n    mapEntry := result.(*SafeItems)\n    mapEntry.Lock()\n    mapEntry.Items = append(mapEntry.Items, item)\n    mapEntry.Unlock()\n}<\/pre>\n<p>\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u53d8\u5316\uff0c\u4f46\u5b83\u786e\u5b9e\u63d0\u4f9b\u4e86\u4e00\u4e9b\u8bed\u6cd5\u7cd6\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5e76\u53d1\u5b89\u5168\u7684\u5207\u7247\u6620\u5c04\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207635","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207635","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=207635"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207635\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}