Skip to main content
2 of 2
C++ is not the Whitespace Programming Language
dyp
  • 39.4k
  • 13
  • 119
  • 190

c++11 Global initialization order and thread_local

Hi when running the following using gcc 4.8.1 when using the thread_local keyword the assertion is hit. When removing the thread_local the assert is not hit. Does anyone know why this is? There is some undefined global ordering but I would expect buf_ to have a valid address before assigning ptr_. Just remove the keyword thread_local and it works for me.

Output:

$ ./ThreadLocal Running Tester ThreadLocal: main.cpp:13: int main(): Assertion `buf == ptr' failed. Aborted (core dumped) Output when removing thread_local keyword Running Tester 

Test.hpp

 #include <iostream> #include <cassert> template <typename std::size_t N> struct Mem { Mem() noexcept: ptr_(buf_) {} char * getBuf() { return buf_; } char * getPtr() { return ptr_; } private: char buf_[N]; char * ptr_; }; template <typename std::size_t N> struct Tester { Tester() { std::cout << " Running Tester " << std::endl; } char * getPtr() { return _mem.getPtr(); } char * getBuf() { return _mem.getBuf(); } private: static thread_local Mem<N> _mem; }; 

main.cpp

#include <iostream> #include "Test.hpp" template <typename std::size_t N> thread_local Mem<N> Tester<N>::_mem; int main() { Tester<500> t; char * ptr = t.getPtr(); char * buf = t.getBuf(); assert( buf == ptr ); } 
bjackfly
  • 3.3k
  • 2
  • 30
  • 42