I am using an stl map, linking an int key to objects of a class I defined, Account. The program involves alot of multithreading. Some of the threads send account objects by reference to functions that make changes in these objects. My question is what happens if one thread just sent an acoount to a function, and then another thread inserts an element to the map? Does the map move the objects around when sorting? If so, then what other stl should I use instead? I know I can solve this problem with locks, but I want to allow maximal parallelism. Thanks for your answers!
Asked
Active
Viewed 106 times
1 Answers
2
Adding an element to a map does not invalidate any pointers, references, or iterators to elements already in the map. However, while you are inserting an element in one thread, you CANNOT search for an element in the map in a different thread because the insertion can rearrange the links between elements already in the map.

1201ProgramAlarm
- 32,384
- 7
- 42
- 56
-
Thank you for your answer! Do you know another container that would allow searching in one thread while inserting in another? – Avivos Dec 12 '15 at 17:41
-
@AvivBurshtein The container needs to be built from the ground up to support that. The STL doesn't have a concurrent_map, but see [this question](http://stackoverflow.com/questions/820526/c-thread-safe-map) for a discussion and suggestions. – 1201ProgramAlarm Dec 13 '15 at 02:41
-
@1201ProgramAlarm Are you sure already existing objects never get moved around? I need to be able to rely on this definately. Can you point me to any literature on the issue? – mo FEAR Apr 24 '22 at 20:55