What operations are thread-safe on std::map? What operations are thread-safe on std::map? multithreading multithreading

What operations are thread-safe on std::map?


C++11 requires that all member functions declared as const are thread-safe for multiple readers.

Calling myMap["xyz"] is not thread-safe, as std::map::operator[] isn't declared as const.Calling myMap.at("xyz") is thread-safe though, as std::map::at is declared as const.


In theory no STL containers are threadsafe. In practice reading is safe if the container is not being concurrently modified. ie the standard makes no specifications about threads. The next version of the standard will and IIUC it will then guarantee safe readonly behaviour.

If you are really concerned, use a sorted array with binary search.


At least in Microsoft's implementation, reading from containers is thread-safe (reference).

However, std::map::operator[] can modify data and is not declared const. You should instead use std::map::find, which is const, to get a const_iterator and dereference it.