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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:35:40+00:00 2026-06-07T00:35:40+00:00

I have a list where one thread just does push_back and other thread occasionally

  • 0
  1. I have a list where one thread just does push_back and other thread occasionally loop through the list and prints all the elements. Do I need a lock in this case?
  2. I have pointers to the elements in some other object. Is it safe to have? I know that vector will move all the objects when it needs more space so pointers will be invalidated.

    mylist.push_back(MyObj(1));
    if(someCond)
    {
    _myLastObj = &mylist.back();
    }

_myLastObj is of type MyObj*

If I had used a vector, the object would have been moved to a different location and the pointer would be pointing to garbage. Is it safe with a list?

  • 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-07T00:35:44+00:00Added an answer on June 7, 2026 at 12:35 am
    1. yes, you need a lock (some form of synchronization).
    2. pointers to elements of a std::list are invalidated only if that same element is removed from the list. Since your code never removes anything from the list, your pointers are safe.

    For a concrete reason why you need the lock, consider for example that list::push_back is permitted to do the following, in this order:

    1. allocate a new node,
    2. set the “next” pointer of the tail of the list to the new node,
    3. set the “next” pointer of the new node to null (or some other end-of-list-marker),
    4. set the “previous” pointer of the new node to the previous tail,
    5. set the list’s own pointer-to-tail to the new node.

    If your reader thread comes in between 2 and 3, then it will go from the previous tail, to the new node, then try to follow an uninitialized pointer. Boom.

    In general though, you need synchronization because it’s the only way to guarantee that changes made in the writer thread are published to the reader thread in any kind of sensible order (or at all).

    Your code should be correct if you imagine that different threads run on different planets, each with its own copy of your program’s memory, and transmit changes to each other (a) when you use a synchronization object (or atomic variable in C++11), plus (b) when you don’t use a synchronization object but transmitting some particular partial change will break your code (such as one half of a two-word object or in this case one of two pointer writes that you need to occur in a particular order). Sometimes this model is more conservative than strictly necessary, and results in slower code. But a less conservative model relies on implementation-specific details of the threading system and memory model.

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

Sidebar

Related Questions

I need to have these 2 threads working constantly on a Generic List, one
I have a list of member IDs that I need to loop over and
I have one list that I want to take a slice of, reverse that
I have one list of entries, and these entry shorted by alphabetically, now I
I have a list of e-mails from one database which I want to check
I have a sortable list like this one: http://jqueryui.com/demos/sortable Is it possible to get
I have a list of Organizations (CalledOrganizationSelectList) and one of them is the one
I have a list of hclust objects resulting from slight variations in one variable
I have a list of keywords and want to see if one cell contains
If I have a list of numbers like this one: 10,9,8,8,9,7,6,5,4,6,7,8,11,10,12,14,16,20,30,29,28,29,27,25,20,18,15,10,8,5,4,1 I want to

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.