0

I have two variables. One is __thread the other is static __thread. If I had a constructor all I'd do is zero all the member variables. I also happen to use this class only outside of function bodies. Will my variables be zero initalized skipping the need for me to write a constructor?


To clarify. I have a class that is defined as __thread Foo foo and static __thread Foo foo. I don't mean I have a variable literally named __thread that I somehow tricked the compiler to let me use without naming a type. I'm positive static is zero initialized but IDK if __thread undoes it or if __thread without static will be zero initalized

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Eric Stotch
  • 141
  • 4
  • 19

1 Answers1

1

Thread-local variables are zero initialized before any other constructors. If you have no constructors, then only the zero initialization is done. In case of a class type, this means all members are set to zero.

Zero initialization is performed in the following situations:

  1. For every named variable with static or thread-local storage duration that is not subject to constant initialization, before any other initialization.

If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.

https://en.cppreference.com/w/cpp/language/zero_initialization

VLL
  • 9,634
  • 1
  • 29
  • 54