V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
frankphper
V2EX  ›  Go 编程语言

Go 语言为什么不支持并发读写 map?

  •  
  •   frankphper · 105 天前 · 2010 次点击
    这是一个创建于 105 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家好,我是 frank ,「 Golang 语言开发栈」公众号作者。

    01 介绍

    在 Go 语言项目开发中,我们经常会使用哈希表 map,它的时间复杂度是 O(1),Go 语言中的 map 使用开放寻址法避免哈希碰撞。

    Go 语言中的 map 并非原子操作,不支持并发读写操作。

    Go 官方认为 map 在大多数情况下是使用 map 进行并发读操作,仅在少数情况下是使用 map 进行并发读写操作。

    如果 Go 语言中的 map 原生支持并发读写操作,在操作时需要先获取互斥锁,反而会降低只有并发读操作时的性能。

    在需要并发读写操作 map 时,可以结合 sync 包中的互斥锁一起使用。

    02 并发读写 map

    Go 支持并发读 map,不支持并发读写 map

    示例代码:

    func main() {
    	var m = make(map[int]string)
    
    	go func() {
    		for {
    			m[1] = "xx"
    		}
    	}()
    
    	go func() {
    		for {
    			_ = m[1]
    		}
    	}()
    	time.Sleep(time.Second * 3)
    }
    

    输出结果:

    fatal error: concurrent map read and map write
    // ...
    

    阅读上面这段代码,我们并发读写 map 类型的变量 m,在运行时,返回致命错误 fatal error: concurrent map read and map write

    Go 语言中的 map 在运行时是怎么检测到 map 的存在写操作?

    源码:

    const (
    	// flags
    	iterator     = 1 // there may be an iterator using buckets
    	oldIterator  = 2 // there may be an iterator using oldbuckets
    	hashWriting  = 4 // a goroutine is writing to the map
    	sameSizeGrow = 8 // the current map growth is to a new map of the same size
    )
    // A header for a Go map.
    type hmap struct {
    	count     int // # live cells == size of map.  Must be first (used by len() builtin)
    	flags     uint8
    	B         uint8  // log_2 of # of buckets (can hold up to loadFactor * 2^B items)
    	noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details
    	hash0     uint32 // hash seed
    
    	buckets    unsafe.Pointer // array of 2^B Buckets. may be nil if count==0.
    	oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing
    	nevacuate  uintptr        // progress counter for evacuation (buckets less than this have been evacuated)
    
    	extra *mapextra // optional fields
    }
    
    // Like mapaccess, but allocates a slot for the key if it is not present in the map.
    func mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer {
    	// ...
    
    done:
    	if h.flags&hashWriting == 0 {
    		fatal("concurrent map writes")
    	}
    	h.flags &^= hashWriting
    	if t.IndirectElem() {
    		elem = *((*unsafe.Pointer)(elem))
    	}
    	return elem
    }
    

    阅读上面这段源码,我们可以发现在 hmap 结构体中的字段 flags,该字段用于标记 map 是否为写入状态。

    在访问 map 时,通过判断 hmap.flagshashWriting 的值,可知是否有其它 goroutine 访问 map,如果有,则返回致命错误 fatal("concurrent map writes")

    03 总结

    本文介绍 Go 语言为什么不支持并发读写 map,Go 官方的说法是在多数情况下 map 只存在并发读操作,如果原生支持并发读写,即降低了并发读操作的性能。

    通过阅读源码,我们了解到在运行时检测是否存在对 map 的写操作,如果存在,则返回致命错误。

    读者朋友们在使用 map 时,要特别注意是否存在对 map 的并发写操作,如果存在,要结合 sync 包的互斥锁一起使用。

    3 条回复    2024-01-14 19:37:51 +08:00
    kneo
        1
    kneo  
       105 天前 via Android
    1.9 就有 sync.Map 了,不提是因为……不知道?
    不过至少排除 AI 创作了。AI 肯定会提一下的。
    lstz
        2
    lstz  
       105 天前 via iPhone
    review
    julyclyde
        3
    julyclyde  
       105 天前
    不懂
    是无法在未获互斥锁的情况下写入吗?还是写了可能坏?

    如果是后者,我觉得不算“不支持”
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3215 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 12:16 · PVG 20:16 · LAX 05:16 · JFK 08:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.