Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what circumstances lock free data structures are faster than lock based ones?

I'm currently reading C++ Concurrency in Action book by Anthony Williams and there are several lock free data structures implementations. In the forward of the chapter about lock free data structures in the book Anthony is writing:

This brings us to another downside of lock-free and wait-free code: although it can increase the potential for concurrency of operations on a data structure and reduce the time an individual thread spends waiting, it may well decrease overall performance.

And indeed I tested all lock free stack implementations described in the book against lock based implementation from one of the previous chapters. And it seems the performance of lock free code is always lower than the lock based stack.

In what circumstances lock free data structure are more optimal and must be preferred?

like image 423
bobeff Avatar asked Oct 27 '16 13:10

bobeff


People also ask

Are lock-free algorithms faster?

The lock- free mode is almost as fast as blocking mode under almost all workloads, and significantly faster when threads are over- subscribed (more threads than processors). We also compare with several existing lock-based and lock-free alternatives. CCS Concepts: • Theory of computation → Concurrent algorithms.

Are lock-free queues faster?

Lock-Free queues provides us better performance for concurrent queue which is non-blocking and linearizable. Although it introduces ABA problem, we have some workaround solutions for it. In general, if if don't want to lock your queue in concurrent programming, try lock-free queue algorithm.

How do immutable and lock-free data structures help?

With a lock-free data structure, some thread makes progress with every step. With a wait-free data structure, every thread can make forward progress, regardless of what the other threads are doing; there's no need for waiting. This is a desirable property to have but hard to achieve.

What are lock-free data structures?

A lock-free data structure can be used to improve performance. A lock-free data structure increases the amount of time spent in parallel execution rather than serial execution, improving performance on a multi-core processor, because access to the shared data structure does not need to be serialized to stay coherent.


1 Answers

One benefit of lock-free structures is that they do not require context switch. However, in modern systems, uncontended locks are also context-switch free. To benefit (performance-wise) from lock-free algo, several conditions have to be met:

  • Contention has to be high
  • There should be enough CPU cores so that spinning thread can run uninterrupted (ideally, should be pinned to its own core)
like image 183
SergeyA Avatar answered Oct 27 '22 00:10

SergeyA