问题描述
我读到,免等待会导致所有线程独立完成,而免锁则确保了程序整体完成。我不太明白。谁能举一个例子(java)来说明这一点。
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.
编辑:无锁意味着程序没有死锁吗?
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; and so we can say that the threads finish independently. All wait-free programs are lock-free.
我不知道有任何Java例子可以说明这一点,但我可以告诉您无锁/等待-免费程序通常使用CAS指令之类的低级原语实现而没有锁。
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.
这篇关于无等待和无锁算法的示例/说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!