How to use the sync.WaitGroup to execute all the goroutine?

huangapple go评论121阅读模式
英文:

How to use the sync.WaitGroup to execute all the goroutine?

问题

目前我正在将所有的值推送到通道中,并从中读取并对其进行平方处理。
我想避免使用time.Sleep(2000 * time.Millisecond),因为它会阻塞程序执行2秒钟,而我希望每个goroutine都能处理并等待其执行,然后退出程序。我对golang有点生疏,所以现在问这个基本问题:(。有人可以帮我吗?

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )
  7. func doSquare(num int) int {
  8. return num * num
  9. }
  10. var wg sync.WaitGroup
  11. func main() {
  12. wg.Add(1)
  13. st := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  14. quit := make(chan bool)
  15. ch := make(chan int)
  16. go func() {
  17. for i := range st {
  18. ch <- i
  19. }
  20. }()
  21. go func() {
  22. for {
  23. select {
  24. case x := <-ch:
  25. fmt.Println(doSquare(x))
  26. // return
  27. case <-quit:
  28. wg.Done()
  29. default:
  30. // fmt.Println("---")
  31. // do something
  32. }
  33. }
  34. }()
  35. quit <- true
  36. wg.Wait()
  37. time.Sleep(2000 * time.Millisecond)
  38. }

请注意,我只翻译了代码部分,其他内容不包括在内。

英文:

Currently I am pushing all the values to the channel and reading from it and doing the square of it.
I want to avoid using time.Sleep(2000 * time.Millisecond) since it's blocking the execution for 2 seconds instead I want every goroutine to process and wait for its execution and then exit the program. I just got out of touch with golang so asking this basic question now :(. Can anyone help me with this?

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sync&quot;
  5. &quot;time&quot;
  6. )
  7. func doSquare(num int) int {
  8. return num * num
  9. }
  10. var wg sync.WaitGroup
  11. func main() {
  12. wg.Add(1)
  13. st := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  14. quit := make(chan bool)
  15. ch := make(chan int)
  16. go func() {
  17. for i := range st {
  18. ch &lt;- i
  19. }
  20. }()
  21. go func() {
  22. for {
  23. select {
  24. case x := &lt;-ch:
  25. fmt.Println(doSquare(x))
  26. // return
  27. case &lt;-quit:
  28. wg.Done()
  29. default:
  30. // fmt.Println(&quot;---&quot;)
  31. // do something
  32. }
  33. }
  34. }()
  35. quit &lt;- true
  36. wg.Wait()
  37. time.Sleep(2000 * time.Millisecond)
  38. }

答案1

得分: 1

只需将quit <- true移动到第一个goroutine的末尾。

  1. func main() {
  2. wg.Add(1)
  3. st := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  4. quit := make(chan bool)
  5. ch := make(chan int)
  6. go func() {
  7. for i := range st {
  8. ch <- i
  9. }
  10. quit <- true
  11. }()
  12. go func() {
  13. for {
  14. select {
  15. case x := <-ch:
  16. fmt.Println(doSquare(x))
  17. // return
  18. case <-quit:
  19. wg.Done()
  20. return
  21. default:
  22. // fmt.Println("---")
  23. // do something
  24. }
  25. }
  26. }()
  27. wg.Wait()
  28. }

这是另一种通过close(ch)来表示没有更多数据的方法。

  1. func main() {
  2. st := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  3. quit := make(chan bool)
  4. ch := make(chan int) // improve concurrency by `ch := make(chan int, 100)`
  5. go func() {
  6. for i := range st {
  7. ch <- i
  8. }
  9. close(ch)
  10. }()
  11. go func() {
  12. for x := range ch {
  13. fmt.Println(doSquare(x))
  14. }
  15. quit <- true
  16. }()
  17. <-quit
  18. }
英文:

Just move quit &lt;- true to the end of the first goroutine

  1. func main() {
  2. wg.Add(1)
  3. st := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  4. quit := make(chan bool)
  5. ch := make(chan int)
  6. go func() {
  7. for i := range st {
  8. ch &lt;- i
  9. }
  10. quit &lt;- true
  11. }()
  12. go func() {
  13. for {
  14. select {
  15. case x := &lt;-ch:
  16. fmt.Println(doSquare(x))
  17. // return
  18. case &lt;-quit:
  19. wg.Done()
  20. return
  21. default:
  22. // fmt.Println(&quot;---&quot;)
  23. // do something
  24. }
  25. }
  26. }()
  27. wg.Wait()
  28. }

This is another way to signal there is no more data by close(ch)

  1. func main() {
  2. st := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  3. quit := make(chan bool)
  4. ch := make(chan int) // improve concurrency by `ch := make(chan int, 100)`
  5. go func() {
  6. for i := range st {
  7. ch &lt;- i
  8. }
  9. close(ch)
  10. }()
  11. go func() {
  12. for x := range ch {
  13. fmt.Println(doSquare(x))
  14. }
  15. quit &lt;- true
  16. }()
  17. &lt;-quit
  18. }

huangapple
  • 本文由 发表于 2023年4月27日 14:37:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76117415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定