开发者

ThreadSafe LinkedList Implementation

开发者 https://www.devze.com 2023-03-04 12:04 出处:网络
I know that the linkedlist is not threadsafe and at work I ve been asked to write a bare bones thread safe linkedlist.

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
        //开发者_如何学Go/ </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; }         
        }            
    }


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

精彩评论

暂无评论...
验证码 换一张
取 消