Question
Does the behavior of the following code meet the standards? If so, why? When does the thread exit?
#include <iostream>
#include <thread>
struct tls_test {
tls_test()
{ std::cout << "tls_test ctor\n"; }
~tls_test()
{ std::cout << "tls_test dtor\n"; }
void print() const
{ std::cout << "tls_test print\n"; }
};
thread_local tls_test t;
void thread_1()
{
std::cout << "thread_1 started\n";
t.print();
std::cout << "thread_1 return\n";
}
int main()
{
std::thread trd{ thread_1 };
trd.join();
std::cout << "main return\n";
}
The given code produces the following output:
thread_1 started
tls_test ctor
tls_test print
thread_1 return
main return
According to [basic.stc.thread]/2, the variable t
is constructed and shall be destroyed on thread exit. Does this behavior meet the standards? If so, why? When does the thread exit?