Avivos
Avivos

Reputation: 11

How does c++ stl map manage memory, and how to get around it

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!

Upvotes: 0

Views: 139

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32727

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.

Upvotes: 2

Related Questions