{"id":57762,"date":"2024-10-10T20:56:24","date_gmt":"2024-10-10T12:56:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57762\/"},"modified":"2024-10-10T20:56:24","modified_gmt":"2024-10-10T12:56:24","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%ad%89%e5%be%85%e7%b5%84%ef%bc%88sync-waitgroup%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57762\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u7b49\u5f85\u7d44\uff08sync.WaitGroup\uff09"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%ad%89%e5%be%85%e7%b5%84%ef%bc%88sync-waitgroup%ef%bc%89-LfxpKkXscE\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u7b49\u5f85\u7d44\uff08sync.WaitGroup\uff09<\/h1>\n<p>\u5728\u4e26\u884c\u7de8\u7a0b\u4e2d\uff0c\u7ba1\u7406\u591a\u500b\u57f7\u884c\u7dd2\u7684\u751f\u547d\u9031\u671f\u662f\u4e00\u9805\u91cd\u8981\u7684\u4efb\u52d9\u3002Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8655\u7406\u9019\u500b\u554f\u984c\uff0c\u90a3\u5c31\u662f <code>sync.WaitGroup<\/code>\u3002\u9019\u500b\u7d50\u69cb\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u7b49\u5f85\u4e00\u7d44 goroutine \u5b8c\u6210\uff0c\u5f9e\u800c\u7c21\u5316\u4e86\u4e26\u884c\u7a0b\u5f0f\u7684\u63a7\u5236\u6d41\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-sync-waitgroup%ef%bc%9f-LfxpKkXscE\">\u4ec0\u9ebc\u662f sync.WaitGroup\uff1f<\/h2>\n<p><code>sync.WaitGroup<\/code> \u662f Go \u8a9e\u8a00\u6a19\u6e96\u5eab\u4e2d\u7684\u4e00\u500b\u7d50\u69cb\uff0c\u4f4d\u65bc <code>sync<\/code> \u5305\u4e2d\u3002\u5b83\u7684\u4e3b\u8981\u529f\u80fd\u662f\u8ddf\u8e2a\u4e00\u7d44 goroutine \u7684\u5b8c\u6210\u72c0\u614b\u3002\u7576\u6211\u5011\u555f\u52d5\u591a\u500b goroutine \u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>WaitGroup<\/code> \u4f86\u78ba\u4fdd\u4e3b\u7a0b\u5f0f\u5728\u6240\u6709 goroutine \u5b8c\u6210\u4e4b\u524d\u4e0d\u6703\u9000\u51fa\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-sync-waitgroup-LfxpKkXscE\">\u5982\u4f55\u4f7f\u7528 sync.WaitGroup<\/h2>\n<p>\u4f7f\u7528 <code>sync.WaitGroup<\/code> \u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5275\u5efa\u4e00\u500b <code>WaitGroup<\/code> \u5be6\u4f8b\u3002<\/li>\n<li>\u5728\u555f\u52d5\u6bcf\u500b goroutine \u4e4b\u524d\uff0c\u8abf\u7528 <code>Add<\/code> \u65b9\u6cd5\u4f86\u589e\u52a0\u8a08\u6578\u3002<\/li>\n<li>\u5728\u6bcf\u500b goroutine \u5b8c\u6210\u6642\uff0c\u8abf\u7528 <code>Done<\/code> \u65b9\u6cd5\u4f86\u6e1b\u5c11\u8a08\u6578\u3002<\/li>\n<li>\u5728\u4e3b\u7a0b\u5f0f\u4e2d\uff0c\u8abf\u7528 <code>Wait<\/code> \u65b9\u6cd5\u4f86\u963b\u585e\uff0c\u76f4\u5230\u8a08\u6578\u70ba\u96f6\u3002<\/li>\n<\/ol>\n<h3 id=\"%e7%af%84%e4%be%8b%e7%a8%8b%e5%bc%8f%e7%a2%bc-LfxpKkXscE\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h3>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n    \"time\"\n)\n\nfunc worker(id int, wg *sync.WaitGroup) {\n    defer wg.Done() \/\/ \u78ba\u4fdd\u5728\u51fd\u6578\u7d50\u675f\u6642\u6e1b\u5c11\u8a08\u6578\n    fmt.Printf(\"Worker %d startingn\", id)\n    time.Sleep(time.Second) \/\/ \u6a21\u64ec\u5de5\u4f5c\n    fmt.Printf(\"Worker %d donen\", id)\n}\n\nfunc main() {\n    var wg sync.WaitGroup\n\n    for i := 1; i &lt;= 5; i++ {\n        wg.Add(1) \/\/ \u589e\u52a0\u8a08\u6578\n        go worker(i, &amp;wg) \/\/ \u555f\u52d5 goroutine\n    }\n\n    wg.Wait() \/\/ \u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210\n    fmt.Println(&quot;All workers done&quot;)\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e94\u500b goroutine\uff0c\u6bcf\u500b goroutine \u90fd\u6703\u6a21\u64ec\u4e00\u6bb5\u5de5\u4f5c\u6642\u9593\u3002\u4e3b\u7a0b\u5f0f\u6703\u5728\u6240\u6709 goroutine \u5b8c\u6210\u4e4b\u524d\u4fdd\u6301\u904b\u884c\uff0c\u78ba\u4fdd\u6240\u6709\u5de5\u4f5c\u90fd\u5df2\u5b8c\u6210\u3002<\/p>\n<h2 id=\"sync-waitgroup-%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-LfxpKkXscE\">sync.WaitGroup \u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li><strong>\u8a08\u6578\u5fc5\u9808\u6b63\u78ba\uff1a<\/strong>\u5728\u4f7f\u7528 <code>Add<\/code> \u548c <code>Done<\/code> \u6642\uff0c\u5fc5\u9808\u78ba\u4fdd\u8a08\u6578\u7684\u6b63\u78ba\u6027\u3002\u5982\u679c <code>Add<\/code> \u7684\u6b21\u6578\u8207 <code>Done<\/code> \u7684\u6b21\u6578\u4e0d\u5339\u914d\uff0c\u5c07\u6703\u5c0e\u81f4 <code>Wait<\/code> \u6c38\u9060\u963b\u585e\u3002<\/li>\n<li><strong>\u4e0d\u8981\u5728 goroutine \u4e2d\u4f7f\u7528 WaitGroup\uff1a<\/strong>\u61c9\u907f\u514d\u5728 goroutine \u4e2d\u8abf\u7528 <code>Wait<\/code>\uff0c\u56e0\u70ba\u9019\u6703\u5c0e\u81f4\u6b7b\u9396\u3002<\/li>\n<li><strong>\u4f7f\u7528 defer\uff1a<\/strong>\u5728 goroutine \u4e2d\u4f7f\u7528 <code>defer wg.Done()<\/code> \u662f\u4e00\u500b\u826f\u597d\u7684\u7fd2\u6163\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5373\u4f7f\u767c\u751f\u932f\u8aa4\u4e5f\u80fd\u6b63\u78ba\u6e1b\u5c11\u8a08\u6578\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-LfxpKkXscE\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c<code>sync.WaitGroup<\/code> \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u6709\u6548\u5730\u7ba1\u7406\u591a\u500b goroutine \u7684\u57f7\u884c\u3002\u901a\u904e\u6b63\u78ba\u4f7f\u7528 <code>Add<\/code>\u3001<code>Done<\/code> \u548c <code>Wait<\/code> \u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u78ba\u4fdd\u4e3b\u7a0b\u5f0f\u5728\u6240\u6709\u5de5\u4f5c\u5b8c\u6210\u4e4b\u524d\u4e0d\u6703\u9000\u51fa\u3002\u9019\u5c0d\u65bc\u9700\u8981\u4e26\u884c\u8655\u7406\u7684\u61c9\u7528\u7a0b\u5f0f\u4f86\u8aaa\uff0c\u7121\u7591\u662f\u4e00\u500b\u91cd\u8981\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f\u958b\u767c\u74b0\u5883\u9084\u662f\u751f\u7522\u74b0\u5883\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Go \u8a9e\u8a00\u4e2d\u7684\u7b49\u5f85\u7d44\uff08sync.WaitGroup\uff09\uff0c\u5b78\u7fd2\u5982\u4f55\u6709\u6548\u7ba1\u7406\u4e26\u767c\u4efb\u52d9\u7684\u57f7\u884c\u8207\u540c\u6b65\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57762","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57762","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=57762"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57762\/revisions"}],"predecessor-version":[{"id":57763,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57762\/revisions\/57763"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}