ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 线程同步 ``` import ("sync") var mu sync.Mutex // 互斥锁, var mu sync.RWMutex 读写锁 ``` ### 互斥锁 ``` package main import ( "fmt" "math/rand" "sync" "time" ) var lock sync.Mutex func main() { m := map[string]int{"a":1, "b":2} for i := 0; i <= 3; i ++ { go func(a map[string]int) { lock.Lock() a["a"] = rand.Int() lock.Unlock() }(m) time.Sleep(time.Second) } lock.Lock() fmt.Println(m) lock.Unlock() time.Sleep(5 * time.Second) } ``` ### 读写锁 场景:读多写少