For a better understanding of this question, here is the code:
// code 1
#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";
}
I'm using TDM-GCC and windows 10 to test this program. here is the output:
thread_1 started
tls_test ctor
tls_test print
thread_1 return
main return
According to basic.stc.thread, the variable t is constructed, shall be destroyed on thread exit. so I think that thread is not exit even the function thread_1
returns.
Does this behavior meet the standards? If so, why? When does the thread exit?
I also read thread.threads, but seems like the standard does not explain this.