繁体   English   中英

修改了 OpenMP 并行区域内函数的静态变量:竞争条件?

[英]static variable local to function within OpenMP parallel region is modified: race condition?

我正在处理一个大而混乱的代码库,我遇到了这样的情况。

#pragma omp parallel for 
for ( int i = 0; i < N; i++) {
  problemFunction();
}

void problemFunction() {

  static bool inFunction;
  if ( inFunction == true ) {
    return;
  }
  else {
    inFunction = true;
  }
}

这会产生竞争条件吗?

与通常驻留在线程堆栈(私有)上的自动局部变量不同,具有静态存储类的局部变量驻留在进程的数据段中,因此在执行给定函数的所有线程之间共享,因此您的代码包含竞争条件. 为了防止共享,您应该使用 OpenMP 的threadprivate构造:

void problemFunction() {
  static bool inFunction;
  #pragma omp threadprivate(inFunction)

  if ( inFunction == true ) {
    return;
  }
  else {
    inFunction = true;
  }
}

这会将变量的存储位置从数据段更改为所谓的线程本地存储 (TLS)。 保留原来的static语义,即在函数调用之间保留值,但现在每个线程都有自己的副本。 threadprivate构造应该总是变量声明之后。

是的,这将导致执行竞争(显然)。 为了避免,您可以使用thread_local (另见此处)变量(另外,您的函数似乎没有必要复杂):

void problemFunction() {
  static thread_local bool inFunction;
  inFunction = true;
}

当每个线程都有自己的变量inFunction 通过这种方式,您的函数适用于任何线程实现,而不仅仅是 OpenMP。

暂无
暂无

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

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