removal -> iterator invalidation – STL, ConcurrentMap, fail fast …

This is a blog post tying up a few blog posts on this subject. It’s instructive to compare the different iterators in different contexts in the face of a tricky removal operation. points out that ConcurrentModEx can occur even in single-threaded myList.remove(..). Note this is not using myIterator.remove(void)

ConcurrentHashMap never throws this same CMEx. See Details? not available yet.

Many jdk5 concurrent collections have thread safe iterators. My book [[java generics]] covers them in some detail.

As seen in, all STL graph containers can cope with removals, but contiguous (non-graph) containers can get iterators invalidated. Java arrayList improves on it by allowing iterator to perform thread-safe remove. I guess this is possible because the iterator thread simplify skips the dead node. Any other iterator is invalidated by CMEx. I guess the previous nodes can shift up.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s