golang value并发安全的另外一种玩法

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

相关文章
相关标签/搜索