Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concurrent linked list

I am trying to design a linked list in c++ that allows concurrent access. Clearly using a single lock for this list is grossly inefficient since disjoint areas may be updated in parallel. Now what are my options other than storing a lock per node?

Also, would a non-blocking version be a better bet in this case? Any relevant links, anyone?

EDIT: Thanks for the responses people. Couple of things I'd like to add:

  1. How about storing N locks for every M nodes instead of 1:1 lock:node ratio? Some threads would wait but it's kind of a trade off. What do you think?
  2. If I intend to find some node in this linked list looks like all the mutexes need be locked. This is an issue, because locking and unlocking of all the mutexes is time consuming. Does anyone have a better alternative?
  3. I am open to non-blocking algorithms. But how do I use CAS in good old C++ without using assembly? I hear that gcc has some __sync attributes that do similar work but not sure.
  4. With non-blocking approach, how do you do a find on the linked list?
like image 997
Fanatic23 Avatar asked Nov 12 '10 17:11

Fanatic23


People also ask

What is concurrent linked list?

It is used to implement Queue with the help of LinkedList concurrently. It is an unbounded thread-safe implementation of Queue which inserts elements at the tail of the Queue in a FIFO(first-in-first-out) fashion. It can be used when an unbounded Queue is shared among many threads.

Is linked list LIFO or FIFO?

A singly-linked list may be LIFO (last-in-first-out) or FIFO (first-in-first-out). If the list is using the LIFO method, the nodes will be added to and deleted from the same end. If it's using FIFO, nodes will be added to one end and deleted from the opposite end. Additionally, the linked list may be sorted.

What is concurrent linked queue?

ConcurrentLinkedQueue is an unbounded thread-safe queue which arranges the element in FIFO. New elements are added at the tail of this queue and the elements are added from the head of this queue. ConcurrentLinkedQueue class and its iterator implements all the optional methods of the Queue and Iterator interfaces.

What is a linked list example?

Just like a garland is made with flowers, a linked list is made up of nodes. We call every flower on this particular garland to be a node. And each of the node points to the next node in this list as well as it has data (here it is type of flower).


2 Answers

Linked lists are inherently sequential data structures. No matter what kind of machinery you use to implement it, the interface and expected behavior imply sequential logic.

What are you trying to do? That should determine what data structure you use, not the familiarity of the data structures you already are comfortable with.

like image 64
MSN Avatar answered Oct 12 '22 11:10

MSN


It's possible to implement a non-blocking singly linked list using interlocked functions:

Interlocked Singly Linked Lists

I don't think it's possible to implement a non-blocking doubly linked list without interlocked compare-and-swap (CAS), which isn't widely available.

like image 23
Martin Broadhurst Avatar answered Oct 12 '22 13:10

Martin Broadhurst