Are std::map and std::vector thread safe? Are std::map and std::vector thread safe? multithreading multithreading

Are std::map and std::vector thread safe?


In case of read only map/vector there is no need to use mutexes.This was already answered for both vector and map
While C++03 doesn't mention threads, C++11 has clause covering you question.

23.2.2 Container data races [container.requirements.dataraces]

1 For purposes of avoiding data races (17.6.5.9), implementations shall consider the following functions to beconst: begin, end, rbegin, rend, front, back, data, find, lower_bound, upper_bound, equal_range, atand, except in associative or unordered associative containers, operator[].
2 Notwithstanding (17.6.5.9), implementations are required to avoid data races when the contents of the containedobject in different elements in the same sequence, excepting vector<bool>, are modified concurrently.
3 [ Note: For a vector<int> x with a size greater than one, x[1] = 5 and *x.begin() = 10 can be executedconcurrently without a data race, but x[0] = 5 and *x.begin() = 10 executed concurrently may result ina data race. As an exception to the general rule, for a vector < bool > y, y[0] = true may race with y[1]= true. —end note ]

Thus in C++11 it is allowed not only to read objects , but also allow concurrent modification of its different objects(but not container!), with exception for vector < bool >