diff --git a/source/statements.tex b/source/statements.tex index 4f0c773c51..b0efdc2de9 100644 --- a/source/statements.tex +++ b/source/statements.tex @@ -1038,11 +1038,30 @@ the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not -complete, so it will be tried again the next time control enters the +complete, so it will be tried again the next time control passes through the declaration. -If control enters the declaration concurrently while the variable is -being initialized, the concurrent execution shall wait for completion +If control passes through the declaration concurrently while the variable is +being initialized, the concurrent execution waits for completion of the initialization. +\begin{example} +\begin{codeblock} +void f() { + static int x = 2; // OK + static std::string s = "hello"; // OK; if an exception is thrown during the + // initialization of \tcode{s}, \tcode{s} remains uninitialized, and + // an attempt to initialize \tcode{s} (but not \tcode{x}) will be + // made the next time \tcode{f} is called. + thread_local const char* w = "world"; // OK, initializes \tcode{w} to \tcode{"world"} on the main thread. + std::jthread([] { + thread_local int k = 4; + std::cout << k + x; // OK, prints \tcode{"6"}. + std::cout << s << ' '; // OK, prints \tcode{"hello "}. + std::cout << w; // Undefined behavior if \tcode{w} hasn't been constant initialized\iref{basic.start.static}. + // On this thread, control hasn't yet passed through + }); // the declaration of \tcode{w}. +} +\end{codeblock} +\end{example} \begin{note} A conforming implementation cannot introduce any deadlock around execution of the initializer.