简体   繁体   English

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

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

I am dealing with a large confusing codebase, and I have a situation like this.我正在处理一个大而混乱的代码库,我遇到了这样的情况。

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

void problemFunction() {

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

Would this create a race condition?这会产生竞争条件吗?

Unlike automatic local variables that usually reside on the thread's stack (which is private), local variables with static storage class reside in the data segment of the process and are thus shared between all threads executing the given function, therefore your code contains a race condition.与通常驻留在线程堆栈(私有)上的自动局部变量不同,具有静态存储类的局部变量驻留在进程的数据段中,因此在执行给定函数的所有线程之间共享,因此您的代码包含竞争条件. To prevent the sharing, you should use OpenMP's threadprivate construct:为了防止共享,您应该使用 OpenMP 的threadprivate构造:

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

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

This will alter the place the variable is stored in from the data segment to the so-called Thread-Local Storage (TLS).这会将变量的存储位置从数据段更改为所谓的线程本地存储 (TLS)。 The original static semantics are kept, ie the value is retained between the function calls, but now each thread has its own copy.保留原来的static语义,即在函数调用之间保留值,但现在每个线程都有自己的副本。 The threadprivate construct should always come after the variable declaration. threadprivate构造应该总是变量声明之后。

Yes, this will cause a race at execution (obviously).是的,这将导致执行竞争(显然)。 To avoid, you can use thread_local (see also here) variable (also, your function seems unnecessary complicated):为了避免,您可以使用thread_local (另见此处)变量(另外,您的函数似乎没有必要复杂):

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

when each thread has their own variable inFunction .当每个线程都有自己的变量inFunction This way your function works with any threading implementation, not just OpenMP.通过这种方式,您的函数适用于任何线程实现,而不仅仅是 OpenMP。

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

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