.net – C # Specialized ConcurrentLinkedList

I need a concurrent data structure with the following properties:

  • Little effort for enumeration
  • Only paste at the end.
  • Distance from any point.
  • The enumeration should be safe when writing at the same time
  • The enumeration does not require that the list is in a constant state
  • No problem if I listed removed items

I've designed the following linked list class that should meet my needs.

using System.Collections;
using System.Collections.Generic;

internal class ConcurrentLinkedList : IEnumerable <Node> {
private read-only baseNode _Root;
private BaseNode _The End;
public ConcurrentLinkedList () {
_end = _root = new BaseNode();
}

public node Insert (T-value) {
var node = new node(Value);
lock (node) {
lock (_end) {
node.Previous = _end;
_end = _end.Next = node;
Return node;
}
}
}

public void Remove (Remove node Node) {
lock (node.Previous) {
lock (node) {
if (node.Next == null) {
if (node.Previous.Next! = node) return;
node.Previous.Next = null;
if (_end == node) _end = node.Previous;
} else {
lock (node.Next) {
if (node.Previous.Next == node) node.Previous.Next = node.Next;
if (node.Next.Previous == node) node.Next.Previous = node.Previous;
}
}
}
}
}


public IEnumerator <node> GetEnumerator () {
for (var current = _root.Next; current! = null; current = current.Next) {
Return return current;
}
}

IEnumerator IEnumerable.GetEnumerator () {
return GetEnumerator ();
}
}

internal class BaseNode {
internal volatile node Next;
}

internal class node : BaseNode {
internal BaseNode Previous;

public T-value {get; }

public node (T-value) {
Value = value;
}
}

I'm aware that the calls can be easily misused, but since it's only for internal use, I'm not worried about it. I am considering creating the entire private subclass of the class to make sure that there is no accidental misuse.

The code contains all the writing in a lock, but there are shared volatile items that are read from the lock. Is there any danger besides the ability to list dropped nodes?

For locking I do not lock all nodes that are changed. This should allow other parts of the list to be changed at the same time. The list can be long. Is that enough?

To avoid deadlock, I always lock from the lowest node to the last one.

Is the included lock sufficient to protect the _end field? I believe that according to the logic used, the current end is always locked when the value of End is changed. Is that enough?

This code tries to be smart and it would be better to either:
– have a write lock and use the volatile material for reading.
– Use a standard ReadWriteLock for the entire list.