Go channel与锁
不同 goroutine 之间如何通讯?有两种方案:
1. 全局变量的互斥锁
2. 使用管道 channel 来解决
因为没有对全局变量 m 加锁,因此会出现资源争夺问题,代码会出现错误,提示 concurrent map writes
var (
myMap = make(map[int]int, 10)
lock sync.Mutex
)
func test(n int) {
res := 1
for i := 1; i <= n; i++ {
…
近期评论