@malibor, I removed my post before you replied, because I felt it wasn't entirely topical. I didn't mean to derail any discussion with you. However, I'm not certain what you're claiming about odr-usage, and I don't want to enter into another lengthy discussion about
constexpr on @helios's thread. Maybe we could start an independent thread?
If multiple threads attempt to initialize the same static local variable concurrently, the initialization occurs exactly once. |
Supposedly, yes, but repeating what's in the original post:
helios wrote: |
---|
However, because static locals need thread-safe initialization, the implementation decided to use TLS to prevent double initialization. Because of various reasons that are not relevant right now, the image's thread information wasn't fully initialized and TLS didn't work properly, so the function that needed that data produced incorrect results. |
so what was your point about static constexpr in your sample code? |
I was comparing @helios's original post with yours, which differs only in the use of
const vs.
constexpr.
So we can conclude that both your example code and my example code is not going to work for the problem helios described? [... Both static variables] will be initialized before function is first entered right? |
The rules are different for static local variables.
A static local that is not
constant-initialized is supposed to be initialized when control first reaches its declaration. A static local that is
constant-initialized is supposed to be initialized when control first reaches the innermost enclosing block scope containing the declaration.
A static member of a class template is not local; it's essentially an inline variable. It's initialized before
main is called when there's only one thread, and no risk of synchronization issues.