{"id":256267,"date":"2022-11-17T08:55:14","date_gmt":"2022-11-17T00:55:14","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=256267"},"modified":"2022-10-28T12:56:40","modified_gmt":"2022-10-28T04:56:40","slug":"sync-waitgroup-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sync-waitgroup-linux.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdGo sync WaitGroup\u4f7f\u7528\u6df1\u5165\u7406\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86Go sync WaitGroup\u4f7f\u7528\u6df1\u5165\u7406\u89e3\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u57fa\u672c\u4ecb\u7ecd<\/strong><\/div>\n

WaitGroup\u662fgo\u7528\u6765\u505a\u4efb\u52a1\u7f16\u6392\u7684\u4e00\u4e2a\u5e76\u53d1\u539f\u8bed\uff0c\u5b83\u8981\u89e3\u51b3\u7684\u5c31\u662f\u5e76\u53d1 - \u7b49\u5f85\u7684\u95ee\u9898\uff1a<\/p>\n

\u5f53\u6709\u4e00\u4e2a goroutine A \u5728\u68c0\u67e5\u70b9\uff08checkpoint\uff09\u7b49\u5f85\u4e00\u7ec4 goroutine \u5168\u90e8\u5b8c\u6210\uff0c\u5982\u679c\u8fd9\u4e9b goroutine \u8fd8\u6ca1\u5168\u90e8\u5b8c\u6210\uff0cgoroutine A \u5c31\u4f1a\u963b\u585e\u5728\u68c0\u67e5\u70b9\uff0c\u76f4\u5230\u6240\u6709 goroutine \u90fd\u5b8c\u6210\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c<\/p>\n

