相关疑难解决方法(0)

thread_local成员变量构造

我在使用thread_local时遇到了一些奇怪的行为,并且不确定我是做错了什么还是GCC错误.我有以下最小的repro场景:

#include <iostream>

using namespace std;

struct bar {
    struct foo {
        foo () {
            cerr << "foo" << endl;
        }
        int i = 42;
    };

    static thread_local foo FOO;
};

static thread_local bar::foo FREE_FOO;
thread_local bar::foo bar::FOO;

int main() {
    bar b;
    cerr << "main" << endl;
    // cerr << FREE_FOO.i << endl;
    cerr << b.FOO.i << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用上面的注释行,输出如下所示:

main
0
Run Code Online (Sandbox Code Playgroud)

Ideone

随着它取消注释,它变成了这样:

main
foo
foo
42
42
Run Code Online (Sandbox Code Playgroud)

Ideone

我只是在这里错过了一些愚蠢的东西吗?

$ gcc -v
Using built-in specs. …
Run Code Online (Sandbox Code Playgroud)

c++ construction thread-local c++11 gcc4.8

6
推荐指数
1
解决办法
4736
查看次数

标签 统计

c++ ×1

c++11 ×1

construction ×1

gcc4.8 ×1

thread-local ×1