Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #81172
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Does static initialization of a local variable always happens synchronized ? |
| Date | 2021-09-11 16:02 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <shictj$1p7$1@dont-email.me> (permalink) |
| References | <shhrig$7fl$1@dont-email.me> |
Am 11.09.2021 um 11:06 schrieb Bonita Montero:
> Look at this code:
>
> #include <iostream>
>
> using namespace std;
>
> #if defined(_MSC_VER)
> #define NOINLINE __declspec(noinline)
> #elif defined(__GNUC__) || defined(__clang__)
> #define NOINLINE __attribute((noinline))
> #endif
>
> struct S
> {
> S();
> };
>
> NOINLINE
> S::S()
> {
> cout << "S::S()" << endl;
> }
>
> NOINLINE
> S &fn()
> {
> static
> S s;
> return s;
> }
>
> int main()
> {
> fn();
> fn();
> }
>
>
> I call fn twice, but of course s is initialized only once at the first
> call of fn. What I asked myself if this happens synchronized, i.e. if
> multiple threads try to initialize s, it's only initiailized only once
> in a locked context.
> With MSVC this happens as expected: before the initialization of s, fn
> calls _Init_thread_header and afterwards it calls _Init_thread_footer.
> These functions each call EnterCriticalSection or LeaveCriticalSection
> on a global mutex. But is this behaviour guaranteed for C++ in general ?
And MSVC of course uses double-checked locking here !
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