STL slist/map containers concurrent/thread-safe access

John Love-Jensen
Fri Sep 15 11:43:00 GMT 2006

Hi Kalaky,

#1.  Please do not cross post to GCC-help and GCC lists.

#2. is not an appropriate forum for your question.

#3. is not an appropriate forum for your question.

#4.  Your question is a general C++ question, not a GCC question.

> Given that removing/inserting elements from a map/slist/whatever does
> not invalidate iterators to list elements, it is safe to use the
> element that the iterator "points" to ?

The rules for iterator invalidation are tightly dependent on the container
involved.  I recommend reading the ISO 14882 standard very carefully about
what STL containers guarantee about their iterators.  (I always have to look
them up myself -- I tend to pessimistically over-assume that my iterators
are invalidated more often then they really are in actuality.)

I'm pretty sure that removing an element from a container will cause any
iterator pointing to that element to become invalidated, for all containers.

How do you have an iterator to an element in a container, when that element
hasn't been inserted into the container yet?

*AFTER* an element is inserted into a container, and then you acquire an
iterator to that element, I presume that iterator properly refers to that

I recommend you use a "Safe STL" for your development.  It will help you
discover problematic STL usage in your code.

Here are some "safe STL" implementations:

Good luck!

More information about the Gcc-help mailing list