在this问题是有人提出同时写入 char 数组中的两个不同偏移量将意味着数据竞争,因为某些处理器(例如 Alpha)没有按字节寻址,因此很难实现这一点。
我当然看到这会大大减慢在 alpha 处理器上写入字节的速度(主要涉及 LL/SC),但据我了解 C++ 标准,数组中的每个字段都是它自己的内存位置(尽管从阅读 §1.7 ,我也可以将整个数组视为一个内存位置——这可能就是这个问题的归结所在)。
所以基本上就是下面的伪代码
char arr[10]; // global field
Thread 1:
arr[1] = 0;
Thread 2:
arr[0] = 1;
是否根据 C++14 标准定义良好?
最佳答案
来自 C++14 标准 (1.7/3):
Two or more threads of execution (1.10) can update and access separate memory locations without interfering with each other.
它之前定义的地方(强调我的)
A memory location is either an object of scalar type or a maximal sequence of adjacent bit-fields all having non-zero width.
所以数组的 char
是内存位置,但数组本身不是;因此,写入不同 char
的单独线程不会相互干扰。
关于c++ - 写入数组中的不同偏移量总是定义明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34706564/