Iterate over STL container using indices safe way to avoid using locks?
问题 Wondering if it's safe to iterate over a STL container such as a vector in the following manner to avoid locking on reads/writes but allowing for push_back() operations only by any "writing" threads. for (size_t i = 0; i < vec.size(); i++) { const T& t = *vec[i]; // do something with t } I understand that iterators can be invalidated by changes to the container but perhaps if we make sure the initial container size is large enough for any future additions, it should also be safe to iterate