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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:33:46+00:00 2026-05-22T01:33:46+00:00

I know that the linkedlist is not threadsafe and at work I ve been

  • 0

I know that the linkedlist is not threadsafe and at work I ve been asked to write a bare bones thread safe linkedlist.

Because of various complications which I wont go through I cannot simply wrap a LinkedList but need to write an implementation of a LinkedList

I am guessing I need this but how can I actually implement an enumerator (for the linedlist) in a thread safe way?

    public class LinkedlistNode
    {
        private LinkedlistNode next;
        private T item;
        /// <summary>
        /// Constructor for a new LinklistNode
        /// </summary>
        /// <param name="node">The node item to create</param>
        public LinkedlistNode(T node)
        {
            next = null;
            item = node;
        }
        /// <summary>
        /// Shows the next item in the collection (or shows null for the the last item)
        /// </summary>
        public LinkedlistNode Next
        {
            get { return next; }
            set { next = value; }
        }
        /// <summary>
        /// The contents of the list
        /// </summary>
        public T Item         
        {             
            get { return item; }             
            set { item = value; }         
        }            
    }
  • 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-22T01:33:47+00:00Added an answer on May 22, 2026 at 1:33 am

    Good start. I would doubly-link the list, first of all, by including a Previous property similar to Next.

    The major problem with making a linked list thread-safe is that, unlike an indexed collection, there are up to three objects that must be locked at the same time to perform adds and deletes. This increases the likelihood of deadlocks if, for instance, another thread is enumerating through the list; the add/remove thread needs to lock the fourth node to delete the fifth node, while the enumerating thread has already locked the fourth item and needs to lock the fifth. A singly-linked list would have the same problem, because that algorithm would require another enumeration top determine the “previous” node, which would end up blocked trying to get to the fourth item which is already locked by the first enumerator waiting to get to the fifth item.

    I guess there’s a big question to be asked: how exactly do you need to add and remove items? If this implementation will be used as the collection behind a Stack or Queue, it becomes MUCH easier to make it thread-safe, as enumerating the list will not be allowed, and of nodes currently in the list, only the endpoint node(s) (one for a Stack, 2 for a Queue) need to be locked when adding/removing, and unless the stack or queue only has one item, locking those nodes will only block other threads attempting to add or remove.

    If this is a full linked list implementation, requiring similar functionality to a List in terms of navigating to any item and adding and removing from anywhere, then I think your best bet is to hide the nodes behind a wrapper that will lock itself before performing any operation, much like Interlocked does with integral types. This is not a “fine-grained” approach by any means; any thread that wants to do anything to the list will have to wait its turn. There are just too many chances for deadlocks when trying to allow multiple threads simultaneous access.

    Your only hope for fine-grained, thread-safe locking without deadlocks is to always acquire locks in the same order the list will be enumerated, and to only allow iteration in one direction. Basically, that requires you to hide the “Previous” node of a doubly-linked list, and allow nodes to acquire “persistent” locks on other nodes.

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

Sidebar

Related Questions

From the various online articles on Java 7 I have come to know that
I know that I can do something like $int = (int)99; //(int) has a
I know that default cron's behavior is to send normal and error output to
I know that you can insert multiple rows at once, is there a way
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that .NET is JIT compiled to the architecture you are running on
I know that the following is true int i = 17; //binary 10001 int
I know that just using rand() is predictable, if you know what you're doing,
I know that IList is the interface and List is the concrete type but

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.