A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does.
A program probably causes deadlock between two threads that are both trying to acquire locks for the same two resources.
To avoid this sort of deadlock when locking multiple resources, all threads should always acquire their locks in the same order.
Thread 1 locked resource1, and won't release it 'till it gets a lock on
resource2. This thread2 holds the lock on resource2, and won't
release it 'till it gets resource1. We're at an impasse. Neither thread can run, and the program freezes up.
A program probably causes deadlock between two threads that are both trying to acquire locks for the same two resources.
To avoid this sort of deadlock when locking multiple resources, all threads should always acquire their locks in the same order.
Thread 1 locked resource1, and won't release it 'till it gets a lock on
resource2. This thread2 holds the lock on resource2, and won't
release it 'till it gets resource1. We're at an impasse. Neither thread can run, and the program freezes up.
本文解析了死锁的概念,即两个或更多线程互相等待对方释放资源而产生的僵局。阐述了死锁发生的典型场景:两个线程试图以不同顺序锁定相同的两份资源。并举例说明了如何通过统一资源锁定顺序来避免此类死锁。

被折叠的 条评论
为什么被折叠?



