How can I write a lock free structure?

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

    If you see lock contention, I would first try to use more granular locks on your data structures rather than completely lock-free algorithms.

    For example, I currently work on multithreaded application, that has a custom messaging system (list of queues for each threads, the queue contains messages for thread to process) to pass information between threads. There is a global lock on this structure. In my case, I don't need speed so much, so it doesn't really matter. But if this lock would become a problem, it could be replaced by individual locks at each queue, for example. Then adding/removing element to/from the specific queue would didn't affect other queues. There still would be a global lock for adding new queue and such, but it wouldn't be so much contended.

    Even a single multi-produces/consumer queue can be written with granular locking on each element, instead of having a global lock. This may also eliminate contention.

提交回复
热议问题