并发非原子 Read/Write 是未定义的行为吗?
Is Concurrent Non-Atomic Read/Write An Undefined Behavior?
C++ 基本类型变量的并发非原子读写(多次读取和多次写入)是 C++ 中的未定义行为吗?我不关心实际值,因为稍后我会发现并发 read/write 是否已经发生,如果是,我将忽略当前值。我只想知道该行为是否是定义明确的 C++?
如果定义明确,如果Thread 1 reads/writes x
and Thread 2 reads/writes y
in, where x
和 y
是以下 union
?
的成员
union {
int x;
double y;
};
Is concurrent non-atomic read and write on variables of C++ fundamental types (multiple reads and multiple writes) an undefined behavior in C++?
是的。该标准(引用最新草案)说:
[intro.races]
The execution of a program contains a data race if it contains two potentially concurrent conflicting actions, at least one of which is not atomic, and neither happens before the other, except for the special case for signal handlers described below.
Any such data race results in undefined behavior. ...
just want to know if the behavior is well-defined C++?
未定义。
if Thread 1 reads/writes x and Thread 2 reads/writes y in, where x and y are members of the following union?
这可能甚至“更多”未定义,因为不仅存在数据竞争,而且还可能读取联合体的非活动成员的值。
尽管该标准未能定义有关多线程的大部分内容(甚至没有定义什么是顺序的,什么是未定义的),但有一点很清楚:您不应该写入您以任何方式使用的任何变量"at the same time": 你必须使用互斥原语来修改有序的变量。
C++ 基本类型变量的并发非原子读写(多次读取和多次写入)是 C++ 中的未定义行为吗?我不关心实际值,因为稍后我会发现并发 read/write 是否已经发生,如果是,我将忽略当前值。我只想知道该行为是否是定义明确的 C++?
如果定义明确,如果Thread 1 reads/writes x
and Thread 2 reads/writes y
in, where x
和 y
是以下 union
?
union {
int x;
double y;
};
Is concurrent non-atomic read and write on variables of C++ fundamental types (multiple reads and multiple writes) an undefined behavior in C++?
是的。该标准(引用最新草案)说:
[intro.races]
The execution of a program contains a data race if it contains two potentially concurrent conflicting actions, at least one of which is not atomic, and neither happens before the other, except for the special case for signal handlers described below. Any such data race results in undefined behavior. ...
just want to know if the behavior is well-defined C++?
未定义。
if Thread 1 reads/writes x and Thread 2 reads/writes y in, where x and y are members of the following union?
这可能甚至“更多”未定义,因为不仅存在数据竞争,而且还可能读取联合体的非活动成员的值。
尽管该标准未能定义有关多线程的大部分内容(甚至没有定义什么是顺序的,什么是未定义的),但有一点很清楚:您不应该写入您以任何方式使用的任何变量"at the same time": 你必须使用互斥原语来修改有序的变量。