Why does unsafe multithreaded std:unordered_map crash more than std:map?

When a customer switched from using std::map to std::unordered_map in their code, they noticed that the rate of crashes increased significantly. Upon investigation, they discovered that the issue stemmed from unsafe multithreaded access to the collection. While it is important to note that unsafe multithreaded access to C++ standard library containers is undefined behavior, the customer was curious about why std::unordered_map seemed to demonstrate the problem more prominently. The reason lies in the implementation differences between std::map and std::unordered_map. The former is typically implemented as a red-black tree, while the latter is implemented as a hash table with separate chaining. Concurrent modifications to the hash table during rehashing can lead to a higher likelihood of crashing. In summary, std::map has a consistently low risk of crashing, while std::unordered_map has a generally low risk but a higher risk during rehashing. It is important to address the unsafe concurrent access rather than solely relying on the choice of data structure.

https://devblogs.microsoft.com/oldnewthing/20231103-00/?p=108966

To top