How can I write a lock free structure?

后端 未结 21 1823
忘了有多久
忘了有多久 2020-12-13 12:57

In my multithreaded application and I see heavy lock contention in it, preventing good scalability across multiple cores. I have decided to use lock free programming to solv

相关标签:
21条回答
  • 2020-12-13 13:52

    If you are writing your own lock-free data structures for a multi-core cpu, do not forget about memory barriers! Also, consider looking into Software Transaction Memory techniques.

    0 讨论(0)
  • 2020-12-13 13:53

    Short answer is:

    You cannot.

    Long answer is:

    If you are asking this question, you do not probably know enough to be able to create a lock free structure. Creating lock free structures is extremely hard, and only experts in this field can do it. Instead of writing your own, search for an existing implementation. When you find it, check how widely it is used, how well is it documented, if it is well proven, what are the limitations - even some lock free structure other people published are broken.

    If you do not find a lock free structure corresponding to the structure you are currently using, rather adapt the algorithm so that you can use some existing one.

    If you still insist on creating your own lock free structure, be sure to:

    • start with something very simple
    • understand memory model of your target platform (including read/write reordering constraints, what operations are atomic)
    • study a lot about problems other people encountered when implementing lock free structures
    • do not just guess if it will work, prove it
    • heavily test the result

    More reading:

    Lock free and wait free algorithms at Wikipedia

    Herb Sutter: Lock-Free Code: A False Sense of Security

    0 讨论(0)
  • 2020-12-13 13:53

    Well, it depends on the kind of structure, but you have to make the structure so that it carefully and silently detects and handles possible conflicts.

    I doubt you can make one that is 100% lock-free, but again, it depends on what kind of structure you need to build.

    You might also need to shard the structure so that multiple threads work on individual items, and then later on synchronize/recombine.

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