3 ms·
> So unordered_map(Robin-hood): Nothing. No erases, no insertions allowed during the foreach loop at all. Yes, but this applies to any hashtable that might reh
by spacechild1 5y ago
> So unordered_map(Robin-hood): Nothing. No erases, no insertions allowed during the foreach loop at all.
Yes, but this applies to any hashtable that might rehash on inseration (= most). In practice this is not a big problem. For example, you can just put the new elements / to be deleted keys on a std::vector and only insert/erase after the for-loop.
I don't see how this makes iteration itself problematic. You can still iterate over a std::unordered_map perfectly fine. Just don't erase/insert elements while iterating.
> I guess you could just have the entire for-each loop inside of a lock, but that seems a bit inefficient?
Yes, you would have to lock the whole for-loop. It's the same with any other data structures where the iterators can get invalidated on inseration/deletion, e.g. std::vector.
Even for containers where iterators are not invalidated, you have to be careful with fain-grained locks. For example, you can only increment the iterator while holding the lock!