map并发读写问题
来源:4-6 map为什么需要扩容?

Gama
2023-06-16
老师好,在读 mapassign 部分源码的时候有几个疑问,函数检测了 h.flags&hashWriting
确定此时没有concurrent map writes之后,计算了key的hash值,然后直接 h.flags ^= hashWriting
把写标志置为1,这中间没看到任何加锁操作:
- 这里不会有竞态问题吗?
- 如果不能防止出现并发写,那前面检查
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是并发不安全的呀
00
相似问题