I have a map<uint, Order*> orders where Order is a defined class with applicable fields such as id, time, price and volume. I also have a thread which listens for incoming order additions and deletions for the map defined below.
void System::OrderDeleted_Thread(unsigned int order_id)
{
if(orders.find(order_id) != orders.end())
{
Order* order = orders[order_id];
orders.erase(order_id);
delete order;
}
}
My problem is very similar to this one:
Segmentation fault in std function std::_Rb_tree_rebalance_for_erase ()
My question is, how can I iterate through my orders map without the program giving me an error when it comes time to re balance the tree? Just like the solution in the link says, I have taken out the .erase(uint) method and gotten it to work. Unfortunately, I cannot keep a map of several tens of thousands keys around.
Thanks in advance!
You need to synchronize access to the map. STL containers are not thread-safe with multiple writers (and erasing elements is writing to the container) without some sort of external synchronization.