此示例是否包含数据争用? [英] Does this example contain a data race?

查看:94
本文介绍了此示例是否包含数据争用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是起源问题,但是我的问题有所不同. C ++内存模型-此示例是否包含数据竞争?

Here is the originan question, but mine have some differences with it. C++ memory model - does this example contain a data race?

我的问题:

//CODE-1: initially, x == 0 and y == 0
if (x) y++; // pthread 1
if (y) x++; // pthread 2

注意:上面的代码是用C而不是C ++(没有内存模型)编写的.那么它包含数据竞赛吗?

Note: the code above is written in C, not C++ (without a memory model). So does it contain a data race?

从我的角度来看:如果我们在顺序一致性内存模型中查看代码,则不会发生数据争用,因为x和y永远不会同时非零.但是,我们永远不能假设序列一致性内存模型,因此编译器重新排序可以进行关于线程内正确性的转换,因为编译器不知道线程的存在.......对吗?

From my point of view: if we view the code in Sequential Consistency memory model, there is no data race because x and y will never be both non-zero at the same time. However, we can never assume a Sequential Consistency memory model, so the compilier reordering could make a transformation that respect to the intra-thread correctness because the compiler isn't aware of the existence of thread.......right?

因此代码可以转换为:

//CODE-2
y++; if (!x) y--;
x++; if (!y) x--;

上面的转换没有违反顺序的正确性,所以它是正确的.这不是编译器的错,对吗?因此,我同意CODE-1包含数据竞争的观点.您呢?

the transformation above doesn't violate the sequential correctness so it's correct.It's not the fault of the compilier, right? So I agree with the view that the CODE-1 contains a data race.What about you?

我还有一个问题,具有内存模型的C ++ 11可以解决此数据争用,因为编译器知道线程,因此它们将根据内存模型类型进行重新排序,对吗?

I have an extra question, C++11 with a memory model can solve this data race because the compilers are aware of the thread, so they will do their reorder according to the memory model type, right?

推荐答案

C ++标准将数据争用(触发未定义的行为)定义为:

The C++ standard defines a data race (which triggers undefined behavior) as:

§1.10.1-2 [介绍性种族]
如果两个表达式求值之一修改一个内存位置(..),而另一个表达式读取或修改了相同的内存位置,则这两个表达式求值会发生冲突.

§ 1.10.1-2 [intro.races]
Two expression evaluations conflict if one of them modifies a memory location (..) and the other one reads or modifies the same memory location.

按照C ++内存模型规则,您的第一个代码片段不包含任何数据争用,因为C ++标准禁止编译器转换会引入这种争用:

Per the C++ memory model rules, your first code fragment contains no data race because the C++ standard forbids compiler transformations that would introduce such a race:

§1.10.1-21 [介绍性种族]
该国际标准通常排除了将分配引入可能不会由抽象机修改的潜在共享内存位置的编译器转换,因为这种分配可能会在不执行抽象机的情况下通过不同的线程覆盖另一个分配.遇到了数据争夺战.

§ 1.10.1-21 [intro.races]
Compiler transformations that introduce assignments to a potentially shared memory location that would not be modified by the abstract machine are generally precluded by this International Standard, since such an assignment might overwrite another assignment by a different thread in cases in which an abstract machine execution would not have encountered a data race.

因此它说,如果if语句(x)中的条件产生false,则即使最终结果是y看起来未修改,也不允许进行任何修改y的转换.

So it says that if the condition in the if-statement (x) yields false, no transformation is allowed that would modify y, even if the end result is that y appears unmodified.

第二个示例显然包含一个数据争用,因为2个线程可以同时写入和读取x(与y相同).

The second example clearly contains a data race because 2 threads can write and read x at the same time (same applies to y).

请注意,自版本11起,C ++和C都具有内存模型.如果使用不支持C11的编译器,则不会正式定义多线程行为.

Note that both C++ and C have a memory model since version 11. If you use a compiler that does not support C11, multithreaded behavior is not officially defined.

这是一个问题,其中显示了非法编译器转换的示例.

Here is a question that shows an example of an illegal compiler transformation.

这篇关于此示例是否包含数据争用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