{"id":207868,"date":"2025-07-08T11:10:31","date_gmt":"2025-07-08T03:10:31","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207868\/"},"modified":"2025-07-08T11:10:31","modified_gmt":"2025-07-08T03:10:31","slug":"%e5%a6%82%e4%bd%95%e7%ad%89%e5%be%85%e7%bc%93%e5%86%b2%e9%80%9a%e9%81%93%ef%bc%88%e4%bf%a1%e5%8f%b7%e9%87%8f%ef%bc%89%e4%b8%ba%e7%a9%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207868\/","title":{"rendered":"\u5982\u4f55\u7b49\u5f85\u7f13\u51b2\u901a\u9053\uff08\u4fe1\u53f7\u91cf\uff09\u4e3a\u7a7a\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>\u5982\u4f55\u7b49\u5f85\u7f13\u51b2\u901a\u9053\uff08\u4fe1\u53f7\u91cf\uff09\u4e3a\u7a7a\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 10:54:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7b49\u5f85\u7f13\u51b2\u901a\u9053\uff08\u4fe1\u53f7\u91cf\uff09\u4e3a\u7a7a\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u6574\u6570\u5207\u7247\uff0c\u5b83\u4eec\u662f\u540c\u65f6\u64cd\u4f5c\u7684\uff1a<\/p>\n<pre>ints := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}<\/pre>\n<p>\u6211\u4f7f\u7528\u7f13\u51b2\u901a\u9053\u4f5c\u4e3a\u4fe1\u53f7\u91cf\uff0c\u4ee5\u83b7\u5f97\u540c\u65f6\u8fd0\u884c\u7684 go \u4f8b\u7a0b\u7684\u4e0a\u9650\uff1a<\/p>\n<pre>sem := make(chan struct{}, 2)\n\nfor _, i := range ints {\n  \/\/ acquire semaphore\n  sem &lt;- struct{}{}\n\n  \/\/ start long running go routine\n  go func(id int, sem chan struct{}) {\n    \/\/ do something\n\n    \/\/ release semaphore\n    &lt;- sem\n  }(i, sem)\n}<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5728\u5230\u8fbe\u6700\u540e\u4e00\u4e2a\u6216\u6700\u540e\u4e24\u4e2a\u6574\u6570\u4e4b\u524d\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u5728\u6700\u540e\u7684 go \u4f8b\u7a0b\u5b8c\u6210\u4e4b\u524d\u7ed3\u675f\u3002<\/p>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u5982\u4f55\u7b49\u5f85\u7f13\u51b2\u901a\u9053\u8017\u5c3d\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u4e0d\u80fd\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u7528\u4fe1\u53f7\u91cf\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a\u901a\u9053\uff09\u3002\u5f53\u60a8\u5904\u7406\u503c\u548c\u8c03\u5ea6\u66f4\u591a goroutine \u65f6\uff0c\u65e0\u6cd5\u4fdd\u8bc1\u5b83\u4e0d\u4f1a\u4e3a\u7a7a\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e0d\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u56e0\u4e3a\u60a8\u662f\u540c\u6b65\u5206\u6d3e\u5de5\u4f5c\uff0c\u4f46\u56e0\u4e3a\u6ca1\u6709\u65e0\u7ade\u4e89\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u901a\u9053\u7684\u957f\u5ea6\uff0c\u6240\u4ee5\u6ca1\u6709\u57fa\u5143\u6765\u7b49\u5f85\u901a\u9053\u7684\u957f\u5ea6\u8fbe\u5230 0\u3002<\/p>\n<p>\u4f7f\u7528 <code>sync.waitgroup<\/code> \u7b49\u5f85\u6240\u6709 goroutine \u5b8c\u6210<\/p>\n<pre>sem := make(chan struct{}, 2)\n\nvar wg sync.waitgroup\n\nfor _, i := range ints {\n    wg.add(1)\n    \/\/ acquire semaphore\n    sem &lt;- struct{}{}\n    \/\/ start long running go routine\n    go func(id int) {\n        defer wg.done()\n        \/\/ do something\n        \/\/ release semaphore\n        &lt;-sem\n    }(i)\n}\n\nwg.wait()<\/pre>\n<p>\u4f7f\u7528\u201c\u5de5\u4f5c\u6c60\u201d\u6765\u5904\u7406\u60a8\u7684\u6570\u636e\u3002\u5b83\u6bd4\u4e3a <strong>each<\/strong> int \u8fd0\u884c goroutine\u3001\u4e3a\u5176\u4e2d\u7684\u53d8\u91cf\u5206\u914d\u5185\u5b58\u7b49\u7b49\u66f4\u4fbf\u5b9c&#8230;&#8230;<\/p>\n<pre>ints := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}\n\nch := make(chan int)\n\nvar wg sync.WaitGroup\n\n\/\/ run worker pool\nfor i := 2; i &gt; 0; i-- {\n    wg.Add(1)\n\n    go func() {\n        defer wg.Done()\n\n        for id := range ch {\n            \/\/ do something\n            fmt.Println(id)\n        }\n    }()\n}\n\n\/\/ send ints to workers\nfor _, i := range ints {\n    ch &lt;- i\n}\n\nclose(ch)\n\nwg.Wait()<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u7b49\u5f85\u7f13\u51b2\u901a\u9053\uff08\u4fe1\u53f7\u91cf\uff09\u4e3a\u7a7a\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\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-207868","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207868","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=207868"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207868\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}