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

  • Home
  • SEARCH
  • 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 563213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:38:10+00:00 2026-05-13T12:38:10+00:00

I have std::list<Info> infoList in my application that is shared between two threads. These

  • 0

I have std::list<Info> infoList in my application that is shared between two threads. These 2 threads are accessing this list as follows:

Thread 1: uses push_back(), pop_front() or clear() on the list (Depending on the situation)
Thread 2: uses an iterator to iterate through the items in the list and do some actions.

Thread 2 is iterating the list like the following:

for(std::list<Info>::iterator i = infoList.begin(); i != infoList.end(); ++i)
{
  DoAction(i);
}

The code is compiled using GCC 4.4.2.

Sometimes ++i causes a segfault and crashes the application. The error is caused in std_list.h line 143 at the following line:

_M_node = _M_node->_M_next;

I guess this is a racing condition. The list might have changed or even cleared by thread 1 while thread 2 was iterating it.

I used Mutex to synchronize access to this list and all went ok during my initial test. But the system just freezes under stress test making this solution totally unacceptable. This application is a real-time application and i need to find a solution so both threads can run as fast as possible without hurting the total applications throughput.

My question is this:
Thread 1 and Thread 2 need to execute as fast as possible since this is a real-time application. what can i do to prevent this problem and still maintain the application performance? Are there any lock-free algorithms available for such a problem?

Its ok if i miss some newly added Info objects in thread 2’s iteration but what can i do to prevent the iterator from becoming a dangling pointer?

Thanks

  • 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-13T12:38:11+00:00Added an answer on May 13, 2026 at 12:38 pm

    In general it is not safe to use STL containers this way. You will have to implement specific method to make your code thread safe. The solution you chose depends on your needs. I would probably solve this by maintaining two lists, one in each thread. And communicating the changes through a lock free queue (mentioned in the comments to this question). You could also limit the lifetime of your Info objects by wrapping them in boost::shared_ptr e.g.

    typedef boost::shared_ptr<Info> InfoReference; 
    typedef std::list<InfoReference> InfoList;
    
    enum CommandValue
    {
        Insert,
        Delete
    }
    
    struct Command
    {
        CommandValue operation;
        InfoReference reference;
    }
    
    typedef LockFreeQueue<Command> CommandQueue;
    
    class Thread1
    {
        Thread1(CommandQueue queue) : m_commands(queue) {}
        void run()
        {
            while (!finished)
            {
                //Process Items and use 
                // deleteInfo() or addInfo()
            };
    
        }
    
        void deleteInfo(InfoReference reference)
        {
            Command command;
            command.operation = Delete;
            command.reference = reference;
            m_commands.produce(command);
        }
    
        void addInfo(InfoReference reference)
        {
            Command command;
            command.operation = Insert;
            command.reference = reference;
            m_commands.produce(command);
        }
    }
    
    private:
        CommandQueue& m_commands;
        InfoList m_infoList;
    }   
    
    class Thread2
    {
        Thread2(CommandQueue queue) : m_commands(queue) {}
    
        void run()
        {
            while(!finished)
            {
                processQueue();
                processList();
            }   
        }
    
        void processQueue()
        {
            Command command;
            while (m_commands.consume(command))
            {
                switch(command.operation)
                {
                    case Insert:
                        m_infoList.push_back(command.reference);
                        break;
                    case Delete:
                        m_infoList.remove(command.reference);
                        break;
                }
            }
        }
    
        void processList()
        {
            // Iterate over m_infoList
        }
    
    private:
        CommandQueue& m_commands;
        InfoList m_infoList;
    }   
    
    
    void main()
    {
    CommandQueue commands;
    
    Thread1 thread1(commands);
    Thread2 thread2(commands);
    
    thread1.start();
    thread2.start();
    
    waitforTermination();
    
    }
    

    This has not been compiled. You still need to make sure that access to your Info objects is thread safe.

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

Sidebar

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.