sync包相关
sync.Once - 函数只执行一下 demo package main import ( "fmt" "sync" ) func main() { var once sync.Once onceBody := func() { fmt.Println("Only once") } done := make(chan ...
sync.Once - 函数只执行一下 demo package main import ( "fmt" "sync" ) func main() { var once sync.Once onceBody := func() { fmt.Println("Only once") } done := make(chan ...
sync.Mutex Go 的 sync.Mutex 底层主要由 state + semaphore 两个字段组成。 type Mutex struct { state int32 // 状态 sema uint32 // 信号量 } state位: waiter_num:等待goroutine starving:是否处于饥饿状态 woken:是否有...
sync.Map 是高性能线程安全 map,核心思想是: 读写分离 数据结构: type Map struct { mu sync.Mutex // 写锁,保护写操作 read atomic.Value // 读专用 map,类型为 readOnly dirty map[interface{}]*entry // 写专用 map...