0

I have following code in one of the source file of my application:

// file1.cpp
#include <memory>

static auto global_variable = std::make_unique<int>(123);

int get_global_variable() { return *global_variable; }

Let assume that my application has some threads which call the get_global_variable. Is the initialization of the global_variable thread-safe?

As far as I know, the global_variable is dynamically initialized. I also know that the initialization of static local variables is thread-safe since C++11. So, I wonder to know if that exception proves the rule that the other types of variables are not thread-safe initialized or it is also thread-safe and does not produce data races.

I've found this answer, but after reading, I'm more confused because the answerer suggested using such pattern:

const T& f()
{
    static T t(a,b,c);
    return t;
}

which supposedly guarantees the thread-safe initialization.

I also found this answer. It states that all globals are initialized before main, so there is only one thread (Peter rightly pointed out that it is not true - or not every time). However, what if my piece of code is a shared library loaded by dlopen function to a program where there is more than one thread?

admo
  • 51
  • 1
  • 6
  • 3
    is this not answering your question https://stackoverflow.com/a/18543786/4117728 ? What specifically is confusing you? – 463035818_is_not_an_ai Jan 26 '21 at 09:55
  • 2
    Globals at file scope are not guaranteed by the standard to be initialised before `main()`. They are guaranteed to be initialised before the first usage in the same translation unit (aka source file). Practically, some implementations will initialise them before `main()`, but that is not required. The initialisation order of such globals in different translation units is also not guaranteed - and implementations that do initialise them before `main()` mostly don't guarantee a particular order between translation units. – Peter Jan 26 '21 at 10:41
  • @largest_prime_is_463035818 It is not answering my question, or I can't extract what I need. Just now, I realized that this question is about if the global initialization is single-threaded, but mine is more general: if the initialization of the global is thread-safe. – admo Jan 26 '21 at 10:53
  • @Peter, thanks. I knew that, and I edited my question:). Thanks to pointing it out. – admo Jan 26 '21 at 10:55
  • I think it is just a matter of wording, but now I understand what is confusing you – 463035818_is_not_an_ai Jan 26 '21 at 10:58

1 Answers1

2

[basic.start.dynamic]/4 It is implementation-defined whether the dynamic initialization of a non-local non-inline variable with static storage duration is sequenced before the first statement of main or is deferred. If it is deferred, it strongly happens before any non-initialization odr-use of any non-inline function or non-inline variable defined in the same translation unit as the variable to be initialized.

I believe this requires the implementation to perform initialization of global variables in a thread-safe manner (if it chooses to defer such initialization).

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85