Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8349037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:52:19+00:00 2026-06-09T07:52:19+00:00

I have an std::map < std::string, std::string > which is having values added to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:52:20+00:00Added an answer on June 9, 2026 at 7:52 am

    There are 2 solutions I can see at this moment:

    1. (Easy, but might still take too long): copy the map (or assign to another container) while locked and then dump the local copy to the debug log while not locked
    2. (Some more work): delegate the updates of the map to another thread via a queue. If the other thread is the one that dumps to the debug log, then you don’t need the locks anymore. This way the fast threads are only locked while accessing the queue.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this map which compiles fine in MSVC10 : std::map<std::string, std::ofstream> m_logFiles; But
I have a std::map<std::string, HANDLE> SampleMap which stores HANDLE objects as value. After the
I have a class like the one below: class Foo { private: std::map<std::string, Bar*>
I have a std::map< std::string, std::string> which initialized with some API call. When I'm
I Have a object which the following field : boost::unordered_map<std::string, std::shared_ptr<Foo> > m_liste_; I
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
I have a std::map<std::string, myClass*> myMap then I am inserting like follow: if(!myKey.empty()) {
In my current setup, I have a typedef std::function<void (MyClass&, std::vector<std::string>) MyFunction; std::map<std::string, MyFunction>
I have the declaration (or similar) std::map< std::string, Stock*> &stocks; throughout my code. Eclipse
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.