[英]Examples/Illustration of Wait-free And Lock-free Algorithms
I've read that wait-free causes all threads to finish independently and lock-free ensures the program as a whole completes.我读过无等待导致所有线程独立完成,无锁确保程序作为一个整体完成。 I couldn't quite get it.
我无法完全理解。 Can anyone give an example (java) illustrating this.
谁能举一个例子(java)来说明这一点。
EDIT: Does lock-free mean a program without deadlock?编辑:无锁是否意味着没有死锁的程序?
If a program is lock-free, it basically means that at least one of its threads is guaranteed to make progress over an arbitrary period of time.如果程序是无锁的,这基本上意味着它的至少一个线程可以保证在任意时间段内取得进展。 If a program deadlocks, none of its threads (and therefore the program as a whole) cannot make progress - we can say it's not lock-free.
如果一个程序死锁,它的线程(以及整个程序)都不能取得进展——我们可以说它不是无锁的。 Since lock-free programs are guaranteed to make progress, they are guaranteed to complete (assuming finite execution without exceptions).
由于无锁程序可以保证取得进展,因此它们可以保证完成(假设无异常的有限执行)。
Wait-free is a stronger condition which means that every thread is guaranteed to make progress over an arbitrary period of time, regardless of the timing/ordering of thread execution; Wait-free 是一种更强的条件,这意味着无论线程执行的时间/顺序如何,每个线程都可以保证在任意时间段内取得进展; and so we can say that the threads finish independently.
所以我们可以说线程是独立完成的。 All wait-free programs are lock-free.
所有无等待程序都是无锁的。
I don't know offhand of any Java examples which illustrate this but I can tell you that lock-free/wait-free programs are typically implemented without locks, using low-level primitives such as CAS instructions.我不知道有哪些 Java 示例可以说明这一点,但我可以告诉您,无锁/无等待程序通常是在没有锁的情况下使用低级原语(例如 CAS 指令)实现的。
No, Lock-free
means a program without locks.无,
Lock-free
的手段,而不锁的程序。 Although, a wait-free
algorithm is also lock-free
;虽然,无
wait-free
算法也是无lock-free
; however, vice versa doesn't hold.然而,反之亦然不成立。 But, both are non-blocking algorithms , nonetheless.
但是,尽管如此,两者都是非阻塞算法。
This wiki entry is a great read to understand lock-free and wait-free mechanism.这个wiki 条目是一个很好的读物,可以理解无锁和无等待机制。
Well, java.util.concurrent.atomic
package is an example of lock-free
programming on single variables.嗯,
java.util.concurrent.atomic
包是单变量lock-free
编程的一个例子。 And in Java 7 ConcurrentLinkedQueue
is an example of wait-free
implementation.而在 Java 7
ConcurrentLinkedQueue
是一个无wait-free
实现的例子。
For further insight, I would like you to read this article, Going atomic by Brian Goetz -- the guy who wrote Java Concurrency in Practice .为了进一步了解,我希望您阅读这篇文章, Brian Goetz撰写的Going atomic ,他是编写Java 并发实践的人。
From the weaker to the stronger condition:从弱到强的条件:
A method is lock-free if it guarantees that infinitely often some method call finishes in a finite number of steps.如果一个方法保证无限频繁地某些方法调用在有限数量的步骤中完成,则该方法是无锁的。
A method is wait-free if it guarantees that every call finishes its execution in a finite number of steps.如果一个方法保证每个调用在有限数量的步骤中完成它的执行,那么它就是无等待的。
Clearly, any wait-free method implementation is also lock-free, but not vice versa.显然,任何无等待方法的实现也是无锁的,但反之则不然。 Lock-free algorithms admit the possibility that some threads could starve.
无锁算法承认某些线程可能会饿死的可能性。
However, from a "Practical Perspective" there are many situations in which starvation, while possible, is extremely unlikely, so a fast lock-free algorithm may be more attractive than a slower wait-free algorithm.然而,从“实际角度”来看,在很多情况下,饥饿虽然可能,但极不可能,因此快速无锁算法可能比较慢的无等待算法更具吸引力。
NOTE: An even stronger property it is called " bounded wait-free " which means: there is a bound on the number of steps a method call can take.注意:一个更强大的属性称为“有界等待”,这意味着:方法调用可以采取的步骤数是有界限的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.