如何使Goroutine在相同函数中不并发执行。

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

how to make Goroutine not Concurrency if Same Function

问题

有没有办法让goroutine按顺序执行(一个接一个)如果它们是相同的函数?

我一开始并不打算使用goroutine。然而,在TCP中使用"os/exec"函数会导致TCP强制停止。因此,我使用goroutine来避免崩溃。但是我仍然希望它们按顺序执行,而不是并发执行。这是我的代码。

  1. func handleTCP(conn net.Conn) {
  2. defer conn.Close()
  3. fmt.Println("handle TCP function")
  4. for {
  5. wg := new(sync.WaitGroup)
  6. wg.Add(1)
  7. go func() {
  8. cmdArgs := []string{temp_str, test_press, gh, "sample.csv"}
  9. cmd := exec.Command("calib.exe", cmdArgs...)
  10. wg.Done()
  11. }()
  12. }
  13. }
英文:

Is there any way to make goroutine execute one after another ( one by one) if it was the same function?

I didn't mean to use goroutine firstly. However, "os/exec" function in TCP will cause the tcp forced to stop. Thus I use goroutine to avoid crashing. But I still want them to execute by order, instead of concurrently. Here is my code.

  1. func handleTCP(conn net.Conn) {
  2. defer conn.Close()
  3. fmt.Println("handle TCP function")
  4. for {
  5. wg := new(sync.WaitGroup)
  6. wg.Add(1)
  7. go func() {
  8. cmdArgs := []string{temp_str, test_press, gh, "sample.csv"}
  9. cmd := exec.Command("calib.exe", cmdArgs...)
  10. wg.Done()
  11. }()
  12. }
  13. }

答案1

得分: 0

尝试将锁放入函数中,这样可以使它们的执行顺序变为串行。但要记住,wg.Done()必须在函数的第一行的defer下面。

类似这样的代码:

  1. var mu sync.Mutex
  2. func handleTCP(conn net.Conn) {
  3. defer conn.Close()
  4. fmt.Println("handle TCP function")
  5. for {
  6. wg := new(sync.WaitGroup)
  7. wg.Add(1)
  8. go func() {
  9. defer wg.Done()
  10. mu.Lock()
  11. defer mu.Unlock()
  12. cmdArgs := []string{temp_str, test_press, gh, "sample.csv"}
  13. cmd := exec.Command("calib.exe", cmdArgs...)
  14. // 执行命令
  15. }()
  16. }
  17. }
英文:

Try to put the lock into function, it makes theirs execution sequential. But remember about wg.Done() must be under defer at first line of the function.
Something like this:

  1. var mu sync.Mutex
  2. func handleTCP(conn net.Conn) {
  3. defer conn.Close()
  4. fmt.Println("handle TCP function")
  5. for {
  6. wg := new(sync.WaitGroup)
  7. wg.Add(1)
  8. go func() {
  9. defer wg.Done()
  10. mu.Lock()
  11. defer mu.UnLock()
  12. cmdArgs := []string{temp_str, test_press, gh, "sample.csv"}
  13. cmd := exec.Command("calib.exe", cmdArgs...)
  14. }()
  15. }
  16. }

答案2

得分: 0

你可以使用锁(locks)来限制对资源的访问,使其一次只能由一个线程访问,如下所示:

  1. i := 1
  2. iLock := &sync.Mutex{}
  3. for {
  4. go func() {
  5. iLock.Lock()
  6. fmt.Printf("i的值:%d\n", i)
  7. iLock.Unlock()
  8. }()
  9. }

更多示例请参考这里

不确定你使用WaitGroup是为了什么目的。如果是为了实现你想要的顺序执行,那么可以将其移除。

英文:

You can use locks to limit access to a resource to one thread at a time as follows

  1. i := 1
  2. iLock = &sync.Mutex{}
  3. for {
  4. go func() {
  5. iLock.Lock()
  6. fmt.Printf("Value of i: %d\n", i)
  7. iLock.Unlock()
  8. }
  9. }

More examples here

Not sure what you are using the WaitGroup for. If it is to achieve the sequentiality you desire, then it can be removed.

huangapple
  • 本文由 发表于 2022年4月21日 10:41:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/71948310.html
匿名

发表评论

匿名网友

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

确定