The following code should be creating the in-class thread_local
only once, but it ends up initializing it on every access
#include <iostream>
#include <thread>
using std::cout;
using std::endl;
template <typename T>
class Something {
public:
struct TLBookkeeping {
TLBookkeeping() {
std::cout << "TLBookkeeping() " << std::this_thread::get_id() << std::endl;
}
};
static void foo();
static thread_local TLBookkeeping bookkeeping_;
};
template <typename T>
thread_local typename Something<T>::TLBookkeeping Something<T>::bookkeeping_;
template <typename T>
void Something<T>::foo() {
std::cout << &bookkeeping_ << std::endl;
std::cout << &bookkeeping_ << std::endl;
}
namespace {
struct Struct {};
}
int main() {
Something<Struct>::foo();
}
(https://wandbox.org/permlink/fgqCDHV0axKDRt89)
Interestingly the bug only shows when Struct
is in an anonymous namespace.
Does someone know if this is a clang bug (gcc does it right - https://wandbox.org/permlink/hsxRj8OdYbt4Eeck) or an inherently incorrect usage of thread_local
? If the latter, what is the correct usage? If the former, what exactly is the bug? And is this documented somewhere? Should we open a bug report?
When you find that a compiler causes runtime behavior which you are relatively certain is incorrect; and another popular compiler does not cause that same behavior - that is sufficient, IMHO, in order to file a bug against said compiler.
As @SamVarshavchik suggestions, you:
I've done just that, so now we have bug 42111.
At worst - it'll be marked as a dupe.
edit: It was not a dupe, and has just now (June 5th 2019) been fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With