简体   繁体   English

引用类型上的OpenMP原子?

[英]OpenMP atomic on a reference type?

The OpenMP standard (<= 4.0) says about atomic : OpenMP标准(<= 4.0)谈到了atomic

#pragma omp atomic [read | write | update | capture ] new-line
expression-stmt

where expression-stmt is an expression statement with one of the following forms: 其中expression-stmt是具有以下形式之一的表达式语句:
... ...
If clause is update or not present: 如果子句是更新的或不存在的:
x++;
... ...
In the preceding expressions: 在前面的表达式中:
x and v (as applicable) are both l-value expressions with scalar type. xv (如适用)都是标量类型的l值表达式。
... ...

So, when I interpret this correctly, the following short code snippet is illegal: 因此,当我正确解释时,以下短代码段是非法的:

int main()
{
  int myCounter = 0;
  int& reference = myCounter;

  #pragma omp parallel for
  for (int i = 0; i < 100; ++i)
  {
    #pragma omp atomic
    reference++; // Increment through reference.
  }
  return 0;
}

Reason: According to this post , a reference (here int& reference ) is not a scalar type. 原因:根据这篇文章 ,引用(此处为int& reference )不是标量类型。 But the standard explicitly states that it must be one, in order to use atomic . 但是该标准明确指出,为了使用atomic ,它必须是一个。

The code compiles with g++, without any warning ( -Wall -Wextra ). 该代码使用g ++编译,没有任何警告( -Wall -Wextra )。

My question is: Have I misunderstood the standard, or the concept of C++'s "reference type"? 我的问题是:我是否误解了标准或C ++“引用类型”的概念? Or do most compilers compile this code, because otherwise the use of atomic is severely limited (basically no data on the heap could be the target of atomic , because you always need a reference or a dereferenced pointer)? 还是大多数编译器都编译该代码,否则atomic的使用会受到严格限制(基本上,堆上的任何数据都不会成为atomic的目标,因为您始终需要引用或取消引用的指针)?

A reference type is not a scalar type. 引用类型不是标量类型。 However, this fact has no bearing on your question. 但是,此事实与您的问题无关。 The important fact is that an expression that evaluates a reference to a scalar type is an lvalue with scalar type. 一个重要的事实是,计算结果为标量类型的引用的表达式与标量类型的左值。 To be specific, the variable reference has type int& but the expression reference has type int and value category lvalue. 具体来说, 变量 reference类型为int&表达式 reference类型为int ,值类别为lvalue。 So yes, your program is conforming. 是的,您的程序符合要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM