6

For example:

#include <thread>

thread_local int n = 1;

void f()
{
    ++n; // is n initialized here for each thread or prior to entering f()?
}

int main()
{
    std::thread ta(f);
    std::thread tb(f);

    ta.join();
    tb.join();
}

It's still not entirely clear from here when is n initialized.

Davit Tevanian
  • 861
  • 8
  • 16

1 Answers1

6

Simple enough, and all according to specification. n is going to be initialized whenever the new thread is run - before you enter any thread-specific functions.

To be exact, it is going to be initialized three times.

SergeyA
  • 61,605
  • 5
  • 78
  • 137