我本以为Go中的mutex
会锁定数据,并且不允许其他任何goroutine
进行读/写,除非拳头goroutine
释放了锁定。看来我的理解是错误的。阻止从其他goroutine
读取/写入的唯一方法是也调用其他lock
中的goroutines
。这样可以确保critical section
只能由一个goroutine
进行访问。
因此,我希望this代码出现死锁:
package main
import(
"fmt"
"sync"
)
type myMap struct {
m map[string]string
mutex sync.Mutex
}
func main() {
done := make(chan bool)
ch := make(chan bool)
myM := &myMap{
m: make(map[string]string),}
go func() {
myM.mutex.Lock()
myM.m["x"] = "i"
fmt.Println("Locked. Won't release the Lock")
ch <- true
}()
go func() {
<- ch
fmt.Println("Trying to write to the myMap")
myM.m["a"] = "b"
fmt.Println(myM)
done <- true
}()
<- done
}
由于拳头goroutine
锁定了该结构,因此我希望第二个goroutine
无法对该结构进行读/写操作,但这在这里不会发生。
如果我要在第二mux.Lock()
中添加goroutine
,则会出现死锁。
我发现mutex
在Go中的工作方式有点怪异。如果我锁定了,那么Go不应允许其他任何goroutine
对其进行读写。
有人可以向我解释Go语言中的互斥锁概念吗?