Facing concurrency issues with Go sync maps

时间:2017-12-18 08:04:23

标签: go concurrency hashmap synchronization

I am having problems with Go's *** Settings *** Force Tags req-42 Default Tags owner-john smoke *** Variables *** ${HOST} 10.0.1.42 *** Test Cases *** No own tags [Documentation] This test has tags owner-john, smoke and req-42. No Operation With own tags [Documentation] This test has tags not_ready, owner-mrx and req-42. [Tags] owner-mrx not_ready No Operation . Below are the details:

I created a global sync map like:

sync.Map

and on an event I populate this map with the intended structure as var MySyncGlobalMap = sync.Map{} . So basically I want to populate the sync map with key as map[int64]map[string]interface{} and value as another sync map of structure int64. Below is how I populate the map:

map[string]interface{}

Now there will be multiple threads which will be accessing this map and do some operations. There will be constant updates to the inner sync map. Once the processing is done on the key of the inner sync map, that key will be deleted from that map.

I will know that a job is complete once the inner sync map becomes empty.

Now since there are multiple threads accessing this map I receiving a panic:

Fatal error: concurrent read and write

I am still wondering that even after using sync map I am facing this issue.

Can any one point out what am I doing wrong?

1 个答案:

答案 0 :(得分:1)

我弄清楚代码的问题是什么。我使用sync.Map作为Value类型而不是指针。

所以,我正在制作底层互斥锁的副本。在读/写操作中,锁定在副本而不是原始互斥锁上。

更改地图以使用指针解决了问题。