Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有人可以给我看一个简单的死锁吗? C++ 中的两个线程示例
最简单的之一 - 它是伪代码,但您应该能够将其翻译成几乎任何语言:
线程 1:
acquire_lock(lockA); ... do some work ... acquire_lock(lockB);
线程 2:
acquire_lock(lockB); ... do some other work ... acquire_lock(lockA);
如果线程 1 正在执行并且尚未持有 lockB 并且线程 2 获取 lockB,则两个线程都不能继续通过第二次获取锁。