{"id":41274,"date":"2024-12-01T08:18:42","date_gmt":"2024-12-01T00:18:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41274\/"},"modified":"2024-12-01T08:18:42","modified_gmt":"2024-12-01T00:18:42","slug":"%e6%9b%b4%e6%94%b9%e5%9c%b0%e5%9b%be%e9%93%be%e6%8e%a5%e5%b9%b6%e5%8f%91%e5%ae%89%e5%85%a8%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41274\/","title":{"rendered":"\u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f"},"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>\u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f<\/span><\/p>\n<h1>\u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 17:54:34<\/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\u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f\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>\u60a8\u77e5\u9053\u5417\uff0c\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u5c06\u6620\u5c04\u53d8\u91cf\u94fe\u63a5\u66f4\u6539\u4e3a\u53e6\u4e00\u4e2a\u94fe\u63a5\u662f\u5426\u5b89\u5168\uff1f<\/p>\n<p>\u4e00\u4e2a\u793a\u4f8b\u662f\uff0c\u5728\u4e00\u4e2a goroutine \u4e2d\u5c06\u6620\u5c04 <code>data<\/code> \u66ff\u6362\u4e3a\u65b0\u6620\u5c04\uff0c\u5e76\u5728\u53e6\u4e00\u4e2a goroutine \u4e2d\u8bfb\u53d6\u5176\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<pre>import (\n    \"fmt\"\n    \"math\/rand\"\n    \"strconv\"\n    \"testing\"\n    \"time\"\n)\n\nfunc TestMap(t *testing.T) {\n    s1 := rand.NewSource(time.Now().UnixNano())\n    r1 := rand.New(s1)\n    data := fill(r1.Intn(100))\n\n    timer := time.NewTimer(10 * time.Second)\n\n    go func() {\n        s1 := rand.NewSource(time.Now().UnixNano())\n        r1 := rand.New(s1)\n\n        for {\n            select {\n            case &lt;-timer.C:\n                return\n            default:\n\n            }\n            p := r1.Intn(100)\n            v := fill(p)\n            data = v\n            fmt.Println(\"_p=\" + strconv.Itoa(p))\n        }\n    }()\n\n    for range []int{1, 2, 3, 4, 5, 6, 7, 8} {\n        go func() {\n            s1 := rand.NewSource(time.Now().UnixNano())\n            r1 := rand.New(s1)\n            for {\n\n                select {\n                case &lt;-timer.C:\n                    return\n                default:\n\n                }\n\n                n := r1.Intn(100)\n                s := strconv.Itoa(n)\n                fmt.Println(data[s])\n            }\n        }()\n    }\n\n    &lt;-timer.C\n}\n\nfunc fill(postfix int) map[string][]string {\n    m := make(map[string][]string)\n    for i := 0; i &lt; 100; i++ {\n        s := strconv.Itoa(i)\n        m[s] = []string{s + \"_\" + strconv.Itoa(postfix)}\n    }\n    return m\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u7136\u4e0d\u5b89\u5168\u3002\u6ca1\u6709\u53d8\u91cf\uff08\u5305\u62ec\u6620\u5c04\uff09\u5bf9\u4e8e\u5e76\u53d1\u8bfb\u5199\u662f\u5b89\u5168\u7684\u3002<\/p>\n<p>\u4f60\u7684\u4e00\u4e2a goroutine \u5199\u5165 <code>data<\/code>\uff0c\u53e6\u4e00\u4e2a goroutine \u8bfb\u53d6\u5b83\u3002\u4f7f\u7528 <code>go test -race<\/code> \u8fd0\u884c\u6d4b\u8bd5\u8fd8\u4f1a\u62a5\u544a\u6570\u636e\u4e89\u7528\uff1a<\/p>\n<pre>testing.go:954: race detected during execution of test<\/pre>\n<p>\u591a\u4e2agoroutine\u4e2d\u5fc5\u987b\u540c\u6b65\u8bfb\u5199<code>data<\/code>\u53d8\u91cf\u3002<\/p>\n<p>\u4e8b\u5b9e\u5e76\u975e\u5982\u6b64\uff0c\u4f7f\u7528sync.mutex\u6765\u9501\u5b9a\u5b83\uff0c\u5e76\u5728\u66f4\u65b0\u6570\u636e\u65f6\u89e3\u9501\u5b83\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f \u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-19 17:54:34 0\u6d4f\u89c8 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300a\u66f4\u6539\u5730\u56fe\u94fe\u63a5\u5e76\u53d1\u5b89\u5168\u5417\uff1f\u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591aGolang\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~ \u95ee\u9898\u5185\u5bb9 \u60a8\u77e5\u9053\u5417\uff0c\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u5c06\u6620\u5c04\u53d8\u91cf\u94fe\u63a5\u66f4\u6539\u4e3a\u53e6\u4e00\u4e2a\u94fe\u63a5\u662f\u5426\u5b89\u5168\uff1f \u4e00\u4e2a\u793a\u4f8b\u662f\uff0c\u5728\u4e00\u4e2a goroutine \u4e2d\u5c06\u6620\u5c04 data \u66ff\u6362\u4e3a\u65b0\u6620\u5c04\uff0c\u5e76\u5728\u53e6\u4e00\u4e2a goroutine \u4e2d\u8bfb\u53d6\u5176\u4e2d\u7684\u5143\u7d20\uff1a import ( &#8220;fmt&#8221; &#8220;math\/rand&#8221; &#8220;strconv&#8221; &#8220;testing&#8221; &#8220;time&#8221; ) func TestMap(t *testing.T) { s1 := rand.NewSource(time.Now().UnixNano()) r1 := rand.New(s1) data := fill(r1.Intn(100)) timer := time.NewTimer(10 * time.Second) go func() { s1 := rand.NewSource(time.Now().UnixNano()) r1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41274","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41274"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41274\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}