프로그래밍/c++

static thread_local 인스턴스의 소멸 시점

제페 2019. 3. 5. 18:45
반응형
#include <chrono>
#include <thread>
#include <iostream>

using namespace std;

class Foo
{
public:

Foo() {
cout << "call constructor" << endl;
}

~Foo() {

cout << "call destructor" << endl;
}
};

int main()
{
thread thrd{[]
{
static thread_local Foo foo;
this_thread::sleep_for(5s);
}};

cout << "before join" << endl;
thrd.join();
cout << "after join" << endl;

return 0;
}

스레드가 종료될 때 제거된다. 당연하게도.

반응형