golang value併發安全的另外一種玩法,就是使用atomic.Value,看一段代碼。golang
package main import ( "sync" "sync/atomic" "time" ) func main() { var m atomic.Value type Map map[string]string m.Store(make(Map)) var mu sync.Mutex read := func(key string) (val string) { m1 := m.Load().(Map) return m1[key] } insert := func(key, val string) { mu.Lock() defer mu.Unlock() m1 := m.Load().(Map) m2 := make(Map) for k, v := range m1 { m2[k] = v } m2[key] = val m.Store(m2) } go func() { for { insert("k", "v") time.Sleep(100 * time.Millisecond) } }() go func() { for { read("k") } }() time.Sleep(10 * time.Second) }
相對於讀寫鎖,少了一些鎖的爭搶,不過相對的,帶來了一些,內存上的開銷,適用於讀多寫少而且變量佔用內存不是特別大的狀況,若是用內存存儲大量數據,這個並不適合,技術上主要是常見的寫時複製(copy-on-write)。安全
另外這個還比較適合程序配置的存儲,貼一段官方的栗子架構
var config Value // holds current server configuration // Create initial config value and store into config. config.Store(loadConfig()) go func() { // Reload config every 10 seconds // and update config value with the new version. for { time.Sleep(10 * time.Second) config.Store(loadConfig()) } }() // Create worker goroutines that handle incoming requests // using the latest config value. for i := 0; i < 10; i++ { go func() { for r := range requests() { c := config.Load() // Handle request r using config c. _, _ = r, c } }() }
恩,挺好玩的。併發
更多架構、PHP、GO相關踩坑實踐技巧請關注個人公衆號:PHP架構師atom