1

I have a thread (Thread A) which continuously iterates over a map say MapA.

Now there is an other thread (ThreadB) which inserts elements into the MapA.

I dont delete elements from MapA

Will there be any concurrency issue by this operation?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
sri
  • 165
  • 1
  • 7
  • i am not concerned about the invalid iterator since i loop continuously if i reach end of map i reset iterator to begining. – sri Jan 16 '13 at 13:42
  • You should be concerned. When an iterator is invalid, you will probably never be able to test it against end of map because operators `++` or `--` will do unpredictable things. – Benoit Jan 16 '13 at 13:47

1 Answers1

4

There is a distinction between thread safety / concurrency issues and invalidating iterators when inserting new items.

The STL is inherently not thread-safe, so be sure to mutex-lock when doing anything else than just reading from an STL object from multiple threads.

However, inserting into a std::map does not invalidate existing iterators (see Does insertion to STL map invalidate other existing iterator?)

Community
  • 1
  • 1
sonicwave
  • 5,952
  • 2
  • 33
  • 49