1

I have a parallel region, I have a static variable defined inside this region, like this

#pragma omp parallel
{
    static int var;
    // some stuff
}

Without threadprivate, static variables are shared between threads, so there is actually one instance seen by all the threads. This source says static initialization happens first and usually at compile time. What happens in this case? Does the compiler just ignore this parallel region for the static variable and treat it as if it had been placed outside the parallel region?

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • That 'source' is misleading. [This source](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf) (a Draft C++ Standard) says: *Dynamic initialization of a block-scope variable with static storage duration or thread storage duration is performed the first time control passes through its declaration.* – Adrian Mole May 03 '22 at 10:24
  • @AdrianMole doesn't control passing happen during compilation? – Kaiyakha May 03 '22 at 10:27
  • How can it? That 'control' a run-time thing. – Adrian Mole May 03 '22 at 10:28
  • @AdrianMole clear, and what about the question itself? – Kaiyakha May 03 '22 at 10:30
  • @AdrianMole I found [this](https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables) saying _If multiple threads attempt to initialize the same static local variable concurrently, the initialization occurs exactly once (similar behavior can be obtained for arbitrary functions with std::call_once)_. It is just sometimes you are not experienced enough to know what exactly to look for :) – Kaiyakha May 03 '22 at 10:37

0 Answers0