企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
> ### 读写锁 ```go package main import ( "time" "sync" "fmt" ) var number int //同时操作时只能一个G写或多个G读) var rwm sync.RWMutex func main() { go func() { for i := 0; i < 3; i++ { rwm.Lock() number++ fmt.Println("G1", number) time.Sleep(time.Second) rwm.Unlock() } }() go func() { for i := 0; i < 3; i++ { rwm.RLock() fmt.Println("G2", number) time.Sleep(time.Second) rwm.RUnlock() } }() go func() { for i := 0; i < 3; i++ { rwm.RLock() fmt.Println("G3", number) time.Sleep(time.Second) rwm.RUnlock() } }() time.Sleep(time.Second * 10) } ``` > ### 相关阅读 * [锁及锁粒度的详细比喻](https://www.cnblogs.com/nancymake/p/6252536.html)