Changing values in container
std::map table;
for (auto item : table) {
item.second = 2;
}
The code above will not change any value in container ‘table’. ‘auto’ will become std::pair
The correct way is:
for (auto &tem : table) {
item.second = 2;
}
or:
for (std::map::iterator it = table.begin(); it != table.end(); ++it) {
it->second = 2;
}
Do traversal and modification concurrently in a container
Using concurrent_hash_map like this:
typedef tbb::concurrent_hash_map CacheMap;
CacheMap cache;
....
// Thread 1
for (auto &item : cache) {
cout << item.second << "\n";
}
// Thread 2
CacheMap::accessor ac;
cache.insert(ac, std::make_pair("hello", 123));
....
will cause the program to coredump.
The reason is that concurrent_hash_map can't be modified and traversed concurrently.
Actually, Intel figure out another solution to concurrently-traverse-and-insert: concurrent_unordered_map.
But still be careful, concurrent_unordered_map support simultaneous traversal and insertion, but not simultaneous traversal and erasure.