Go语言原生支持的Lock(sync.Mutex)不是可重入的,同个协程重复获得锁会panic1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package sync
import (
"sync"
"testing"
)
func TestLock(t *testing.T) {
lock := sync.Mutex{}
lock.Lock()
t.Log("get lock 1")
lock.Lock()
t.Log("get lock 2")
lock.Unlock()
}
1 | === RUN TestLock |
如何实现类似Java语言提供的ReentrantLock,支持可重入呢?
more >>