Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between lockless and lockfree?

In some articles about algorithm, some use the word lockfree, and some use lockless. What's the difference between lockless and lockfree? Thanks!

Update

http://www.intel.com/content/dam/www/public/us/en/documents/guides/intel-dpdk-programmers-guide.pdf

section 5.2 --"Lockless Ring Buffer in Linux*", it's a example of use word "lockless"

like image 579
大宝剑 Avatar asked Dec 09 '13 09:12

大宝剑


People also ask

What is Lockless?

adjective. ˈläklə̇s. : having no lock. a lockless cabin. a long lockless stretch of water.

What is the different between lock-free and wait-free?

Intuitively, lock-free means that some process is always guaranteed to make progress by completing its operations within a finite number of system steps, while wait-free means that each process completes its operations within a finite number of its own steps.

Is lock-free 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.

What does lock-free mean in C++?

Lock-free techniques allow multiple threads to work together in a non-blocking way, often achieving incredible performance. As the name suggests, locks are not used.


2 Answers

An algorithm is lock-free if it satisfies that when the program threads are run sufficiently long at least one of the threads makes progress (for some sensible definition of progress). All wait-free algorithms are lock-free.

In general, a lock-free algorithm can run in four phases: completing one's own operation, assisting an obstructing operation, aborting an obstructing operation, and waiting. Completing one's own operation is complicated by the possibility of concurrent assistance and abortion, but is invariably the fastest path to completion. e.g. Non blocking algorithms

Lockless programming, is a set of techniques for safely manipulating shared data without using locks. There are lockless algorithms available for passing messages, sharing lists and queues of data, and other tasks. Lockless programming is pretty complicated. e.g. All purely functional data structures are inherently lock-free, since they are immutable

like image 164
aryann Avatar answered Oct 19 '22 09:10

aryann


Lock-free is a more formal thing (look for lock-free algorithms). The essence of it for data structures is that if two threads/processes access the data structure and one of them dies, the other one is still guaranteed to complete the operation.

Lockless is about implementation - it means the algorithm does not use locks (or using the more formal name - mutual exclusion).

Therefore a lock-free algorithm is also lockless (because if one thread locks and then dies the other one would wait forever) but not the other way around - there are algorithms which don't use locks (e.g. they use compare-and-swap) but still can hang if the other process dies. The dpdk ring buffer mentioned above is an example of lockless which is not lock-free.

like image 34
Uri Simchoni Avatar answered Oct 19 '22 10:10

Uri Simchoni