Adding data to a hashmap in parallel

℡╲_俬逩灬. 提交于 2019-12-25 02:28:45

问题


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 ?


回答1:


use concurrent_unordered_map instead of unordered_map in concurrent_unordered_map.h



来源:https://stackoverflow.com/questions/10064372/adding-data-to-a-hashmap-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!