Let’s say that in the main thread of a C++11 program I build an std::set, fill it with items and extract an iterator it from it. After that, from another thread, I start modifying the set in such a way that elements can only be added to it but not erased.
Is the validity of it assured also while the set is being modified, or should I consider it invalid while the set is being modified by insertion operations from the other thread?
From section 23.2.1
[container.requirements.general]:For associative containers such as
std::set, section 23.2.4 ([associative.reqmts]) says:So your iterators will remain valid after inserting additional items.
However, thread safety is a different topic completely.
Section 17.6.5.9 (
[res.on.data.races]) provides thatSince that results in reading the container while it’s being updated, it is not necessarily safe to use a
std::setiterator while inserting into the collection from another thread. Your implementation may provide a stronger guarantee.