Goroutine与Mutex的同步

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

Goroutine synchronization with Mutex

问题

  1. var n int32 = 0
  2. var mutex *sync.Mutex = new(sync.Mutex)
  3. for i := 0; i < 100000; i++ {
  4. go func() {
  5. mutex.Lock()
  6. defer mutex.Unlock()
  7. atomic.AddInt32(&n, 1)
  8. }()
  9. }
  10. fmt.Println(n)

我想知道为什么 n 的结果不是 100000,看起来互斥锁没有起作用。

英文:
  1. var n int32 = 0
  2. var mutex *sync.Mutex = new(sync.Mutex)
  3. for i := 0; i &lt; 100000; i++ {
  4. go func() {
  5. mutex.Lock()
  6. defer mutex.Unlock()
  7. atomic.AddInt32(&amp;n, 1)
  8. }()
  9. }
  10. fmt.Println(n)

I'm wondering why the result of n is not 100000, looks like the mutex lock does not work.

答案1

得分: 2

fmt.Println(n)在go线程完成之前执行,你需要在fmt.Println(n)之前添加time.Sleep(5 * time.Millisecond),如下所示:

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "sync/atomic"
  6. "time"
  7. )
  8. func main() {
  9. var n int32 = 0
  10. var mutex *sync.Mutex = new(sync.Mutex)
  11. for i := 0; i < 100000; i++ {
  12. go func() {
  13. mutex.Lock()
  14. defer mutex.Unlock()
  15. atomic.AddInt32(&n, 1)
  16. }()
  17. }
  18. time.Sleep(5 * time.Millisecond)
  19. fmt.Println(n)
  20. }

输出:

100000

或者

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "sync/atomic"
  6. )
  7. func main() {
  8. var n int32 = 0
  9. var mutex *sync.Mutex = new(sync.Mutex)
  10. var wg sync.WaitGroup
  11. for i := 0; i < 100000; i++ {
  12. wg.Add(1)
  13. go func() {
  14. mutex.Lock()
  15. defer mutex.Unlock()
  16. atomic.AddInt32(&n, 1)
  17. wg.Done()
  18. }()
  19. }
  20. wg.Wait()
  21. fmt.Println(n)
  22. }
  23. 输出
  24. --------
  25. 100000
英文:

fmt.Println(n) is executing before the go threads completion,You need to add the time.Sleep(5 * time.Millisecond) before fmt.Println(n) like below

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sync&quot;
  5. &quot;sync/atomic&quot;
  6. &quot;time&quot;
  7. )
  8. func main() {
  9. var n int32 = 0
  10. var mutex *sync.Mutex = new(sync.Mutex)
  11. for i := 0; i &lt; 100000; i++ {
  12. go func() {
  13. mutex.Lock()
  14. defer mutex.Unlock()
  15. atomic.AddInt32(&amp;n, 1)
  16. }()
  17. }
  18. time.Sleep(5 * time.Millisecond)
  19. fmt.Println(n)
  20. }
  21. Output :
  22. --------
  23. 100000

Or

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sync&quot;
  5. &quot;sync/atomic&quot;
  6. )
  7. func main() {
  8. var n int32 = 0
  9. var mutex *sync.Mutex = new(sync.Mutex)
  10. var wg sync.WaitGroup
  11. for i := 0; i &lt; 100000; i++ {
  12. wg.Add(1)
  13. go func() {
  14. mutex.Lock()
  15. defer mutex.Unlock()
  16. atomic.AddInt32(&amp;n, 1)
  17. wg.Done()
  18. }()
  19. }
  20. wg.Wait()
  21. fmt.Println(n)
  22. }
  23. Output :
  24. --------
  25. 100000

huangapple
  • 本文由 发表于 2022年9月22日 12:40:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/73809431.html
匿名

发表评论

匿名网友

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

确定