How can I write a lock free structure?

后端 未结 21 1821
忘了有多久
忘了有多久 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:44

    Cliff Click has dome some major research on lock free data structures by utilizing finite state machines and also posted a lot of implementations for Java. You can find his papers, slides and implementations at his blog: http://blogs.azulsystems.com/cliff/

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

    The basic principle for lock-free synchronisation is this:

    • whenever you are reading the structure, you follow the read with a test to see if the structure was mutated since you started the read, and retry until you succeed in reading without something else coming along and mutating while you are doing so;

    • whenever you are mutating the structure, you arrange your algorithm and data so that there is a single atomic step which, if taken, causes the entire change to become visible to the other threads, and arrange things so that none of the change is visible unless that step is taken. You use whatever lockfree atomic mechanism exists on your platform for that step (e.g. compare-and-set, load-linked+store-conditional, etc.). In that step you must then check to see if any other thread has mutated the object since the mutation operation began, commit if it has not and start over if it has.

    There are plenty of examples of lock-free structures on the web; without knowing more about what you are implementing and on what platform it is hard to be more specific.

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

    In Java, utilize the java.util.concurrent packages in JDK 5+ instead of writing your own. As was mentioned above, this is really a field for experts, and unless you have a spare year or two, rolling your own isn't an option.

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

    Use a library such as Intel's Threading Building Blocks, it contains quite a few lock -free structures and algorithms. I really wouldn't recommend attempting to write lock-free code yourself, it's extremely error prone and hard to get right.

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

    Most lock-free algorithms or structures start with some atomic operation, i.e. a change to some memory location that once begun by a thread will be completed before any other thread can perform that same operation. Do you have such an operation in your environment?

    See here for the canonical paper on this subject.

    Also try this wikipedia article article for further ideas and links.

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

    Inmutability would have this effect. Changes to the object result in a new object. Lisp works this way under the covers.

    Item 13 of Effective Java explains this technique.

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