#include <stdio.h>
class C1
{
public:
C1() { printf("c1 cons\n"); }
~C1() { printf("c1 des\n"); }
};
class C2
{
public:
C2() { printf("c2 cons\n"); }
~C2() { printf("c2 des\n"); }
};
int main(int argc, const char *argv[])
{
thread_local C1 c1;
thread_local C2 c2; // 预期 c2 后构造,先析构。因为语义上 c2 有可能依赖 c1(例如 c1 是 c2 的构造参数)
return 0;
}
输出:
[xiaochu.yh test]$g++ test.cpp --std=c++11
[xiaochu.yh test]$./a.out
c1 cons
c2 cons
c2 des
c1 des