concurrent linked list

后端 未结 5 1250
既然无缘
既然无缘 2021-02-05 20:45

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

相关标签:
5条回答
  • 2021-02-05 21:25

    There are lots of different ways you could go about this, depending on how you're going to use the list.

    First, for a list, a single mutex lock is not necessarily a big deal because lists can support splicing. Say you have a list of chars AF and another list BCDE. You don't have to lock the mutex, insert B, lock it again, insert C, etc. You can insert BCDE all at once between A and F by setting A's next pointer to B and E's next pointer to F, forming ABCDEF. No matter how many elements you want to insert at once you only need one lock. This is true even for a doubly linked list. So it might not be a bottleneck in your application.

    Assuming that it would be a bottleneck though, you need to consider whether you're going to have one or multiple writers and one or multiple readers.

    Assuming you have a single writer and multiple readers, you can avoid mutex locks entirely by using atomic instructions, assuming they're available for your architecture. In GCC these are available through the builtin __sync_* functions and in Visual C++ they're available via Interlocked*, but if you're stuck with a compiler that lacks direct support for them you can still use them via inline assembly. The writer will use the atomic instructions to atomically set next pointers to patch elements in and out of the list.

    Hopefully that gets you started. To get a deep answer I'd suggest asking another question and including:

    1. Are there one/multiple readers?
    2. Are there one/multiple writers?
    3. Singly or doubly linked list?
    4. Some idea of what your use case is.
    0 讨论(0)
  • 2021-02-05 21:27

    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.

    0 讨论(0)
  • 2021-02-05 21:31

    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.

    0 讨论(0)
  • 2021-02-05 21:32

    What about Skip Lists? Have a look at java implementation in "concurrent" package.

    0 讨论(0)
  • 2021-02-05 21:37

    Are you sure this is a problem worth solving? Would it perhaps be more useful to encapsulate the actual end-use into a class that handles the locking of a normal list and provides a thread-safe interface? This way you don't try to put locking at too low of a level (which as you surmised is not an easy problem to solve).

    0 讨论(0)
提交回复
热议问题