这是比赛条件吗? [英] Is this a race condition?
问题描述
竞赛条件的定义:竞赛条件或竞赛危险是系统或过程中的缺陷,由此过程的输出或结果意外地且严重地依赖于其他事件的顺序或时间.
The definition of a race condition: A race condition or race hazard is a flaw in a system or process whereby the output or result of the process is unexpectedly and critically dependent on the sequence or timing of other events.
考虑以下伪代码:
Global variable i initialized to 6;
Thread 1:
acquire(lock l)
increment global variable i, i.e. i++;
Thread 2:
acquire(lock l)
double the value of global var i, i.e.: i*=2;
如果T1首先获取锁l,然后T2第二,则i的值为14.另一方面,如果T2首先获取锁l,而T1其次,则i的值为13.
If T1 acquires the lock l first and T2 second the value of i will be 14. On the other hand, if T2 acquires the lock l first and T1 second the value of i will be 13.
那么,这是否是比赛条件?
So, is this a race condition or not ?
更新:经过大量评论和回答后,意见仍存在分歧.我的意见是是的,这是一个竞赛条件"类别.实际上,在另一个问题上,我以竞争状况为例给出了此示例. 同时,我还阅读了不,这不是比赛条件"类别中的一些有趣评论. 我想我会解决并得出结论,这是一种竞赛条件,还是不是一种竞赛条件,这取决于人们从哪个角度/水平看待问题. 但是,我仍在等待有趣的答案/评论.
UPDATE: after a number of comments and answers, the opinions are still divergent. My opinion is in the "YES, this is a race condition" category. Actually I gave this example as a race condition situation, on a different question. At the same time, I also read some interesting comments in the "NO, this isn't a race condition" category. I think I will settle and conclude that this is or isn't a race condition depending on the perspective/level from which one looks at the problem. However, I'm still waiting for interesting answers/comments.
推荐答案
我认为示例算法是否具有竞争条件取决于该算法的预期功能.
I think whether the example algorithm has a race condition depends on the what the algorithm is expected to do.
i
的修改不会引起数据争用-这些访问是序列化的,并且彼此之间是原子发生的.
There is no data race on the modification of i
- those accesses are serialized and occur atomically with respect to each other.
但是,如果在乘法运算发生之前对增量进行运算对算法的正确性很重要(反之亦然),那么就存在竞争,必须使用其他方法来同步算法的执行.如果该算法被认为是计算i * 2 + 1
的一种复杂方法(使用线程执行计算可能很荒谬),那么就存在竞争条件.
However, if it's important to the correctness of the algorithm that the increment occur before the multiplication (or vice versa), then there is a race and other means would have to be used to synchronize the execution of the algorithm. If the algorithm is supposed to be a complicated way to calculate i * 2 + 1
(as ridiculous as it might be to perform the calculation using threads), then there's a race condition.
考虑以下程序片段:
int data;
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mux = PTHREAD_MUTEX_INITIALIZER;
void* wait_for_data(void*)
{
pthread_mutex_lock( &mux);
pthread_cond_wait( &condvar, &mux);
puts("got the data: %d\n", data);
pthread_mutex_unlock( &mux);
return 0;
}
void* set_data(void*)
{
pthread_mutex_lock( &mux);
data = 42;
pthread_cond_signal( &condvar);
pthread_mutex_unlock( &mux);
return 0;
}
两个线程本质上是完全互斥的-没有数据争用.但是,如果set_data()
在wait_for_data()
进入等待状态之前通知条件变量,则wait_for_data()
将永远不会完成.我认为大多数人会由于不正确使用条件变量而将其称为竞争条件.
Both threads are essentially completely mutually exclusive - there is no data race. However, if set_data()
signals the condition variable before wait_for_data()
gets around to waiting on it, wait_for_data()
will never complete. I think most people would call that a race condition due to improper use of the condition variable.
这篇关于这是比赛条件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!