jjzjj

MapSnapshot

全部标签

go - 映射变量快照的并发读/写

我遇到了一个我无法理解的情况。在我的代码中,我使用的函数需要读取map(但不写入,只循环遍历该map中现有数据的快照)。这是我的代码:typeMyStructstruct{*sync.RWMutexMyMapmap[int]MyDatas}varMapVar=MyStruct{&sync.RWMutex{},make(map[int]MyDatas)}funcMyFunc(){MapVar.Lock()MapSnapshot:=MapVar.MyMapMapVar.Unlock()for_,a:=rangeMapSnapshot{//Mapconcurrentwrite/readocc