dimanche 3 septembre 2017

Can I create an unordered_map of string and atomic

I would like to create an unordered_map of <string, atomic<int>>. I would use this to increment (fetch_add), store or load the values of the atomic based on a string (the key). For example, say I have 10 atomic integer counters, but I want to only get the values of 4 or them. I would like a unordered_map that looked like this:

unordered_map<string, atomic<int>> myValues = {
    {"first", atomic<int>(0)},
    {"second", atomic<int>(0)}, ... the last key being "tenth"}
};

Then say I had a vector of strings like

vector<string> wanted = {"first", "third", "tenth"};

The I would like to do the following:

for (auto want: wanted) {
    cout <<  myValues[want].load() << endl;

This should print out the values for the keys in wanted.

Can I do this? If I try to create a map as above, I get the error message that the assignment operator for atomic is deleted? Is there a way to do this?

Thanks

Aucun commentaire:

Enregistrer un commentaire