How to initialize thread local variable in c++? [duplicate]
You need to use lazy initialization.
myClass{public: static __thread int64_t m_minInt; static __thread bool m_minIntInitialized; static int64_t getMinInt();};__thread int64_t myClass::m_minInt;__thread bool myClass::m_minIntInitialized;int64_t myClass::getMinInt(){ if (!m_minIntInitialized) // note - this is (due to __thread) threadsafe { m_minIntInitialized = true; m_minInt = 100; } return m_minInt;}
m_minIntInitialized
is guaranteed to be zero.
In most cases (ELF specification) it is placed to .tbss section, which is zero-initialized.
For C++ - http://en.cppreference.com/w/cpp/language/initialization
For all other non-local static and thread-local variables, Zero initialization takes place. In practice, variables that are going to be zero-initialized are placed in the .bss segment of the program image, which occupies no space on disk, and is zeroed out by the OS when loading the program.