🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 线程同步 ``` 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) } ``` ### 读写锁 场景:读多写少