movq
2023-02-17 23:20:16 +08:00
我总结一下死锁和活锁的对比,以下来自于《 C++ Concurrency in Action 》
1.死锁没法救活,除非编程时就避免。这一点活锁有优势,因为活锁就算你编程时不避免,由于 CPU 的调度存在一定的偏差,也有可能可以自行脱离活锁。(可以看引用的“not-so-serious cases”)
2.死锁发生时因为是等待,所以不会自旋,不占 CPU ,这方面是比活锁好的。
> Deadlock - As you saw in chapter 3, in the case of deadlock, one thread is waiting for another, which is in turn waiting for the first. If your threads deadlock, the tasks they're supposed to be doing won't get done. In the most visible cases, one of the threads involved is the thread responsible for the user interface, in which case the interface will cease to respond. In other cases, the interface will remain responsive, but some required tasks won't complete, such as a search not returning or a document not printing.
> Livelock - Livelock is similar to deadlock in that one thread is waiting for another, which is in turn waiting for the first. The key difference here is that the wait is not a blocking wait but an active checking loop, such as a spin lock. In serious cases, the symptoms are the same as deadlock (the app doesn't make any progress), except that the CPu usage is high because threads are still running but blocking each other. In not-so-serious cases, the livelock will eventually resolve because of the random scheduling, but there will be a long delay in the task that got livelocked, with a high CPU usage during that delay.