I have a question regarding thread safety, as shown below (I have only two streams in which one of the streams is read only from the card, the other streams will write and read as shown):
//Thread 2: the reading and writing thread unordered_map<int, unordered_map<classA*>*>testMap; //need lock because writing to the map? testMap[1] = new unordered_map<int, classA*>; //do not need lock because only reading and the other thread is only reading? unordered_map<classA*>* ptr = testMap[1]; //need lock because writing? (*ptr)[1] = new classA; //do not need lock because only reading and the other thread is only reading? classA* ptr2 = (*ptr)[1]; //din't modify the map, but modify the data pointed by the pointer stored by the map, do I need lock? ptr2->field1 = 5; ptr2->field2 = 6; //end of reading and writing thread
What is the correct way to block unordered_map? Also, should I use a single lock or multiple locks?
Thanks.
source share