This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


2914. Unclear order of initialization of static and thread-local variables

Section: 6.9.3.2  [basic.start.static]     Status: review     Submitter: Brian Bi     Date: 2024-06-20

(From submission #559.)

Specification is missing that clarifies that non-local variables with static storage duration are initialized before those with thread storage duration.

Suggested resolution:

  1. Change in 6.9.3.2 [basic.start.static] paragraph 1 as follows:

    Variables with static storage duration are initialized as a consequence of program initiation. Variables with thread storage duration are initialized as a consequence of thread execution. If the program does not start a thread (6.9.2 [intro.multithread]) other than the main thread (6.9.3.1 [basic.start.main]) before the completion of all non-deferred (6.9.3.3 [basic.start.dynamic]) initialization of non-block variables with static storage duration, then all non-deferred initialization (if any) of non-block variables with static storage duration strongly happens before any non-deferred initialization of non-block variables with thread storage duration. Within each of these phases of initiation, initialization occurs as follows.
  2. Change in 6.9.3.3 [basic.start.dynamic] paragraph 5 as follows:

    It is implementation-defined whether the dynamic initialization of a non-block non-inline variable o with static storage duration is sequenced before the first statement of main or is deferred. If it is deferred, it strongly happens before any non-initialization odr-use of any non-inline function or non-inline variable defined in the same translation unit as the variable to be initialized o and any deferred initialiation of non-inline non-block variables with thread storage duration defined in the same translation unit as o. [ Footnote: ... ] It is implementation-defined in which threads and at which points in the program such deferred dynamic initialization occurs.