I'm wondering if it's allowed to concurrently get values for some keys and in the same time inserting new key-value pairs to the STL map in Visual C++ 2010, if I guarantee that each thread accesses/inserts a different key.
No, it is not allowed. Lookup involves traversal of internal data structures (for std::map common approach for internal representation is binary search tree like Red–black tree). On the other hand insert modifies internal structures.
If simultaneous lookups and inserts would be thread-safe, then each access, even in single-thread environment, would involve high syncronization cost for operations, which contradicts C++ princple - "you do pay for what you don't use".
Thread Safety in MSVC 2010:
If a single object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected. For example, given an object A, if thread 1 is writing to A, then thread 2 must be prevented from reading from or writing to A.
That said, if you already have reference to element before insert operations in other thread - it would be safe to access element via that reference, because object is not moved during internal rebalancing.
ISO C++11 23.2.4/9:
The insert and emplace members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.
MSVC 2012 (but not MSVC 2010) has concurrency::concurrent_unordered_map
associative container (note it is unordered which makes it similar to std::unordered_map
, i.e. it requires hash and equality for keys rather than strict weak ordering):
The concurrent_unordered_map
class is a concurrency-safe container that controls a varying-length sequence of elements of type std::pair<const _Key_type, _Element_type>
. The sequence is represented in a way that enables concurrency-safe append, element access, iterator access, and iterator traversal operations.
Intel TBB library has similar container - tbb::concurrent_unordered_map
:
Template class for associative container that supports concurrent insertion and traversal.