0

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 ?

2
  • Have a std::mutex at the same scope as your std::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. Commented Apr 8, 2012 at 17:52
  • It's probably more efficient to have one hashmap per thread and then merge them pairwise concurrently as you finish up. Commented Apr 8, 2012 at 17:57

1 Answer 1

2

use concurrent_unordered_map instead of unordered_map in concurrent_unordered_map.h

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.