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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:57:03+00:00 2026-06-13T08:57:03+00:00

Ok, recently I’ve been thinking about thread safety a lot and I was wondering

  • 0

Ok, recently I’ve been thinking about thread safety a lot and I was wondering why linked lists or deques are not thread safe.

Let’s assume we have a simple linked list class like this:

class myLinkedList {
private:
    myLinkedList* next;
    int m_value;

public:
    myLinkedList() { next = NULL; m_value = 0; }
    void setValue(int value) { m_value = value; }
    int getValue() { return m_value; }
    void addNext(int value) { next = new myLinkedList; next->setValue(value); }
    myLinkedList* getNext() { return next; }
};

Now I just want to add new elements at the end and delete elements (read them first and then delete them) at the beginning. I basically just get the address of the first next, delete the first element and remember the next as my new first element. For adding new elements I only remember the last element and when I add a new element I just set a new next and remember my new next as the last element.

Where is the problem with threads in this scenario? Writers and readers shouldn’t have any problems with this since they never interact with each other. It’s not like using arrays or vectors (there I very well get why it causes problems).

  • 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-13T08:57:04+00:00Added an answer on June 13, 2026 at 8:57 am

    The comments to your question are correct, your implementation will not work. However, to answer the actual question, here’s a race condition in your code:

    void addNext(int value) { next = new myLinkedList; next->setValue(value) }
    

    Imagine that thread A executes:

    next = new myLinkedList;
    

    Now thread A gets preempted, and thread B also executes the same instruction. That means that next now doesn’t point to wherever thread A wants it to point to, but instead it points to wherever thread B set it. Thread B continues the execution with:

    next->setValue(value)
    

    Soon after that (or even at the same time), thread A also executes the above.

    Can you see the problem? Thread A calls next->setValue() on B’s next and A’s next is lost.

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

Sidebar

Related Questions

Recently I've been thinking about how to transform a complex polygon into a non-complex
recently I am reading about Thinking in Java. that code in my PC is
Recently,I am handling a solution for WebPart internationalization,but I am not familiar with the
Recently I've been doing quite the project mostly working with the DateTime class. Now,..
Recently I've read this performance guide Let's make the web faster and was puzzled
Recently I've been dealing with texts with mixed languages, including Chinese, English, and even
Recently I've been experimenting with the use of the Func<T> class, and so far
Recently I've noticed that on occasion I do not get a mayorship notification when
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently users of my website complained about the lack of support to IE6\7. Is

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.