gpt4 book ai didi

c++ - g++ 线程局部变量是否遵循默认初始化规则?

转载 作者:太空狗 更新时间:2023-10-29 22:53:04 26 4
gpt4 key购买 nike

使用 g++ 声明函数静态线程本地存储:

void f() {
static __thread somePodStruct thing;
...
}

我可以假设 thing 会被零初始化吗?

最佳答案

根据 GCC 文档:

http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html

In C++, if an initializer is present for a thread-local variable, it must be a constant-expression, as defined in 5.19.2 of the ANSI/ISO C++ standard.

所以你可以明确地将它设置为零。
因此,为了安全起见,没有任何假设的负面影响,您可以自己明确地进行零初始化。

关于c++ - g++ 线程局部变量是否遵循默认初始化规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3162528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com