First, let’s reiterate that unsafe multithreaded access to C++ standard library containers is undefined behavior. Simultaneous read operations are safe for multithreading, but simultaneous writes or a read and write occurring simultaneously are unsafe for multithreading.
Additionally, do not use operator[]
on std::(unordered)map. It is the most convenient syntax, but is not often what you actually want.
The std::map subscript operator is a convenience, but a potentially dangerous one
The operations for reading and writing single elements for C++ standard library maps
A simplified overview of ways to add or update elements in a std::map