Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #81181
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Does static initialization of a local variable always happens synchronized ? |
| Date | 2021-09-11 20:03 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <shir2b$b66$1@dont-email.me> (permalink) |
| References | <shhrig$7fl$1@dont-email.me> <shi50r$5vv$1@dont-email.me> <shidqb$7vq$1@dont-email.me> <shigrr$vah$1@dont-email.me> |
On 11/09/2021 17:09, Alf P. Steinbach wrote:
> On 11 Sep 2021 16:17, Bonita Montero wrote:
>> Am 11.09.2021 um 13:47 schrieb Alf P. Steinbach:
>>> On 11 Sep 2021 11:06, Bonita Montero wrote:
>>> Yes.
>>>
>>> C++ 17 §9.7/4 (next to last sentence applies):
>>> ❝Dynamic initialization of a block-scope variable with static storage
>>> duration or thread storage duration is performed 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 declaration. If control enters the declaration concurrently while
>>> the variable is being initialized, the concurrent execution shall
>>> wait for completion of the initialization. If control re-enters the
>>> declaration recursively while the variable is being initialized, the
>>> behavior is undefined.❞
>>
>> It couldn't be really undefined since the flag which remembers that
>> the object has been created successfully is set afterwards. Otherwise
>> it woud be impossible that the object will be tried to be re-created
>> if the constructor throws an exception and the function containing
>> the object will be called again. So an infinite recursion is the only
>> possible behaviour here.
>
> The standard says it's undefined behavior, hence it's undefined
> behavior. Really. So I believe that you meant to say that there can't be
> any other actual effect of this UB than infinite recursion.
>
> But of course there can be: formal UB means that the compiler is free to
> insert checking that does whatever the compiler writers want.
>
It is quite easy to see how you could get a deadlock from recursive use,
but not concurrent use. A typical initialisation sequence could be
roughly :
if (!initialised_flag) { // atomic flag
get_lock();
if (!initialised_flag) {
do_initialisation();
initialised_flag = true;
}
release_lock();
}
Calling this concurrently when the lock is taken, the second thread will
block on "get_lock()" until the first thread is finished, then see the
initialisation was done and go on its way. But calling it recursively,
the block at "get_lock()" will never finish because it is the same
thread that was supposed to be doing the initialisation.
On the other hand, the sequence could be:
if (!initialised_flag) {
initialise_temporary_object();
compare_and_swap(static_object, temporary_object, null);
initialised_flag = true;
}
That would be fine if there is no problem in accidentally creating extra
objects and discarding them. The compare and swap would avoid
overriding a previous initialisation. This would work fine with both
concurrent and recursive use.
So the standard says the behaviour is undefined on recursive use,
leaving the implementation freedom to pick either tactic (or other tactics).
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 11:06 +0200
Re: Does static initialization of a local variable always happens synchronized ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-11 13:47 +0200
Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 16:17 +0200
Re: Does static initialization of a local variable always happens synchronized ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-11 17:09 +0200
Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 17:18 +0200
Re: Does static initialization of a local variable always happens synchronized ? David Brown <david.brown@hesbynett.no> - 2021-09-11 20:03 +0200
Re: Does static initialization of a local variable always happens synchronized ? Paavo Helde <myfirstname@osa.pri.ee> - 2021-09-11 15:13 +0300
Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 16:02 +0200
Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-11 21:19 +0200
Re: Does static initialization of a local variable always happens synchronized ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2021-09-11 22:05 +0200
Re: Does static initialization of a local variable always happens synchronized ? Bonita Montero <Bonita.Montero@gmail.com> - 2021-09-12 08:24 +0200
csiph-web