{"id":2786,"date":"2019-01-31T14:26:28","date_gmt":"2019-01-31T06:26:28","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=2786"},"modified":"2019-01-31T14:29:27","modified_gmt":"2019-01-31T06:29:27","slug":"go%e8%af%ad%e8%a8%80%e5%b9%b6%e5%8f%91%e7%bc%96-sync%e5%8c%85%e4%b9%8bwaitgroup%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/2786","title":{"rendered":"GO\u8bed\u8a00\u5e76\u53d1\u7f16&#8211;sync\u5305\u4e4bWaitGroup\u7684\u4f7f\u7528"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix csdn-tracking-statistics\" data-pid=\"blog\" data-mod=\"popu_307\" data-dsm=\"post\">\n<div class=\"article-copyright\">\u6982\u8ff0<\/div>\n<div id=\"content_views\" class=\"markdown_views\">\n<p>WaitGroup\u7528\u4e8e\u7b49\u5f85\u4e00\u7ec4\u7ebf\u7a0b\u7684\u7ed3\u675f\u3002\u7236\u7ebf\u7a0b\u8c03\u7528Add\u65b9\u6cd5\u6765\u8bbe\u5b9a\u5e94\u7b49\u5f85\u7684\u7ebf\u7a0b\u7684\u6570\u91cf\u3002\u6bcf\u4e2a\u88ab\u7b49\u5f85\u7684\u7ebf\u7a0b\u5728\u7ed3\u675f\u65f6\u5e94\u8c03\u7528Done\u65b9\u6cd5\u3002\u540c\u65f6\uff0c\u4e3b\u7ebf\u7a0b\u91cc\u53ef\u4ee5\u8c03\u7528Wait\u65b9\u6cd5\u963b\u585e\u81f3\u6240\u6709\u7ebf\u7a0b\u7ed3\u675f\u3002<br \/>\n\u4f46\u5728\u4f7f\u7528\u65f6\uff0c\u4e5f\u6709\u4e00\u4e9b\u95ee\u9898\u9700\u8981\u6ce8\u610f\uff0c\u8bf7\u770b\u672c\u6587\u7684\u8be6\u7ec6\u5206\u89e3\u3002<br \/>\n\u53e6\u5916\uff0cWaitGroup\u7684\u4f7f\u7528\u573a\u666f\u5341\u5206\u6709\u9650\uff0c\u4e3a\u4ec0\u4e48\u5462\uff1f\u5177\u4f53\u539f\u56e0\uff0c\u8bf7\u770b\u672c\u6587\u7684\u603b\u7ed3\u90e8\u5206\u7684\u5206\u6790\u3002<\/p>\n<h2 id=\"\u4e3b\u8981\u51fd\u6570\"><a name=\"t1\"><\/a>\u4e3b\u8981\u51fd\u6570<\/h2>\n<h3 id=\"func-waitgroup-add\"><a name=\"t2\"><\/a>func (*WaitGroup) Add<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (wg *WaitGroup) Add(delta int)<\/pre>\n<p>Add\u65b9\u6cd5\u5411\u5185\u90e8\u8ba1\u6570\u52a0\u4e0adelta\uff0cdelta\u53ef\u4ee5\u662f\u8d1f\u6570\uff1b\u5982\u679c\u5185\u90e8\u8ba1\u6570\u5668\u53d8\u4e3a0\uff0cWait\u65b9\u6cd5\u963b\u585e\u7b49\u5f85\u7684\u6240\u6709\u534f\u7a0b\u90fd\u4f1a\u91ca\u653e\uff0c\u5982\u679c\u8ba1\u6570\u5668\u5c0f\u4e8e0\uff0c\u5219\u8c03\u7528panic\u3002\u6ce8\u610fAdd\u52a0\u4e0a\u6b63\u6570\u7684\u8c03\u7528\u5e94\u5728Wait\u4e4b\u524d\uff0c\u5426\u5219Wait\u53ef\u80fd\u53ea\u4f1a\u7b49\u5f85\u5f88\u5c11\u7684\u534f\u7a0b\u3002\u4e00\u822c\u6765\u8bf4\u672c\u65b9\u6cd5\u5e94\u5728\u521b\u5efa\u65b0\u7684\u534f\u7a0b\u6216\u8005\u5176\u4ed6\u5e94\u7b49\u5f85\u7684\u4e8b\u4ef6\u4e4b\u524d\u8c03\u7528\u3002<\/p>\n<h3 id=\"func-wg-waitgroup-done\"><a name=\"t3\"><\/a>func (wg *WaitGroup) Done()<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (wg *WaitGroup) Done()<\/pre>\n<p>Done\u65b9\u6cd5\u51cf\u5c11WaitGroup\u8ba1\u6570\u5668\u7684\u503c\uff0c\u5e94\u5728\u534f\u7a0b\u7684\u6700\u540e\u6267\u884c\u3002<\/p>\n<h3 id=\"func-wg-waitgroup-wait\"><a name=\"t4\"><\/a>func (wg *WaitGroup) Wait()<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func (wg *WaitGroup) Wait()<\/pre>\n<p>Wait\u65b9\u6cd5\u963b\u585e\u76f4\u5230WaitGroup\u8ba1\u6570\u5668\u51cf\u4e3a0\u3002<\/p>\n<h2 id=\"\u7f16\u7a0b\u5b9e\u6218\"><a name=\"t5\"><\/a>\u7f16\u7a0b\u5b9e\u6218<\/h2>\n<h3 id=\"\u7b49\u5f85\u67d0\u4e2a\u534f\u7a0b\u7ed3\u675f\"><a name=\"t6\"><\/a>\u7b49\u5f85\u67d0\u4e2a\u534f\u7a0b\u7ed3\u675f<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func main() {\r\n    var wg sync.WaitGroup\r\n\r\n    wg.Add(1)\r\n    go func() {\r\n        defer wg.Done()\r\n        fmt.Println(\"1 goroutine sleep ...\")\r\n        time.Sleep(2e9)\r\n        fmt.Println(\"1 goroutine exit ...\")\r\n    }()\r\n\r\n    wg.Add(1)\r\n    go func() {\r\n        defer wg.Done()\r\n        fmt.Println(\"2 goroutine sleep ...\")\r\n        time.Sleep(4e9)\r\n        fmt.Println(\"2 goroutine exit ...\")\r\n    }()\r\n\r\n    fmt.Println(\"waiting for all goroutine \")\r\n    wg.Wait()\r\n    fmt.Println(\"All goroutines finished!\")\r\n}<\/pre>\n<h4 id=\"\u5c0f\u7ed3\">\u5c0f\u7ed3<\/h4>\n<p>\u8981\u6ce8\u610fAdd\u548cDone\u51fd\u6570\u4e00\u5b9a\u8981\u914d\u5bf9\uff0c\u5426\u5219\u53ef\u80fd\u53d1\u751f\u6b7b\u9501\uff0c\u6240\u62a5\u7684\u9519\u8bef\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">fatal error: all goroutines are asleep - deadlock!<\/pre>\n<h3 id=\"\u7b49\u5f85\u534f\u7a0b\u7ec4\u7ed3\u675f\"><a name=\"t7\"><\/a>\u7b49\u5f85\u534f\u7a0b\u7ec4\u7ed3\u675f<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">func main() {\r\n    sayHello := func(wg *sync.WaitGroup, id int) {\r\n        defer wg.Done()\r\n        fmt.Printf(\"%v goroutine start ...\\n\", id)\r\n        time.Sleep(2)\r\n        fmt.Printf(\"%v goroutine exit ...\\n\", id)\r\n    }\r\n\r\n    var wg sync.WaitGroup\r\n    const N = 5\r\n    wg.Add(N)\r\n    for i := 0; i &lt; N; i++ {\r\n        go sayHello(&amp;wg, i)\r\n    }\r\n\r\n    fmt.Println(\"waiting for all goroutine \")\r\n    wg.Wait()\r\n    fmt.Println(\"All goroutines finished!\")\r\n}<\/pre>\n<p>\u8fd0\u884c\u4ee5\u4e0a\u7a0b\u5e8f\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">waiting for all goroutine \r\n1 goroutine start ...\r\n5 goroutine start ...\r\n5 goroutine exit ...\r\n4 goroutine start ...\r\n4 goroutine exit ...\r\n3 goroutine start ...\r\n1 goroutine exit ...\r\n0 goroutine start ...\r\n3 goroutine exit ...\r\n0 goroutine exit ...\r\n2 goroutine start ...\r\n2 goroutine exit ...\r\nAll goroutines finished!<\/pre>\n<p>\u65e0\u8bba\u8fd0\u884c\u591a\u5c11\u6b21\uff0c\u90fd\u80fd\u4fdd\u8bc1All goroutines finished\uff01\u8fd9\u4e00\u53e5\u5728\u6700\u540e\u4e00\u884c\u8f93\u51fa\uff0c\u8fd9\u8bf4\u660e\uff0cWait()\u51fd\u6570\u7b49\u4e86\u6240\u6709\u534f\u7a0b\u90fd\u7ed3\u675f\u81ea\u5df1\u624d\u8fd4\u56de\u3002<\/p>\n<h4 id=\"\u5c0f\u7ed3-1\">\u5c0f\u7ed3<\/h4>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u901a\u8fc7WaitGroup\u63d0\u4f9b\u7684\u4e09\u4e2a\u540c\u6b65\u63a5\u53e3\uff0c\u5b9e\u73b0\u4e86\u7b49\u5f85\u4e00\u4e2a\u534f\u7a0b\u7ec4\u5b8c\u6210\u7684\u540c\u6b65\u64cd\u4f5c\u3002\u5728\u5b9e\u73b0\u65f6\u8981\u6ce8\u610f\uff1a<br \/>\n* Add\u7684\u53c2\u6570N\u5fc5\u987b\u548c\u521b\u5efa\u7684goroutine\u7684\u6570\u91cf\u76f8\u7b49\uff0c\u5426\u5219\u4f1a\u62a5\u51fa\u6b7b\u9501\u7684\u9519\u8bef\u4fe1\u606f<br \/>\n* \u53e6\u5916\uff0csayHello()\u51fd\u6570\u4e2d\u8981\u4f20\u9012WaitGroup\u7684\u6307\u9488\u5462\uff1f\u5728\u8be5\u7ed3\u6784\u7684\u5b9e\u73b0\u6e90\u7801\u4e2d\u5df2\u7ecf\u6709\u8bb2\u89e3\uff1a<\/p>\n<p>A WaitGroup must not be copied after first use.<\/p>\n<pre>\u5c31\u662f\u8bf4\uff0c\u8be5\u7ed3\u6784\u5b9a\u4e49\u540e\u5c31\u4e0d\u80fd\u88ab\u590d\u5236\uff0c\u6240\u4ee5\u8fd9\u91cc\u8981\u4f7f\u7528\u6307\u9488\u3002<\/pre>\n<h2 id=\"\u603b\u7ed3\"><a name=\"t8\"><\/a>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7WaitGroup\u63d0\u4f9b\u7684\u4e09\u4e2a\u51fd\u6570\uff1aAdd,Done,Wait\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u7b49\u5f85\u67d0\u4e2a\u534f\u7a0b\u6216\u534f\u7a0b\u7ec4\u5b8c\u6210\u7684\u540c\u6b65\u64cd\u4f5c\u3002\u4f46\u5728\u4f7f\u7528\u65f6\u8981\u6ce8\u610f:<\/p>\n<ul>\n<li>Add\u7684\u6570\u91cf\u548cDone\u7684\u8c03\u7528\u6570\u91cf\u5fc5\u987b\u76f8\u7b49\u3002<\/li>\n<li>\u53e6\u5916\uff0c\u5c31\u662fWaitGroup\u7ed3\u6784\u4e00\u65e6\u5b9a\u4e49\u5c31\u4e0d\u80fd\u590d\u5236\u7684\u539f\u56e0\u3002<\/li>\n<\/ul>\n<p>WaitGroup\u5728\u9700\u8981\u7b49\u5f85\u591a\u4e2a\u4efb\u52a1\u7ed3\u675f\u518d\u8fd4\u56de\u7684\u4e1a\u52a1\u6765\u8bf4\u8fd8\u662f\u5f88\u6709\u7528\u7684\uff0c\u4f46\u73b0\u5b9e\u4e2d\u7528\u7684\u66f4\u591a\u7684\u53ef\u80fd\u662f\uff0c\u5148\u7b49\u5f85\u4e00\u4e2a\u534f\u7a0b\u7ec4\uff0c\u82e5\u6240\u6709\u534f\u7a0b\u7ec4\u90fd\u6b63\u786e\u5b8c\u6210\uff0c\u5219\u4e00\u76f4\u7b49\u5230\u6240\u6709\u534f\u7a0b\u7ec4\u7ed3\u675f\uff1b\u82e5\u5176\u4e2d\u6709\u4e00\u4e2a\u534f\u7a0b\u53d1\u751f\u9519\u8bef\uff0c\u5219\u544a\u8bc9\u534f\u7a0b\u7ec4\u7684\u5176\u4ed6\u534f\u7a0b\uff0c\u5168\u90e8\u505c\u6b62\u8fd0\u884c\uff08\u672c\u6b21\u4efb\u52a1\u5931\u8d25\uff09\u4ee5\u514d\u6d6a\u8d39\u7cfb\u7edf\u8d44\u6e90\u3002<br \/>\n\u8be5\u573a\u666fWaitGroup\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\uff0c\u90a3\u4e48\u8be5\u573a\u666f\u8be5\u5982\u4f55\u5b9e\u73b0\u5462\uff0c\u5c31\u9700\u8981\u7528\u5230\u901a\u77e5\u673a\u5236\uff0c\u5176\u5b9e\u4e5f\u53ef\u4ee5\u7528channel\u6765\u5b9e\u73b0\uff0c\u5177\u4f53\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u8bf7\u770b\u540e\u7eed\u7684\u6587\u7ae0\u3002<br \/>\n\u8fd9\u6837\u8bf4\u6765\uff0cWaitGroup\u7684\u4f7f\u7528\u573a\u666f\u662f\u6709\u9650\u7684\u3002<\/p>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8ff0 WaitGroup\u7528\u4e8e\u7b49\u5f85\u4e00\u7ec4\u7ebf\u7a0b<\/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-2786","post","type-post","status-publish","format-standard","hentry","tag-06-godocker"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2786","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=2786"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/2786\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=2786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=2786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=2786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}