thread-local static variable unnecessarily constructed again
| Bugzilla Link | 42111 |
| Resolution | FIXED |
| Resolved on | Jun 04, 2019 17:04 |
| Version | 8.0 |
| OS | Linux |
| CC | @dwblaikie,@DougGregor,@jyu2-git,@zygoloid |
Extended Description
Relevant StackOverflow question: https://stackoverflow.com/q/52471237/1593077
Consider the following program:
#include
using std::cout; using std::endl;
template
static void foo();
static thread_local TLBookkeeping bookkeeping_;
};
template
template
namespace { struct Struct {}; }
int main() {
Something
If we compile and run it, we get:
TLBookkeeping() 140713161373504 0x7ffa55ffb73f TLBookkeeping() 140713161373504 0x7ffa55ffb73f
i.e. the static TLBookeeping variable is constructed twice. IIANM, this shouldn't happen; and compiling with GCC doesn't cause this behavior.
Fixed in r362562.
Yes, because that thread local variable has internal linkage(eg. inside unnamed name space), thread not safe, need guard var for thread safe.
the initialization guard for globals with internal linkage.
I don't know what the "initialization guard" is, but ok. I'll also repeat the observation by @Curious in his Stackoverflow question: If you take struct Struct out of the anonymous namespace (give it external linkage?), the bug disappears.
We're optimizing away the initialization guard for globals with internal linkage. That's incorrect for thread_locals, because the initialization function is called once per use not once per definition.