写入数组中的不同偏移量总是定义良好 [英] Writing to different offsets in array always well defined

查看:83
本文介绍了写入数组中的不同偏移量总是定义良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题中,有人提出同时在char数组中写入两个不同的偏移量将意味着数据竞争. ,由于某些处理器(例如Alpha)没有按字节寻址,因此很难实现这一点.

In this question it was brought up that writing to two different offsets in a char array concurrently would imply a data race, since some processors such as Alpha don't have byte-wise addressing so it'd be hard to implement this.

我当然知道这会大大减慢在alpha处理器上写入字节的速度(基本上涉及LL/SC),但是据我了解C ++标准,数组中的每个字段都是其自己的内存位置(尽管从§1.7读取) ,我还可以将整个数组视为一个内存位置-这可能就是这个问题的原因.

I certainly see that this would very much slow down writing bytes on alpha processors (basically involving a LL/SC), but as I understand the C++ standard every field in an array is its own memory location (although from reading §1.7, I could also see the whole array as one memory location - that's probably what this question boils down to).

下面的伪代码基本上是

char arr[10]; // global field
Thread 1:
arr[1] = 0;
Thread 2:
arr[0] = 1;

定义是否符合C ++ 14标准?

well defined according to the C++14 standard or not?

推荐答案

根据C ++ 14标准(1.7/3):

From the C++14 standard (1.7/3):

两个或多个执行线程(1.10)可以更新和访问单独的内存位置,而不会互相干扰.

Two or more threads of execution (1.10) can update and access separate memory locations without interfering with each other.

先前定义的位置(强调我的位置)

Where it previously defines (emphasis mine)

存储位置可以是标量类型的对象,也可以是相邻位字段的最大序列,所有这些位都具有 非零宽度.

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的单独线程不会互相干扰.

So the chars of the array are memory locations, but the array itself is not; therefore, separate threads writing to different chars do not interfere with each other.

这篇关于写入数组中的不同偏移量总是定义良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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