【英文】Go语言的锁

Introduction

Notes on learning locks in Go language

Mutex Lock

  • When one goroutine locks some code and has not yet unlocked it, another goroutine trying to execute the locked code will be blocked
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import "sync"

var lock sync.Mutex

func task() {
lock.Lock()

...

lock.Unlock()
}

func main() {
for i := 0; i < 3; i++ {
go task()
}
}

Conclusion

References

Bilibili - Guo Hongzhi