Variables declared thread_local are unique for each thread. But do they consume memory if the function is not called?
Let's say I have a bunch of libraries that have thread_local variables in their functions. When I create a thread, are these variables going to be initialized even if I never call the functions that use them?
Example:
int lib1_foo()
{
thread_local int a, b;
// ...
}
int lib2_bar()
{
thread_local BigObject c;
// ...
}
int main()
{
std::thread t([]() {
// Do a, b, c consume memory?
// Are they initialized?
)();
t.join();
}
They certainly consume memory. cppreference has this to say (emphasis mine):
As for initialisation, it then goes on to say:
From which we get:
And: