-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CWG2914 [basic.start.static] Unclear order of initialization of static and thread local variables #559
Comments
The suggested resolution seems too strong. In the case of deferred dynamic initialization, the dynamic initialization should not happen before the initialization of (at least some) thread storage duration variables. Consider the following example, where // TU A
int a = whatever();
thread_local int b = whatever();
void foo();
int main() {
foo();
}
// TU B
static int c = whatever();
void foo() { return c; } My understanding is that initializing |
OK, how about this. Edit [basic.start.static]/1:
Edit [basic.start.dynamic]/5:
|
Interestingly, static initialization is arguably "non-deferred", so the first paragraph would make static initialization of variables with static storage duration happen before static initialization of variables with thread storage duration. I don't think that's a problem, though? The second paragraph doesn't appear to ensure that deferred dynamic initialization of variables with thread storage duration happens before an odr-use of a function or variable in the same TU, but I think that's also a problem with the status quo. Otherwise, I think this looks fine? |
That might be what the status quo is trying to say anyway with its "within each of these phases" wording.
Deferred dynamic initialization of thread local variables is governed by p7. In p5, it's true that adding the suggested guarantee would possibly allow a function in the same TU to observe a thread local variable prior to its dynamic initialization. However, such a use of that function would not be a "non-initialization odr-use" because it would be caused by the initialization of a static variable, so it wouldn't violate p7. |
Ah, yes, sorry. I missed that paragraph. Looks fine then. |
Full name of submitter: Brian Bi
Issue description: It is heavily implied, but not stated outright, that initialization of all non-local static variables happens before initialization of all non-local thread-local variables. For example, [basic.start.static] seems to refer to initializing variables with static storage duration and thread storage duration as "phases", which may imply that one phase completes before the other starts. [basic.start.term]/2 appears to guarantee that returning from
main
destroys all thread-local variables of the main thread before destroying any static variables, which would be peculiar if the static variables were not initialized first.Suggested resolution: Edit [basic.start.static]/1 as shown:
The text was updated successfully, but these errors were encountered: