{"id":207003,"date":"2025-07-08T08:14:45","date_gmt":"2025-07-08T00:14:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207003\/"},"modified":"2025-07-08T08:14:45","modified_gmt":"2025-07-08T00:14:45","slug":"go-%e4%b8%ad%e7%9a%84%e7%ab%9e%e4%ba%89%e6%9d%a1%e4%bb%b6%e5%8f%91%e7%94%9f%e5%8f%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207003\/","title":{"rendered":"Go \u4e2d\u7684\u7ade\u4e89\u6761\u4ef6\u53d1\u751f\u53d8\u5316"},"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>Go \u4e2d\u7684\u7ade\u4e89\u6761\u4ef6\u53d1\u751f\u53d8\u5316<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 13:48: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\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGo \u4e2d\u7684\u7ade\u4e89\u6761\u4ef6\u53d1\u751f\u53d8\u5316\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u300ago in action\u300b\u5173\u4e8e\u7ade\u4e89\u6761\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>var (\n    counter int\n    wg sync.WaitGroup\n)\n\nfunc main() {\n    wg.Add(2)\n    go incCounter(1)\n    go incCounter(2)\n\n    wg.Wait()\n    fmt.Println(\"Final Counter:\", counter)\n}\n\nfunc incCounter(id int) {\n    defer wg.Done()\n\n    for count := 0; count &lt; 2; count++ {\n        value := counter\n        \/\/1 fmt.Println(\"value=\",value)\n        runtime.Gosched()\n\n        value++\n\n        counter = value\n        \/\/2 fmt.Println(\"counter=\",counter)\n    }\n}<\/pre>\n<p>\u636e\u8bf4final counter\u6700\u540e\u5e94\u8be5\u662f2\uff0c\u8fd9\u91cc\u89e3\u91ca\u4e00\u4e0b\uff1a \u201c\u6bcf\u4e2a goroutine \u90fd\u4f1a\u8986\u76d6\u53e6\u4e00\u4e2a goroutine \u7684\u5de5\u4f5c\u3002\u5f53 goroutine \u4ea4\u6362\u6b63\u5728\u8fdb\u884c\u4e2d\u3002\u6bcf\u4e2a goroutine \u90fd\u4f1a\u521b\u5efa\u81ea\u5df1\u7684\u8ba1\u6570\u5668\u53d8\u91cf\u526f\u672c\uff0c\u5e76\u4e14 then \u88ab\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a goroutine\u3002\u5f53\u7ed9 goroutine \u65f6\u95f4\u518d\u6b21\u6267\u884c\u65f6\uff0c\u8ba1\u6570\u5668\u53d8\u91cf\u7684\u503c\u5df2\u7ecf\u6539\u53d8\uff0c\u4f46 goroutine \u6ca1\u6709\u6539\u53d8 \u66f4\u65b0\u5176\u526f\u672c\u3002\u76f8\u53cd\uff0c\u5b83\u7ee7\u7eed\u589e\u52a0\u5b83\u6240\u62e5\u6709\u7684\u526f\u672c\u5e76\u8bbe\u7f6e\u503c \u8fd4\u56de\u5230\u8ba1\u6570\u5668\u53d8\u91cf\uff0c\u66ff\u6362\u5176\u4ed6 goroutine \u6267\u884c\u7684\u5de5\u4f5c\u3002\u201d <\/p>\n<p>\u6211\u731c\u662f\u73af\u5883\u539f\u56e0\uff0c\u6211\u7684\u673a\u5668\u5728go 1.10.3+win10\u4e0b\u8f93\u51fa4\u3002\u6211\u60f3\u77e5\u9053\u8fd9\u672c\u4e66\u51fa\u7248\u540e\uff0cgo \u53d1\u751f\u4e86\u4ec0\u4e48\u53d8\u5316\uff1f\u5982\u679c\u6211\u53d6\u6d88\u6ce8\u91ca\u6807\u8bb0 1 \u6700\u7ec8\u8ba1\u6570\u5668\u6253\u5370 2 \u6216\u968f\u673a\u5982\u679c\u6211\u53d6\u6d88\u6ce8\u91ca\u6807\u8bb0 2\u3002\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u672c\u4e66\u662f\u9519\u8bef\u7684\u3002\u6570\u636e\u7ade\u4e89\u7684\u8981\u70b9\u662f\u7ed3\u679c\u662f\u4e0d\u786e\u5b9a\u7684\u3002 <\/p>\n<p>\u4f8b\u5982\uff0c<code>final counter<\/code> \u53ef\u4ee5\u662f\u4efb\u4f55\u503c\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"runtime\"\n    \"sync\"\n)\n\nvar (\n    counter int\n    wg      sync.waitgroup\n)\n\nfunc main() {\n    wg.add(2)\n    go inccounter(1)\n    go inccounter(2)\n\n    wg.wait()\n    fmt.println(\"final counter:\", counter)\n}\n\nfunc inccounter(id int) {\n    defer wg.done()\n\n    for count := 0; count &lt; 2; count++ {\n        value := counter\n        \/\/1 fmt.println(\"value=\",value)\n        runtime.gosched()\n\n        value++\n\n        counter = value\n        \/\/2 fmt.println(\"counter=\",counter)\n    }\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>$ go version\ngo version devel +65fa2b615b Fri Aug 3 23:35:53 2018 +0000 linux\/amd64\n$ go run racer.go\nFinal Counter: 4\n$ go run racer.go\nFinal Counter: 2\n$ go run racer.go\nFinal Counter: 2\n$ go run racer.go\nFinal Counter: 2\n$ go run racer.go\nFinal Counter: 2\n$ go run racer.go\nFinal Counter: 4\n$ go run racer.go\nFinal Counter: 2\n$ go run racer.go\nFinal Counter: 4\n$ go run -race racer.go\n==================\nWARNING: DATA RACE\nRead at 0x0000005e4600 by goroutine 7:\n  main.incCounter()\n      \/home\/peter\/gopath\/src\/racer.go:27 +0x6f\n\nPrevious write at 0x0000005e4600 by goroutine 6:\n  main.incCounter()\n      \/home\/peter\/gopath\/src\/racer.go:33 +0x90\n\nGoroutine 7 (running) created at:\n  main.main()\n      \/home\/peter\/gopath\/src\/racer.go:17 +0x89\n\nGoroutine 6 (finished) created at:\n  main.main()\n      \/home\/peter\/gopath\/src\/racer.go:16 +0x68\n==================\nFinal Counter: 4\nFound 1 data race(s)\nexit status 66\n$<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aGo \u4e2d\u7684\u7ade\u4e89\u6761\u4ef6\u53d1\u751f\u53d8\u5316\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207003","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207003","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=207003"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207003\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}