Can I have thread_local object which ctor/dtor will be called on each thread creation even if it is not used?

Viewed 97

I'm still new to C++11 thread_local storage. In particular I'm curious about following code:

class foo_t
{
    foo_t() { std::cout << "Foo ctor"; }
    ~foo_t() { std::cout << "Foo dtor"; }
}

thread_local foo_t foo;

void bar() {  }
void baz() {  }
int main()
{
    std::thread t1(bar), t2(baz);
    t1.join(); t2.join();
}

Will the ctor/dtor of foo_t be called twice in this example (I mean does the standard requires so)? What if I move bar to other translation unit? What if such unit won't have a declaration of foo_t nor foo?

0 Answers
Related