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 );
}
It looks like a bug in GCC. Apparently Tester::_mem
is not being initialized at all. GCC 4.9.0 does the same, but clang 3.5.0 works fine
Making _mem
not dependent on template parameter makes GCC crash.
Finally, making Tester
a non-template class makes GCC work at last.