I have an std::map < std::string, std::string > which is having values added to it at irregular intervals from one thread (but frequently and needs to be very fast), and occasionally having groups of entries removed.
I need from a different thread to dump a snapshot of the map as text to a debug log on command from a user.
Clearly it’s not thread safe to just iterate through the map outputting the debug information while it could be updated so I’m currently taking a read lock (mutex) before dumping the data and a write lock for every insert or delete. This works fine, but I can’t really lock the map for this long, it delays the processing of incoming updates too much.
I don’t believe I can lock and unlock the debug dump thread for each item as modifying the map from the other thread can invalidate the iterator I believe.
Is there any way I can do this safely without having to take out a read lock on the whole data structure while I write it out so that new values can still be inserted quickly? I realise I won’t be able to get a guarenteed consistent view of the data if values can be added and removed while I’m iterating though it, but as long as it’s safe that’s understood.
If there is no way to use a map for this, can anyone suggest any other data structure I could use?
edit: I’m hoping for a solution that means I don’t need to take out an expensive lock when adding an item.
There are 2 solutions I can see at this moment: