Medium
One mutex guarding an entire hash table
Two threads call writeKey() concurrently with completely different keys ("apple" and "zebra") that hash to different buckets in the underlying hash table. Given a single mutex protecting the whole table, are these two calls able to proceed concurrently?
main.cpp
std::mutex mtx; // one lock for the entire table
std::unordered_map<std::string, int> table;
void writeKey(const std::string& k, int v) {
std::lock_guard<std::mutex> lock(mtx);
table[k] = v;
}
// Thread 1: writeKey("apple", 1);
// Thread 2: writeKey("zebra", 2); // different bucket, no real conflict38 of 51 in Concurrency