Semantics of STL containers (std::map) in a multithreaded scenario
I understand the C++ standard does not talk about threading. My
question here is directed more towards what happens when a STL
container is used in a certain way. I'd appreciate any thoughts. I
re-iterate I don't want to probe into what C++ standard says when I
trample some data from multiple threads, I simply want to know if I
have understood this right.
I have a "std::map<somedatatype, someotherdatatype> myMap", where stuff
gets inserted regularly. In a multi-threaded environment, there are
other parts of code that simultaneously read from this map.
I would like to know if I have understood this right. Consider a
scenario like this:
// thread 1 in one part of the application
busy_inserting_stuff_into_myMap
// while thread 2 in another part of the application
1. gets a reference to the myMap
2. starts iterating by performing an ordinary loop:
std::map<>::iterator itrbegin = myMap.begin();
std::map<>::iterator itrend = myMap.end();
std::map<>::iterator itr;
for (itr = itrbegin; itr != itr.end(); ++itr)
{
}
Since thread 1 is regularly inserting stuff, is there a chance thread 2
finds all its iterators invalidated while its in the process of
looping? In other words what is the effect of inserting into a map
from one thread while simultaneously looping through the same map in
another thread? I don't care if thread 2 misses some info during the
looping (since the map gets updated all the time...)
thanks!