http://stackoverflow.com/questions/34510/what-is-a-race-condition
When writing multi-threaded applications, one of the most common problems experienced are race conditions. My question to the community, is: What is a race condition? How do you detect them? How do you handle them? And finally, how do you prevent them from occurring?
A race condition occurs when 2 or more threads are able to access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any point, you don't know the order at which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are 'racing' to access/change the data.
Often problems occur when one thread does a "check-then-act" (e.g. "check" if the value is X, and then "act" to do something that depends on the value being X) and another thread does something to the value in between the "check" and the "act".
if(x == 5) //The 'Check'
{
y = x * 2; //The 'Act'
//If x is changed by another thread in between the if(x==5) and the "y=x*5", y will not be equal to 10.
}
The point being, y could be 10, or it could be anything, depending on whether another thread changed x in between the check and act. You have no real way of knowing.
In order to prevent race conditions occuring, typically you would put a lock around the shared data to ensure that only one thread can access the data at a time. This would mean something like this:
//Obtain lock for x
if(x == 5)
{
y = x * 2; //Now, nothing can change x until the lock is released. Therefore y = 10
}
//release lock for x
本文详细解释了竞态条件的概念,即多个线程同时访问并尝试修改共享数据时可能产生的问题。文章通过具体示例说明了如何检测竞态条件,并探讨了预防措施,如使用锁来确保同一时刻只有一个线程可以访问共享数据。
1348

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



