There is a specific mention in the C++0x FDIS (n3290) for this.
§ 17.6.5.9 Data race avoidance
The whole paragraph is of interest but more particularly:
3/ A C++ standard library function shall not directly or indirectly modify objects (1.10) accessible by threads other than the current thread unless the objects are accessed directly or indirectly via the function’s non-const arguments, including this.
means that you may call cbegin
and cend
on std::vector<T>
safely. As well as calling operator==
or operator<
on std::string
.
6/ Operations on iterators obtained by calling a standard library container or string member function may access the underlying container, but shall not modify it.
means that merely iterating over a container should not modify the said container in any way.
Despite 3/ though, there seems to be room for global objects though, as iterators modifying some kind of shared register object in which they would associate themselves with the container (STL debug features). I don't make sense of:
7/ Implementations may share their own internal objects between threads if the objects are not visible to users and are protected against data races.
otherwise.
Anyway, the Standard guarantees that iterating over the vector
will be safe... but makes no guarantees when it comes to actually reading the objects (those are your own). In this case, this is covered because std::string
is covered above.
EDIT: As David Hammen justly noted, this Standard is not yet fully implemented. Many compilers already provided the guarantees above, even though the previous Standard never spoke about threads. MSVC, gcc, clang, icc, comeau, etc... All big names should already provide this guarantee, as can be seen from Als' answer.