Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Static and thread_local initialization order

TL;DR The initialization of the variables a and b in your question are indeterminately sequenced in relation to each other. The initialization order is not guaranteed between them. The initiali...

posted 4y ago by Lundin‭  ·  edited 4y ago by Lundin‭

Answer
#2: Post edited by user avatar Lundin‭ · 2020-11-17T16:00:59Z (almost 4 years ago)
  • TL;DR
  • The variables `a` and `b` in your question are indeterminately sequenced in relation to each other. The initialization order is not guaranteed between them.
  • ---
  • The initialization rules of C++ are quite complex, especially past C++11. The relevant part would be C++11 3.6.2, "Initialization of non-local variables" which speaks of zero initialization, then constant initialization. The formal terms are defined as
  • > Together, zero-initialization and constant initialization are called _static initialization_; all other initialization
  • is _dynamic initialization_.
  • Your specific case sorts under dynamic initialization, even if the storage duration of the objects are static/thread storage. Static storage variables have the following rules, emphasis mine:
  • > **Dynamic initialization of a non-local variable with static storage duration** is either ordered or
  • unordered. Definitions of explicitly specialized class template static data members have ordered initialization.
  • Other class template static data members (i.e., implicitly or explicitly instantiated specializations) have
  • unordered initialization. **Other non-local variables with static storage duration have ordered initialization.
  • Variables with ordered initialization defined within a single translation unit shall be initialized in the order
  • of their definitions in the translation unit.**
  • Regarding thread storage, the same section continues, emphasis mine:
  • > If a program starts a thread (30.3), the subsequent initialization
  • of a variable is unsequenced with respect to the initialization of a variable defined in a different translation
  • unit. Otherwise, the initialization of a variable is indeterminately sequenced with respect to the initialization
  • of a variable defined in a different translation unit. **If a program starts a thread, the subsequent unordered
  • initialization of a variable is unsequenced with respect to every other dynamic initialization. Otherwise,
  • the unordered initialization of a variable is indeterminately sequenced with respect to every other dynamic
  • initialization.**
  • TL;DR
  • The initialization of the variables `a` and `b` in your question are indeterminately sequenced in relation to each other. The initialization order is not guaranteed between them.
  • ---
  • The initialization rules of C++ are quite complex, especially past C++11. The relevant part would be C++11 3.6.2, "Initialization of non-local variables" which speaks of zero initialization, then constant initialization. The formal terms are defined as
  • > Together, zero-initialization and constant initialization are called _static initialization_; all other initialization
  • is _dynamic initialization_.
  • Your specific case sorts under dynamic initialization, even if the storage duration of the objects are static/thread storage. Static storage variables have the following rules, emphasis mine:
  • > **Dynamic initialization of a non-local variable with static storage duration** is either ordered or
  • unordered. Definitions of explicitly specialized class template static data members have ordered initialization.
  • Other class template static data members (i.e., implicitly or explicitly instantiated specializations) have
  • unordered initialization. **Other non-local variables with static storage duration have ordered initialization.
  • Variables with ordered initialization defined within a single translation unit shall be initialized in the order
  • of their definitions in the translation unit.**
  • Regarding thread storage, the same section continues, emphasis mine:
  • > If a program starts a thread (30.3), the subsequent initialization
  • of a variable is unsequenced with respect to the initialization of a variable defined in a different translation
  • unit. Otherwise, the initialization of a variable is indeterminately sequenced with respect to the initialization
  • of a variable defined in a different translation unit. **If a program starts a thread, the subsequent unordered
  • initialization of a variable is unsequenced with respect to every other dynamic initialization. Otherwise,
  • the unordered initialization of a variable is indeterminately sequenced with respect to every other dynamic
  • initialization.**
#1: Initial revision by user avatar Lundin‭ · 2020-11-17T15:55:53Z (almost 4 years ago)
TL;DR

The variables `a` and `b` in your question are indeterminately sequenced in relation to each other. The initialization order is not guaranteed between them.

---

The initialization rules of C++ are quite complex, especially past C++11. The relevant part would be C++11 3.6.2, "Initialization of non-local variables" which speaks of zero initialization, then constant initialization. The formal terms are defined as

> Together, zero-initialization and constant initialization are called _static initialization_; all other initialization
is _dynamic initialization_.

Your specific case sorts under dynamic initialization, even if the storage duration of the objects are static/thread storage. Static storage variables have the following rules, emphasis mine:

> **Dynamic initialization of a non-local variable with static storage duration** is either ordered or
unordered. Definitions of explicitly specialized class template static data members have ordered initialization.
Other class template static data members (i.e., implicitly or explicitly instantiated specializations) have
unordered initialization. **Other non-local variables with static storage duration have ordered initialization.
Variables with ordered initialization defined within a single translation unit shall be initialized in the order
of their definitions in the translation unit.**

Regarding thread storage, the same section continues, emphasis mine:

> If a program starts a thread (30.3), the subsequent initialization
of a variable is unsequenced with respect to the initialization of a variable defined in a different translation
unit. Otherwise, the initialization of a variable is indeterminately sequenced with respect to the initialization
of a variable defined in a different translation unit. **If a program starts a thread, the subsequent unordered
initialization of a variable is unsequenced with respect to every other dynamic initialization. Otherwise,
the unordered initialization of a variable is indeterminately sequenced with respect to every other dynamic
initialization.**