I've got an instance of an std::unordered_map serving as a database of some sort. (It's basically just a regular hashmap).
I have 4 threads retrieving long strings from a network (I'm using the c++11 std::threads) and within each of those threads, i tokenize the strings they receive. The tokenization itself is paralellized as well using the new Microsoft AMP library (so the process of tokenization is done by the GPU threads).
I would like each of the 4 "main" threads to add the tokens as keys into the hashmap as the GPU threads finish tokenizing the strings (and then repeat the process).
How would i go about synchronizing all of this so that i can safely add new keys into my database from each of the threads ?
std::mutexat the same scope as yourstd::unordered_map<>and have each thread lock the mutex before touching the hash table. Honestly, though, if you're concerned about throughput, you'd probably be better off with a proper concurrent container rather than a manually synchronized one.