map并发读写问题

来源:4-6 map为什么需要扩容?

Gama

2023-06-16

老师好,在读 mapassign 部分源码的时候有几个疑问,函数检测了 h.flags&hashWriting确定此时没有concurrent map writes之后,计算了key的hash值,然后直接 h.flags ^= hashWriting把写标志置为1,这中间没看到任何加锁操作:

  1. 这里不会有竞态问题吗?
  2. 如果不能防止出现并发写,那前面检查 h.flags&hashWriting是否就没意义了?反正也防不住?
    同样的在 mapaccess 中也没有看到锁,谢谢老师。
	if h.flags&hashWriting != 0 {
		fatal("concurrent map writes")
	}
	hash := t.hasher(key, uintptr(h.hash0))

	// Set hashWriting after calling t.hasher, since t.hasher may panic,
	// in which case we have not actually done a write.
	h.flags ^= hashWriting
写回答

1回答

Moody

2023-06-16

所以map是并发不安全的呀

0
0

深入Go底层原理,重写Redis中间件实战

深入Go语言原理+Go重写Redis

474 学习 · 172 问题

查看课程