\u8bd5\u60f3\u5982\u679c\u6ca1\u6709WaitGroup\uff0c\u60f3\u8981\u5728\u534f\u7a0bA\u7b49\u5230\u5176\u4ed6\u534f\u7a0b\u6267\u884c\u5b8c\u6210\u540e\u80fd\u7acb\u9a6c\u6267\u884c\uff0c\u53ea\u80fd\u4e0d\u65ad\u8f6e\u8be2\u5176\u4ed6\u534f\u7a0b\u662f\u5426\u6267\u884c\u5b8c\u6bd5\uff0c\u8fd9\u6837\u7684\u95ee\u9898\u662f\uff1a<\/p>\n

  • \u53ca\u65f6\u6027\u5dee\uff1a\u8f6e\u8be2\u95f4\u9694\u8d8a\u9ad8\uff0c\u53ca\u65f6\u6027\u8d8a\u5dee<\/li>\n
  • \u65e0\u8c13\u7684\u7a7a\u8f6e\u8bad\uff0c\u6d6a\u8d39\u7cfb\u7edf\u8d44\u6e90<\/li>\n

    \u800c\u7528WaitGroup\u65f6\uff0c\u534f\u7a0bA\u53ea\u7528\u963b\u585e\uff0c\u76f4\u5230\u5176\u4ed6\u534f\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u518d\u901a\u77e5\u534f\u7a0bA,\u5176\u4ed6\u8bed\u8a00\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u5de5\u5177\uff0c\u4f8b\u5982Java\u7684CountDownLatch<\/p>\n

    \u4f7f\u7528<\/strong><\/div>\n

    Waitgroup\u63d0\u4f9b\u4e863\u4e2a\u65b9\u6cd5\uff1a<\/p>\n

    func (wg *WaitGroup) Add(delta int)\r\nfunc (wg *WaitGroup) Done()\r\nfunc (wg *WaitGroup) Wait()<\/pre>\n

    Add\uff1a\u589e\u52a0\u8ba1\u6570\u503c<\/p>\n

    Done\uff1a\u51cf\u5c11\u8ba1\u6570\u503c<\/p>\n

    Wait\uff1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684 goroutine \u4f1a\u4e00\u76f4\u963b\u585e\uff0c\u76f4\u5230 WaitGroup \u7684\u8ba1\u6570\u503c\u53d8\u4e3a 0<\/p>\n

    \u6e90\u7801\u5206\u6790<\/strong><\/div>\n
    type WaitGroup struct {\r\n   \/\/ \u907f\u514d\u590d\u5236\r\n   noCopy noCopy\r\n   \/\/ 64\u4f4d\u73af\u5883\u4e0b\uff0c\u9ad832\u4f4d\u662f\u8ba1\u6570\u503c\uff0c\u4f4e32\u4f4d\u8bb0\u5f55waiter\u7684\u6570\u91cf\r\n   state1 uint64\r\n   \/\/ \u7528\u4e8e\u4fe1\u53f7\u91cf\r\n   state2 uint32\r\n}<\/pre>\n
    Add<\/strong><\/span><\/div>\n
    func (wg *WaitGroup) Add(delta int) {\r\n   \/\/ \u83b7\u53d6\u72b6\u6001\u503c\uff0c\u4fe1\u53f7\u91cf\r\n   statep, semap := wg.state()\r\n   \/\/ \u5c06\u53c2\u6570delta\u5de632\u4f4d\uff0c\u52a0\u5230statep\u4e2d\uff0c\u5373\u7ed9\u8ba1\u6570\u503c\u52a0\u4e0adelta\r\n   state := atomic.AddUint64(statep, uint64(delta)<<32)\r\n   \/\/ \u52a0\u540e\u7684\u8ba1\u6570\u503c\r\n   v := int32(state >> 32)\r\n   \/\/ waiter\u7684\u6570\u91cf\r\n   w := uint32(state)\r\n   \/\/ \u52a0\u540e\u4e0d\u80fd\u662f\u8d1f\u503c\r\n   if v < 0 {\r\n      panic( \"sync: negative WaitGroup counter\" )\r\n   }\r\n   \/\/ \u6709waiter\u7684\u60c5\u51b5\u4e0b\uff0c\u5f53\u524d\u534f\u7a0b\u53c8\u52a0\u4e86\u8ba1\u6570\u503c\uff0cpanic\r\n   \/\/ \u5373\u6709waiter\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u80fd\u518d\u7ed9waitgroup\u589e\u52a0\u8ba1\u6570\u503c\u4e86\r\n   if w != 0 && delta > 0 && v == int32(delta) {\r\n      panic( \"sync: WaitGroup misuse: Add called concurrently with Wait\" )\r\n   }\r\n   \/\/ \u5982\u679c\u52a0\u5b8c\u540ev\u5927\u4e8e0\uff0c\u6216\u8005\u52a0\u5b8c\u540ev\u7b49\u4e8e0\uff0c\u4f46\u6ca1\u6709\u7b49\u5f85\u8005\uff0c\u76f4\u63a5\u8fd4\u56de\r\n   if v > 0 || w == 0 {\r\n      return\r\n   }\r\n   \/\/ \u63a5\u4e0b\u6765\u5c31\u662fv\u7b49\u4e8e0\uff0c\u4e14w\u5927\u4e8e0\u7684\u60c5\u51b5\r\n   \/\/ \u518d\u6b21\u68c0\u67e5\u662f\u5426\u6709Add\u548cWait\u5e76\u53d1\u8c03\u7528\u7684\u60c5\u51b5\r\n   if *statep != state {\r\n      panic( \"sync: WaitGroup misuse: Add called concurrently with Wait\" )\r\n   }\r\n   \/\/ \u5c06\u8ba1\u6570\u503c\u548cwaiter\u6570\u91cf\u6e050\r\n   *statep = 0\r\n   \/\/ \u5524\u9192\u6240\u6709\u7684waiter\r\n   for ; w != 0; w-- {\r\n      runtime_Semrelease(semap, false, 0)\r\n   }\r\n}<\/pre>\n
  • \u56e0\u4e3astate\u9ad832\u4f4d\u4fdd\u5b58\u8ba1\u6570\u503c\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u53c2\u6570delta\u5de6\u79fb32\u4f4d\u540e\u52a0\u5230state\u4e0a\u624d\u6b63\u786e<\/li>\n

    \u5982\u679c\u52a0\u5b8c\u540ev\u5927\u4e8e0\uff0c\u6216\u8005\u52a0\u5b8c\u540ev\u7b49\u4e8e0\uff0c\u4f46\u6ca1\u6709\u7b49\u5f85\u8005\uff0c\u76f4\u63a5\u8fd4\u56de<\/p>\n

  • v\u5927\u4e8e0\uff1a\u8868\u793a\u81ea\u5df1\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u8c03\u7528Done\u7684\u534f\u7a0b\uff0c\u4e0d\u7528\u81ea\u5df1\u6765\u91ca\u653ewaiter\uff0c\u76f4\u63a5\u8fd4\u56de<\/li>\n
  • v\u7b49\u4e8e0\uff0c\u4f46\u6ca1\u6709\u7b49\u5f85\u8005\uff1a\u56e0\u4e3a\u6ca1\u6709\u7b49\u5f85\u8005\uff0c\u4e5f\u5c31\u4e0d\u7528\u91ca\u653e\u7b49\u5f85\u8005\uff0c\u4e5f\u76f4\u63a5\u8fd4\u56de<\/li>\n

    \u5426\u5219\u5c31\u662fv\u7b49\u4e8e0\uff0c\u4e14w\u5927\u4e8e0\u7684\u60c5\u51b5\uff1a<\/p>\n

    \u81ea\u5df1\u662f\u6700\u540e\u4e00\u4e2a\u8c03\u7528Done\u7684\uff0c\u4e14\u8fd8\u6709\u7b49\u5f85\u8005\uff0c\u90a3\u5c31\u5524\u9192\u6240\u6709\u7b49\u5f85\u8005<\/p>\n

    Done<\/strong><\/span><\/div>\n

    Done\u5185\u90e8\u8c03\u7528Add\uff0c\u53ea\u662f\u53c2\u6570\u4f20-1\uff0c\u8868\u793a\u51cf\u5c11\u8ba1\u6570\u503c<\/p>\n

    func (wg *WaitGroup) Done() {\r\n   wg.Add(-1)\r\n}<\/pre>\n
    Wait<\/strong><\/span><\/div>\n
    func (wg *WaitGroup) Wait() {\r\n   statep, semap := wg.state()\r\n   for {\r\n      state := atomic.LoadUint64(statep)\r\n      \/\/ v\uff1a\u8ba1\u6570\u503c\r\n      v := int32(state >> 32)\r\n      w := uint32(state)\r\n      \/\/ \u5982\u679c\u8ba1\u6570\u503c\u4e3a0\uff0c\u81ea\u5df1\u4e0d\u9700\u8981\u7b49\u5230\uff0c\u76f4\u63a5\u8fd4\u56de\r\n      if v == 0 {\r\n         return\r\n   }\r\n      \/\/ \u589e\u52a0waiter\u8ba1\u6570\u503c\r\n if atomic.CompareAndSwapUint64(statep, state, state+1) {\r\n         \/\/ \u81ea\u5df1\u5728\u4fe1\u53f7\u91cf\u4e0a\u963b\u585e\r\n         runtime_Semacquire(semap)\r\n         \/\/ \u68c0\u67e5Waitgroup\u662f\u5426\u5728wait\u8fd4\u56de\u524d\u88ab\u91cd\u7528\r\n         if *statep != 0 {\r\n            panic( \"sync: WaitGroup is reused before previous Wait has returned\" )\r\n         }\r\n         return\r\n      }\r\n   }\r\n}<\/pre>\n

    \u5982\u679c\u8ba1\u6570\u503c\u4e3a0\uff0c\u5f53\u524d\u4e0d\u9700\u8981\u963b\u585e\uff0c\u76f4\u63a5\u8fd4\u56de<\/p>\n

    \u5426\u5219\u5c06waiter\u6570\u91cf\u52a01\uff0c\u5982\u679c\u6dfb\u52a0\u6210\u529f\uff0c\u5c31\u628a\u81ea\u5df1\u963b\u585e\u5230\u4fe1\u53f7\u91cf\u4e0a<\/p>\n

    \u88ab\u5524\u9192\u65f6\uff0c\u5982\u679cstatep\u4e0d\u4e3a0\uff0c\u8868\u793a\u8be5waitgroup\u662f\u5426\u5728wait\u8fd4\u56de\u524d\u88ab\u91cd\u7528\u4e86\uff0cpanic<\/p>\n

    \u6ce8\u610f\u4e8b\u9879<\/strong><\/div>\n

    \u901a\u8fc7\u6e90\u7801\u5206\u6790\u53ef\u4ee5\u770b\u51fa\uff0cWaitgroup\u6709\u4ee5\u4e0b\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n

    \u8ba1\u6570\u5668\u7684\u503c\u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e0\uff1a<\/p>\n

    \u4e00\u5f00\u59cb\u8c03\u7528Add\u65f6\uff0c\u4e0d\u80fd\u4f20\u8d1f\u6570<\/p>\n

    \u8c03\u7528Done\u7684\u6b21\u6570\u4e0d\u80fd\u8fc7\u591a\uff0c\u5bfc\u81f4\u8d85\u8fc7\u4e86 WaitGroup \u7684\u8ba1\u6570\u503c<\/p>\n

    \u56e0\u6b64\u4f7f\u7528 WaitGroup \u7684\u6b63\u786e\u59ff\u52bf\u662f\uff0c\u9884\u5148\u786e\u5b9a\u597d WaitGroup \u7684\u8ba1\u6570\u503c\uff0c\u7136\u540e\u8c03\u7528\u76f8\u540c\u6b21\u6570\u7684 Done \u5b8c\u6210\u76f8\u5e94\u7684\u4efb\u52a1<\/p>\n

    \u8981\u4fdd\u8bc1\u5728\u671f\u671b\u7684Add\u8c03\u7528\u5b8c\u6210\u540e\uff0c\u518d\u8c03\u7528Wait\uff0c\u5426\u5219Wait\u53d1\u73b0\u8ba1\u6570\u503c\u4e3a0\u65f6\u4e0d\u4f1a\u963b\u585e<\/p>\n

    \u6700\u597d\u5728\u4e00\u4e2a\u534f\u7a0b\u4e2d\uff0c\u6309\u987a\u5e8f\u5148\u8c03Add\uff0c\u518d\u8c03Wait<\/p>\n

    \u9700\u8981\u91cd\u7528\u65f6\uff0c\u9700\u8981\u5728\u524d\u4e00\u7ec4\u8c03\u7528Wait\u7ed3\u675f\u540e\uff0c\u518d\u5f00\u59cb\u65b0\u4e00\u8f6e\u7684\u4f7f\u7528<\/p>\n

    WaitGroup \u662f\u53ef\u4ee5\u91cd\u7528\u7684\u3002\u53ea\u8981 WaitGroup \u7684\u8ba1\u503c\u6062\u590d\u5230\u96f6\u503c\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u5b83\u5c31\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u65b0\u521b\u5efa\u7684 WaitGroup\uff0c\u88ab\u91cd\u590d\u4f7f\u7528\uff0c\u800c\u4e0d\u80fd\u5728\u524d\u4e00\u7ec4\u6ca1\u4f7f\u7528\u5b8c\u7684\u60c5\u51b5\u4e0b\u53c8\u4f7f\u7528<\/p>\n","protected":false},"excerpt":{"rendered":"

    WaitGroup\u662fgo\u7528\u6765\u505a\u4efb\u52a1\u7f16\u6392\u7684\u4e00\u4e2a\u5e76\u53d1\u539f\u8bed\uff0c\u5b83\u8981\u89e3\u51b3\u7684\u5c31\u662f\u5e76\u53d1 – \u7b49\u5f85\u7684\u95ee\u9898\uff1a \u5f53\u6709\u4e00\u4e2a goro […]<\/p>\n","protected":false},"author":1482,"featured_media":220401,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256267","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=256267"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256267\/revisions"}],"predecessor-version":[{"id":256272,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256267\/revisions\/256272"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/220401"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=256267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=256267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=256267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}