Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do g++ thread-local variables follow default initialization rules?

Using g++ to declare function-static thread-local storage:

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

can I assume that thing will get zero-initialized?

like image 583
Bob Lied Avatar asked Mar 13 '26 15:03

Bob Lied


1 Answers

According to the GCC documentation:

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.

So you can explicitly set it to zero.
So to be on the safe side with no down side of any assumptions you can get zero initialization be explicitly doing it yourself.

like image 118
Martin York Avatar answered Mar 15 '26 03:03

Martin York



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!