multithreading 竞争条件和死锁之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3130079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between racearound condition and deadlock
提问by ckv
What is the difference between a dead lock and a race around condition in programming terms?
在编程术语中,死锁和条件竞争之间有什么区别?
回答by BobbyShaftoe
Think of a race condition using the traditional example. Say you and a friend have an ATM cards for the same bank account. Now suppose the account has $100 in it. Consider what happens when you attempt to withdraw $10 and your friend attempts to withdraw $50 at exactly the same time.
使用传统示例来考虑竞争条件。假设您和一个朋友有一张用于同一个银行账户的 ATM 卡。现在假设账户中有 100 美元。考虑一下当您尝试提取 10 美元而您的朋友尝试在完全相同的时间提取 50 美元时会发生什么。
Think about what has to happen. The ATM machine must take your input, read what is currently in your account, and then modify the amount. Note, that in programming terms, an assignment statement is a multi-step process.
想想必须发生什么。ATM 机必须接受您的输入,读取您帐户中的当前金额,然后修改金额。请注意,在编程术语中,赋值语句是一个多步骤过程。
So, label both of your transactions T1 (you withdraw $10), and T2 (your friend withdraws $50). Now, the numbers below, to the left, represent time steps.
因此,标记您的交易 T1(您提取 10 美元)和 T2(您的朋友提取 50 美元)。现在,下面左边的数字代表时间步长。
T1 T2
---------------- ------------------------
1. Read Acct (0)
2. Read Acct (0)
3. Write New Amt ()
4. Write New Amt ()
5. End
6. End
After both transactions complete, using this timeline, which is possible if you don't use any sort of locking mechanism, the account has $50 in it. This is $10 more than it should (your transaction is lost forever, but you still have the money).
两笔交易完成后,使用此时间线(如果您不使用任何类型的锁定机制,这是可能的),帐户中有 50 美元。这比它应该多出 10 美元(您的交易永远丢失了,但您仍然有钱)。
This is a called race condition. What you want is for the transaction to be serializable, that is in no matter how you interleave the individual instruction executions, the end result will be the exact same as someserial schedule (meaning you run them one after the other with no interleaving) of the same transactions. The solution, again, is to introduce locking; however incorrect locking can lead to dead lock.
这就是所谓的竞争条件。您想要的是使事务可序列化,即无论您如何交错单个指令执行,最终结果都将与某些串行调度完全相同(意味着您一个接一个地运行它们而没有交错)相同的交易。解决方案同样是引入锁定;但是不正确的锁定会导致死锁。
Deadlock occurs when there is a conflict of a shared resource. It's sort of like a Catch-22.
当共享资源发生冲突时会发生死锁。这有点像 Catch-22。
T1 T2
------- --------
1. Lock(x)
2. Lock(y)
3. Write x=1
4. Write y=19
5. Lock(y)
6. Write y=x+1
7. Lock(x)
8. Write x=y+2
9. Unlock(x)
10. Unlock(x)
11. Unlock(y)
12. Unlock(y)
You can see that a deadlock occurs at time 7 because T2 tries to acquire a lock on x
but T1 already holds the lock on x
but it is waiting on a lock for y
, which T2 holds.
您可以看到在时间 7 发生了死锁,因为 T2 尝试获取锁,x
但 T1 已经持有该锁,x
但它正在等待y
T2 持有的锁。
This bad. You can turn this diagram into a dependency graph and you will see that there is a cycle. The problem here is that x and y are resources that may be modified together.
这不好。你可以把这个图变成一个依赖图,你会看到有一个循环。这里的问题是 x 和 y 是可以一起修改的资源。
One way to prevent this sort of deadlock problem with multiple lock objects (resources) is to introduce an ordering. You see, in the previous example, T1 locked x
and then y
but T2 locked y
and then x
. If both transactions adhered here to some ordering rule that says "x
shall always be locked before y
" then this problem will not occur. (You can change the previous example with this rule in mind and see no deadlock occurs).
防止这种多锁对象(资源)死锁问题的一种方法是引入排序。您会看到,在前面的示例中,T1 锁定x
,然后y
T2 锁定y
,然后x
。如果这里的两个事务都遵守一些排序规则,即“x
应始终在之前锁定y
”,那么这个问题就不会发生。(您可以根据此规则更改前面的示例,并且不会发生死锁)。
These are trivial examples and really I've just used the examples you may have already seen if you have taken any kind of undergrad course on this. In reality, solving deadlock problems can be much harder than this because you tend to have more than a couple resources and a couple transactions interacting.
这些都是微不足道的例子,我只是使用了你可能已经看过的例子,如果你参加过任何类型的本科课程。实际上,解决死锁问题可能比这要困难得多,因为您往往拥有多个资源和多个交互事务。
Hope this helps a little bit. As always, use Wikipedia as a starting point for CS concepts:
希望这会有所帮助。与往常一样,使用 Wikipedia 作为 CS 概念的起点:
http://en.wikipedia.org/wiki/Deadlock
http://en.wikipedia.org/wiki/Deadlock
回答by TFal
A deadlock is when two (or more) threads are blocking each other. Usually this has something to do with threads trying to acquire shared resources. For example if threads T1 and T2 need to acquire both resources A and B in order to do their work. If T1 acquires resource A, then T2 acquires resource B, T1 could then be waiting for resource B while T2 was waiting for resource A. In this case, both threads will wait indefinitely for the resource held by the other thread. These threads are said to be deadlocked.
死锁是指两个(或多个)线程相互阻塞。通常这与尝试获取共享资源的线程有关。例如,如果线程 T1 和 T2 需要同时获取资源 A 和 B 才能完成它们的工作。如果 T1 获取资源 A,则 T2 获取资源 B,然后 T1 可能正在等待资源 B,而 T2 正在等待资源 A。在这种情况下,两个线程将无限期地等待另一个线程持有的资源。这些线程被称为死锁。
Race conditions occur when two threads interact in a negatve (buggy) way depending on the exact order that their different instructions are executed. If one thread sets a global variable, for example, then a second thread reads and modifies that global variable, and the first thread reads the variable, the first thread may experience a bug because the variable has changed unexpectedly.
当两个线程以否定(错误)方式交互时,会发生竞争条件,具体取决于执行不同指令的确切顺序。例如,如果一个线程设置了一个全局变量,然后第二个线程读取并修改了该全局变量,而第一个线程读取了该变量,则第一个线程可能会遇到错误,因为该变量已意外更改。
回答by cryptonkid
Deadlock :
死锁:
- This happens when 2 or morethreads are waitingon each other to release the resource for infinite amount of time.
- In this the threads are in blocked state and not executing.
- 当2 个或更多线程无限期地等待彼此释放资源时,就会发生这种情况。
- 在这种情况下,线程处于阻塞状态并且不执行。
Race/Race Condition:
比赛/比赛条件:
- This happens when 2 or morethreads run in parallelbut end up giving a result which is wrong and not equivalent if all the operations are done in sequential order.
- Here all the threads run and execute there operations.
- 当2 个或更多线程并行运行但最终给出的结果是错误的并且如果所有操作都按顺序完成时不等效,就会发生这种情况。
- 在这里,所有线程都运行并执行那里的操作。
In Coding we need to avoid both race and deadlock condition.
在编码中,我们需要避免竞争和死锁情况。
回答by Dean Harding
I assume you mean "race conditions" and not "race around conditions" (I've heard that term...)
我假设你的意思是“竞争条件”而不是“竞争条件”(我听说过这个词......)
Basically, a dead lock is a condition where thread A is waiting for resource X while holding a lock on resource Y, and thread B is waiting for resource Y while holding a lock on resource X. The threads block waiting for each other to release their locks.
基本上,死锁是这样一种情况:线程 A 正在等待资源 X 同时持有资源 Y 的锁,而线程 B 正在等待资源 Y 同时持有资源 X 的锁。线程阻塞等待彼此释放它们锁。
The solution to this problem is (usually) to ensure that you take locks on all resources in the same orderin all threads. For example, if you always lock resource X beforeresource Y then my example can never result in a deadlock.
这个问题的解决方案是(通常)确保您在所有线程中以相同的顺序锁定所有资源。例如,如果您总是在资源 Y之前锁定资源 X ,那么我的示例永远不会导致死锁。
A race condition is something where you're relying on a particular sequence of events happening in a certain order, but that can be messed up if another thread is running at the same time. For example, to insert a new node into a linked list, you need to modify the list head, usually something like so:
竞争条件是指您依赖于以特定顺序发生的特定事件序列,但如果另一个线程同时运行,则可能会搞砸。例如,要在链表中插入一个新节点,您需要修改链表头,通常是这样的:
newNode->next = listHead;
listHead = newNode;
But if two threads do that at the same time, then you might have a situation where they run like so:
但是如果两个线程同时这样做,那么你可能会遇到这样的情况:
Thread A Thread B
newNode1->next = listHead
newNode2->next = listHead
listHead = newNode2
listHead = newNode1
If this were to happen, then Thread B's modification of the list will be lost because Thread A would have overwritten it. It can be even worse, depending on the exact situation, but that's the basics of it.
如果发生这种情况,线程 B 对列表的修改将丢失,因为线程 A 会覆盖它。根据具体情况,情况可能更糟,但这是基本情况。
The solution to this problem is usually to ensure that you include the proper locking mechanisms (for example, take out a lock any time you want to modify the linked list so that only one thread is modifying it at a time).
解决这个问题的方法通常是确保您包含正确的锁定机制(例如,在您想要修改链表的任何时候取出一个锁,以便一次只有一个线程在修改它)。