mercredi 10 juillet 2019

Altering a value in map inside the iteration of the same map(Nested iterator) C++

I am facing some strange issue in the code below which is compiled in linux,

The below piece of code iterates a map "ObjectsMap" from Begin and inside the iteration loop, I am trying to change the value of a different key in the same map "ObjectsMap".

map<int, ObjectA>::iterator outerItr = reDt->ObjectsMap->begin();
for (; outerItr!= reDt->ObjectsMap->end(); outerItr++)
{
  int parentid = outerItr->second.parentID;
  map<int, ObjectA>::iterator innerItr = reDt->ObjectsMap->find(parentid);
  if (innerItr != reDt->ObjectsMap->end())
  {
     innerItr->second.selfTime = innerItr->second.selfTime + outerItr->second.execTime;
  }
}

While this code executes, I am getting incorrect/unexpected value in the selfTime. But if I add some log inside the if() block then strangely the value is correct. If the log is removed then again I get incorrect value in selfTime.

What is that strange thing here.? Is it because of data race behavior?

Aucun commentaire:

Enregistrer un commentaire