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 4333484
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:23:59+00:00 2026-05-21T10:23:59+00:00

I have a container (C++) on which I need to operate in two ways,

  • 0

I have a container (C++) on which I need to operate in two ways, from different threads: 1) Add and remove elements, and 2) iterate through its members. Clearly, remove element while iteration is happening = disaster. The code looks something like this:

class A
{
public:
   ...
   void AddItem(const T& item, int index) { /*Put item into my_stuff at index*/ }
   void RemoveItem(const T& item) { /*Take item out of m_stuff*/ }
   const list<T>& MyStuff() { return my_stuff; } //*Hate* this, but see class C
private:
   Mutex mutex; //Goes in the *Item methods, but is largely worthless in MyStuff()
   list<T> my_stuff; //Just as well a vector or deque
};
extern A a; //defined in the .cpp file

class B
{
   ...
   void SomeFunction() { ... a.RemoveItem(item); }
};

class C
{
   ...
   void IterateOverStuff()
   {
      const list<T>& my_stuff(a.MyStuff());
      for (list<T>::const_iterator it=my_stuff.begin(); it!=my_stuff.end(); ++it)
      {
          ...
      }
   }
};

Again, B::SomeFunction() and C::IterateOverStuff() are getting called asynchronously. What’s a data structure I can use to ensure that during the iteration, my_stuff is ‘protected’ from add or remove operations?

  • 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-05-21T10:24:00+00:00Added an answer on May 21, 2026 at 10:24 am

    sounds like a reader/writer lock is needed. Basically, the idea is that you may have 1 or more readers OR a single writer. Never can you have a read and write lock at the same time.

    EDIT: An example of usage which I think fits your design involves making a small change. Add an “iterate” function to the class which owns the list and make it templated so you can pass a function/functor to define what to do for each node. Something like this (quick and dirty pseudo code, but you get the point…):

    class A {
    public:
        ...
        void AddItem(const T& item, int index) {
            rwlock.lock_write();
            // add the item
            rwlock.unlock_write();
        }
    
        void RemoveItem(const T& item) {
            rwlock.lock_write();
            // remove the item
            rwlock.unlock_write();
        }
    
        template <class P>
        void iterate_list(P pred) {
            rwlock.lock_read();
            std::for_each(my_stuff.begin(), my_stuff.end(), pred);
            rwlock.unlock_read();
        }
    
    private:
        rwlock_t rwlock;
        list<T> my_stuff; //Just as well a vector or deque
    };
    
    
    extern A a; //defined in the .cpp file
    
    class B {
        ...
        void SomeFunction() { ... a.RemoveItem(item); }
    };
    
    class C {
        ...
    
        void read_node(const T &element) { ... }
    
        void IterateOverStuff() {
            a.iterate_list(boost::bind(&C::read_node, this));
       }
    };
    

    Another Option would be to make the reader/writer lock publicly accessible and have the caller responsible for correctly using the lock. But that’s more error prone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one std::list<> container and these threads: One writer thread which adds elements
I have a container element which I need to resize as its contents change.
i have a log file which contains hundreds/thousands of seperate XML messages and need
I have a PDF file, which contains data that we need to import into
I have an Asp.Net repeater, which contains a textbox and a checkbox. I need
I have a container of custom controls each of which have 2 controls in
I have a function which searches an STL container then returns the iterator when
I have UserControl which contains a TextBox and a Button control. The Button opens
I have a string which contain tags in the form < tag > .
I have a table which contains my server status create table ServerStatus ( ServerId

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.