I am using a boost::unordered_map<const std::string, std::list<TypeA> >
in a performance critical multi-threaded environment. I understand that writing to STL containers isn't thread safe and the same goes for boost::unordered_map
.
boost::unordered_map<const std::string, std::list<TypeA> > myMap;
// Added some elements to myMap
Now, if I want to add or delete an element of type A to the list as , is it necessary is I just lock the entire map as opposed to locking the list that is being modified so that other threads can read/write the rest of the key value pairs?
// Assuming there are pair with the keys "Apple" and "Orange" in myMap
A a, b;
myMap["Orange"].push_back(a) //Add an element to the list
myMap["Apple"].remove(b); //Remove an element
What if the list is replaced by another STL container?
Thanks.