Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"So why don't you make like a tree, and get outta here." -- Biff in "Back to the Future"


devel / comp.lang.c++ / Thread-safe initialization of static objects

SubjectAuthor
* Thread-safe initialization of static objectsBonita Montero
+* Re: Thread-safe initialization of static objectsPavel
|+* Re: Thread-safe initialization of static objectsBonita Montero
||+* Re: Thread-safe initialization of static objectsPavel
|||`* Re: Thread-safe initialization of static objectsBonita Montero
||| `* Re: Thread-safe initialization of static objectsPavel
|||  `* Re: Thread-safe initialization of static objectsBonita Montero
|||   +* Re: Thread-safe initialization of static objectsScott Lurndal
|||   |+* Re: Thread-safe initialization of static objectsRichard Damon
|||   ||`* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   || `* Re: Thread-safe initialization of static objectsRichard Damon
|||   ||  `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   ||   +* Re: Thread-safe initialization of static objectsRichard Damon
|||   ||   |`- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   ||   `* Re: Thread-safe initialization of static objectsScott Lurndal
|||   ||    `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   |`- Re: Thread-safe initialization of static objectsBonita Montero
|||   +* Re: Thread-safe initialization of static objectsPavel
|||   |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | +* Re: Thread-safe initialization of static objectsPavel
|||   | |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | +* Re: Thread-safe initialization of static objectsPaavo Helde
|||   | | |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | +* Re: Thread-safe initialization of static objectsPavel
|||   | | | |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | +* Re: Thread-safe initialization of static objectsRichard Damon
|||   | | | | |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | +* Re: Thread-safe initialization of static objectsRichard Damon
|||   | | | | | |+* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | ||+* Re: Thread-safe initialization of static objectsPaavo Helde
|||   | | | | | |||+* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | ||||+- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | | | | ||||`* Re: Thread-safe initialization of static objectsPaavo Helde
|||   | | | | | |||| +* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | |||| |+- Re: Thread-safe initialization of static objectsRichard Damon
|||   | | | | | |||| |`- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | | | | |||| `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | ||||  `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | ||||   `* Re: Thread-safe initialization of static objectsPaavo Helde
|||   | | | | | ||||    +- Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | ||||    `- Re: Thread-safe initialization of static objectsBonita Montero
|||   | | | | | |||`* Re: Thread-safe initialization of static objectsMichael S
|||   | | | | | ||| +- Re: Thread-safe initialization of static objectsScott Lurndal
|||   | | | | | ||| `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | | | | ||`* Re: Thread-safe initialization of static objectsRichard Damon
|||   | | | | | || `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | | | | |`* Re: Thread-safe initialization of static objectsPavel
|||   | | | | | | `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | | | | |  `* Re: Thread-safe initialization of static objectsPavel
|||   | | | | | |   `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | | | | |    `- Re: Thread-safe initialization of static objectsPavel
|||   | | | | | `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | | | `- Re: Thread-safe initialization of static objectsPavel
|||   | | | `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |  `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |   +* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |   |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |   | `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |   |  +- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |   |  `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |   |   +- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |   |   `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |   +- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |   `* Re: Thread-safe initialization of static objectsRichard Damon
|||   | | |    `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |     +- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |     `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |      `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |       `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |        `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |         `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |          `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |           `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |            `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |             `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |              +- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |              `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |               `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                 `* Re: Thread-safe initialization of static objectsScott Lurndal
|||   | | |                  +- Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                  `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                   `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                    `* Re: Thread-safe initialization of static objectsRichard Damon
|||   | | |                     `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                      `* Re: Thread-safe initialization of static objectsPavel
|||   | | |                       `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        +* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        | `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |  `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        |   +* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |   |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        |   | +* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |   | |`* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        |   | | `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |   | |  `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        |   | |   `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |   | |    `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        |   | |     `* Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |   | |      `* Re: Thread-safe initialization of static objectsBonita Montero
|||   | | |                        |   | `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | | |                        |   `* Re: Thread-safe initialization of static objectsKaz Kylheku
|||   | | |                        `* Re: Thread-safe initialization of static objectsPavel
|||   | | `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   | `- Re: Thread-safe initialization of static objectsChris M. Thomasson
|||   `- Re: Thread-safe initialization of static objectsMarcel Mueller
||`- Re: Thread-safe initialization of static objectsChris M. Thomasson
|`* Re: Thread-safe initialization of static objectsChris M. Thomasson
+* Re: Thread-safe initialization of static objectsPaavo Helde
+* Re: Thread-safe initialization of static objectsChris M. Thomasson
+* Re: Thread-safe initialization of static objectsChris M. Thomasson
`* Re: Thread-safe initialization of static objectsFrederick Virchanza Gotham

Pages:123456789101112131415161718192021222324252627
Re: Thread-safe initialization of static objects

<uegtd7$3dolc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1668&group=comp.lang.c%2B%2B#1668

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 10:03:52 +0200
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <uegtd7$3dolc$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udoico$1d0tr$1@dont-email.me>
<udotj5$1e8do$2@dont-email.me> <udpbun$1gf8c$1@dont-email.me>
<udqatg$1mar3$1@dont-email.me> <udqbg3$1mdv6$1@dont-email.me>
<udqq8u$1p3q6$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <ue4t6i$3um57$1@dont-email.me>
<ue5sp2$7ms9$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad>
<uegt3a$3dj0r$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 08:03:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3596972"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/G3evdPVgWzPaaGxFbBCrp1tYEnUmvSuQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:AZA13TxMjivPmxza1rdFH5kd0+k=
Content-Language: de-DE
In-Reply-To: <uegt3a$3dj0r$4@dont-email.me>
 by: Bonita Montero - Thu, 21 Sep 2023 08:03 UTC

Am 21.09.2023 um 09:58 schrieb Bonita Montero:

>> They are only set to zero if they are not initialized explicitly.

> I'm not talking about what the standad says but how it's implemented
> with all operating systems that support virtual memory.

This shows "all zero" on all modern operating systems:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{ static char mem[0x1000000];
bool zero = find_if( mem, end( mem ), []( char c ) -> bool { return c;
} ) == end( mem );
cout << (zero ? "all zero" : "has non-zero") << endl;
}

Re: Thread-safe initialization of static objects

<bd177a2a-9ffa-45bc-81d4-fe7a46b79024n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1669&group=comp.lang.c%2B%2B#1669

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:1aa1:b0:40f:e2a5:3100 with SMTP id s33-20020a05622a1aa100b0040fe2a53100mr82761qtc.6.1695283469842;
Thu, 21 Sep 2023 01:04:29 -0700 (PDT)
X-Received: by 2002:a4a:4f15:0:b0:573:540a:fe3b with SMTP id
c21-20020a4a4f15000000b00573540afe3bmr1546589oob.0.1695283469582; Thu, 21 Sep
2023 01:04:29 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 21 Sep 2023 01:04:29 -0700 (PDT)
In-Reply-To: <uegft8$3bf1v$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com> <ueff5s$32ncr$3@dont-email.me>
<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com> <uegft8$3bf1v$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bd177a2a-9ffa-45bc-81d4-fe7a46b79024n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 21 Sep 2023 08:04:29 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 24
 by: Michael S - Thu, 21 Sep 2023 08:04 UTC

On Thursday, September 21, 2023 at 7:13:45 AM UTC+3, Chris M. Thomasson wrote:
> On 9/20/2023 12:36 PM, Michael S wrote:
> > On Wednesday, September 20, 2023 at 9:55:09 PM UTC+3, Chris M. Thomasson wrote:
> >> On 9/20/2023 9:48 AM, Michael S wrote:
> >>> On Wednesday, September 20, 2023 at 4:18:42 PM UTC+3, Bonita Montero wrote:
> >>>> Am 20.09.2023 um 13:54 schrieb Richard Damon:
> [...]
> > It's unlikely to be unreliable in this particular circumstances.
> > But apart from it, my code has bug (race condition) :(
> > Just shows that this sort of crap is not easy.
> [...]
> Implement your algorithm in Relacy Race Detector. It can help. I know
> the guy who created it. A good guy.

I like my last version, the one that wakes up waiting threads in chained mannner
by means of QueueUserAPC(), better.
It's only disadvantage is higher memory footage - 6 bytes per guarded object instead of 1.
Can be reduced to 5 bytes, but in practice compiler will allocate 8 bytes anyway, so let it
be 6.

Re: Thread-safe initialization of static objects

<61effc74-6a97-442a-bc48-ff57df13f4c3n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1670&group=comp.lang.c%2B%2B#1670

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:620a:16b6:b0:76f:cd2:972f with SMTP id s22-20020a05620a16b600b0076f0cd2972fmr58848qkj.6.1695284640636;
Thu, 21 Sep 2023 01:24:00 -0700 (PDT)
X-Received: by 2002:a05:6830:4a6:b0:6bf:146a:b86 with SMTP id
l6-20020a05683004a600b006bf146a0b86mr1430075otd.3.1695284640327; Thu, 21 Sep
2023 01:24:00 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!border-2.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 21 Sep 2023 01:23:59 -0700 (PDT)
In-Reply-To: <20230920213647.190@kylheku.com>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com> <ueff5s$32ncr$3@dont-email.me>
<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com> <uegfqq$3bf1v$1@dont-email.me>
<20230920213647.190@kylheku.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <61effc74-6a97-442a-bc48-ff57df13f4c3n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 21 Sep 2023 08:24:00 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 41
 by: Michael S - Thu, 21 Sep 2023 08:23 UTC

On Thursday, September 21, 2023 at 7:46:33 AM UTC+3, Kaz Kylheku wrote:
> On 2023-09-21, Chris M. Thomasson <chris.m.t...@gmail.com> wrote:
> > On 9/20/2023 12:36 PM, Michael S wrote:
> > PulseEvent never worked right.
>
> PulseEvent might have worked under cooperative multitasking (i.e.
> up until Windows 3).
>

Did they have PulseEvent() before Win32 ?
I never studied Win16 API (preferred DOS up until Nt 4) and by now
I can't find Win16 API documentation.

> Under cooperative multitasking, effectively, the running thread holds a
> de-facto global mutex: one which prevents all other threads from
> running. Yielding is like giving up that mutex.
>
> Under cooperative multitasking, certain synchronization objects like
> events effectively behave like condition variables due to the implicit
> mutex.
>
> You know that the thread which calls PulseEvent is the only one running,
> and so after the operation, you know no new threads are waiting on the
> event; no thread gets to run until that thread yields.

It seems to me, co-operative multitasking by itself is not a significant
simplification. What simplifies things dramatically is co-operative multitasking
on single processor.
Single processor part appear more important than co-operative multitasking
part. Implementing PulseEvent() on single processor OS with preemptive
multitasking in user space, but non-preemptive kernel should be quite easy.

> --
> TXR Programming Language: http://nongnu.org/txr
> Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
> Mastodon: @Kazi...@mstdn.ca
> NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Thread-safe initialization of static objects

<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1671&group=comp.lang.c%2B%2B#1671

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:620a:1706:b0:773:f15d:3c07 with SMTP id az6-20020a05620a170600b00773f15d3c07mr71000qkb.3.1695285484212;
Thu, 21 Sep 2023 01:38:04 -0700 (PDT)
X-Received: by 2002:a05:6870:7696:b0:1d6:b110:2f1f with SMTP id
dx22-20020a056870769600b001d6b1102f1fmr1964574oab.8.1695285483785; Thu, 21
Sep 2023 01:38:03 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 21 Sep 2023 01:38:03 -0700 (PDT)
In-Reply-To: <uegt3a$3dj0r$4@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udafjf$2joeq$1@dont-email.me> <udnqhc$160ia$3@dont-email.me>
<udoico$1d0tr$1@dont-email.me> <udotj5$1e8do$2@dont-email.me>
<udpbun$1gf8c$1@dont-email.me> <udqatg$1mar3$1@dont-email.me>
<udqbg3$1mdv6$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 21 Sep 2023 08:38:04 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3618
 by: Michael S - Thu, 21 Sep 2023 08:38 UTC

On Thursday, September 21, 2023 at 10:58:51 AM UTC+3, Bonita Montero wrote:
> Am 21.09.2023 um 02:22 schrieb Pavel:
>
> > You did not even show that they use mutex at all ...
>
> This doesn't matter, because it's clear that static initialization
> can't work any other way than with a mutex. Yielding is inacceptable
> and spinlocking is inacceptable for reasons I've describet multiple
> times.

Actually, considering that contention is very rare in practice, spinning
with Sleep(1) in the loop body will work adequately, if not ideally.
The rest can be implemented with InterlockedCompareExchange().

So even use of Futex | Critical Section | SRW lock, although it make
a lot of sense in practice, is not necessary from theoretical P.O.V.

> > This is irrelevant to the discussion, but still is wrong:
> There's a non-initialized BSS segment with every executable and every
> shared library. This isn't physically part of the executable file but
> there's only a declared size to reserve. This block is initially com-
> pletely zero. This helps the C++ implementation since every static
> local object has initially an initialized-flag which is zero. This
> is stored along with the object itself.
> > They are only set to zero if they are not initialized explicitly.
> I'm not talking about what the standad says but how it's implemented
> with all operating systems that support virtual memory.

Re: Thread-safe initialization of static objects

<ueh0rk$3ebkg$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1672&group=comp.lang.c%2B%2B#1672

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 11:02:44 +0200
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <ueh0rk$3ebkg$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udpbun$1gf8c$1@dont-email.me>
<udqatg$1mar3$1@dont-email.me> <udqbg3$1mdv6$1@dont-email.me>
<udqq8u$1p3q6$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <ue4t6i$3um57$1@dont-email.me>
<ue5sp2$7ms9$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad>
<uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 09:02:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3616400"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/9W7o7PFdKs2FLTIwyvDGwdt1S+w3Kp8o="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/fPt79UZN9L9LMf9AlxdBaq+ttY=
In-Reply-To: <5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 09:02 UTC

Am 21.09.2023 um 10:38 schrieb Michael S:

> Actually, considering that contention is very rare in practice, ...

The initialization might last an arbitrary time, so spinning
is inacceptable.

> So even use of Futex | Critical Section | SRW lock, although it make
> a lot of sense in practice, is not necessary from theoretical P.O.V.

Why should a SRW-lock make sense here ? If the object isn't initialized
iz can't be read meanwhile. And a futex isn't a replacement for a mutex
but just a faster slow path for a mutex.

Re: Thread-safe initialization of static objects

<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1673&group=comp.lang.c%2B%2B#1673

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:1e98:b0:417:a501:49e with SMTP id bz24-20020a05622a1e9800b00417a501049emr143237qtb.4.1695290392715;
Thu, 21 Sep 2023 02:59:52 -0700 (PDT)
X-Received: by 2002:a05:6870:9550:b0:1c5:538a:d34a with SMTP id
v16-20020a056870955000b001c5538ad34amr2084185oal.11.1695290392497; Thu, 21
Sep 2023 02:59:52 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 21 Sep 2023 02:59:52 -0700 (PDT)
In-Reply-To: <ueh0rk$3ebkg$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udafjf$2joeq$1@dont-email.me> <udpbun$1gf8c$1@dont-email.me>
<udqatg$1mar3$1@dont-email.me> <udqbg3$1mdv6$1@dont-email.me>
<udqq8u$1p3q6$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <ue4t6i$3um57$1@dont-email.me>
<ue5sp2$7ms9$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad>
<uegt3a$3dj0r$4@dont-email.me> <5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
<ueh0rk$3ebkg$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 21 Sep 2023 09:59:52 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 36
 by: Michael S - Thu, 21 Sep 2023 09:59 UTC

On Thursday, September 21, 2023 at 12:03:01 PM UTC+3, Bonita Montero wrote:
> Am 21.09.2023 um 10:38 schrieb Michael S:
>
> > Actually, considering that contention is very rare in practice, ...
>
> The initialization might last an arbitrary time, so spinning
> is inacceptable.

Why is it unacceptable? The impact of needless wake up of the waiting thread
once per tick is negligible in terms of system performance and not too
horrible in terms power consumption. The impact of needless sleep till the
next tick is also insignificant under assumption that initialization took long time.

I'd call it non-ideal when better solutions available, but perfectly acceptable otherwise.

> > So even use of Futex | Critical Section | SRW lock, although it make
> > a lot of sense in practice, is not necessary from theoretical P.O.V.
> Why should a SRW-lock make sense here ?

Because SRW lock in exclusive mode is better critical section than critical
section. On newer versions of Windows critical sections exist purely for
backward compatibility.
That's what I was told unofficially by knowledgeable Microsoft guy.

> If the object isn't initialized
> iz can't be read meanwhile. And a futex isn't a replacement for a mutex
> but just a faster slow path for a mutex.

Are you sure?
My impression was that fast path (uncontended case) is also faster due
to absence of syscall overhead.

Re: Thread-safe initialization of static objects

<ueh8s7$3gh0c$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1674&group=comp.lang.c%2B%2B#1674

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 13:19:34 +0200
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <ueh8s7$3gh0c$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
<ueh0rk$3ebkg$1@dont-email.me>
<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 21 Sep 2023 11:19:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2c607c57714c305f205dd4c1f27e305a";
logging-data="3687436"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QBCeJL2R5y88Xa2LLKZM98nwZwG74ZaU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:ivxiRKztfEIpIeMG8PNmyGP0/OE=
Content-Language: en-GB
In-Reply-To: <3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
 by: David Brown - Thu, 21 Sep 2023 11:19 UTC

On 21/09/2023 11:59, Michael S wrote:
> On Thursday, September 21, 2023 at 12:03:01 PM UTC+3, Bonita Montero wrote:
>> Am 21.09.2023 um 10:38 schrieb Michael S:
>>
>>> Actually, considering that contention is very rare in practice, ...
>>
>> The initialization might last an arbitrary time, so spinning
>> is inacceptable.
>
> Why is it unacceptable? The impact of needless wake up of the waiting thread
> once per tick is negligible in terms of system performance and not too
> horrible in terms power consumption. The impact of needless sleep till the
> next tick is also insignificant under assumption that initialization took long time.
>
> I'd call it non-ideal when better solutions available, but perfectly acceptable otherwise.
>

I don't know why Bonita dislikes spinning and sleeping, but I know why
/I/ don't like it as a solution. Sleeping does not affect thread
priority in the way mutexes do. That means if one low priority thread
starts the initialisation and takes the lock, then gets pre-empted by a
higher priority thread that tries to take the lock, your whole system
can deadlock. (The high priority thread may be sleeping, but there may
be a mid-priority thread that can run and blocks the low-priority thread
from running.)

This is going to be a more likely failure situation if you have fewer
(or only one) cores. But the same compilers and toolchains are used for
a wide variety of systems - you can't pick a solution that might fail on
some systems. And note that these kinds of failures never turn up in
testing - they are rare, and only happen at the worst possible moment
after deployment.

This is particularly a risk for real-time systems and embedded systems,
where you are more likely to have fewer cores (perhaps only one), or
maybe some of your cores are dedicated to real-time tasks and
unavailable for other threads. You will also likely have high priority
and real-time priority threads, which will block the low-priority
threads. And on such systems there might be very serious costs or
safety risks involved in system hangs - you want to be sure that they
are not possible, to the best of practical abilities, rather than
designing in a solution that has a definite non-zero chance of failure.

Using mutexes avoids this problem, because if a high priority thread is
waiting on a mutex held by a low priority thread, the holder's priority
gets boosted to that of the high priority thread until it releases the lock.

Spinning should only be used when you have full control of the
priorities of threads that may take the spin lock, and can be sure it is
safe.

Re: Thread-safe initialization of static objects

<DUVOM.87557$noZ7.33800@fx13.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1675&group=comp.lang.c%2B%2B#1675

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx13.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
Content-Language: en-US
In-Reply-To: <uege3r$3bfsj$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 29
Message-ID: <DUVOM.87557$noZ7.33800@fx13.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Thu, 21 Sep 2023 07:36:03 -0400
X-Received-Bytes: 2717
 by: Richard Damon - Thu, 21 Sep 2023 11:36 UTC

On 9/20/23 11:42 PM, Bonita Montero wrote:
> Am 21.09.2023 um 00:46 schrieb Richard Damon:
>
>> It likely just needs to "Yield" (like call SwitchToThread) to let
>> other threads get a chance to run, and then check the initialization
>> status again.
>
> A yield yould be to inefficient.

Why?

You can't do anything until the object you are waiting to be initialized
gets initialized, so your "efficiency" isn't important.

Remember, the ONLY reason you need to wait is because you came to an
object that is actively in the process of being initialized, so a fairly
rare condition.

If it hasn't been initialized yet, you don't wait, you just claim the
initialization and go, or if the objects initialization is complete, you
just use it.

Also, Yield can't be less efficient than blocking on a Mutex, as the
implementation of the mutex blocking needs to yield too.

As has elsewhere been mentioned, you typically wait a little bit in a
spin wait to see if it will be quick or slow, so "Yield" is only done on
the "Slow" path, and it must be ok to be slow on the slow path, as it is
the slow path.

Re: Thread-safe initialization of static objects

<r_VOM.87558$noZ7.69799@fx13.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1676&group=comp.lang.c%2B%2B#1676

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx13.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Thread-safe initialization of static objects
Content-Language: en-US
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <udotj5$1e8do$2@dont-email.me>
<udpbun$1gf8c$1@dont-email.me> <udqatg$1mar3$1@dont-email.me>
<udqbg3$1mdv6$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me>
<uegtd7$3dolc$1@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <uegtd7$3dolc$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 32
Message-ID: <r_VOM.87558$noZ7.69799@fx13.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Thu, 21 Sep 2023 07:42:14 -0400
X-Received-Bytes: 2912
 by: Richard Damon - Thu, 21 Sep 2023 11:42 UTC

On 9/21/23 4:03 AM, Bonita Montero wrote:
> Am 21.09.2023 um 09:58 schrieb Bonita Montero:
>
>>> They are only set to zero if they are not initialized explicitly.
>
>> I'm not talking about what the standad says but how it's implemented
>> with all operating systems that support virtual memory.
>
> This shows "all zero" on all modern operating systems:
>
> #include <iostream>
> #include <algorithm>
>
> using namespace std;
>
> int main()
> {
>     static char mem[0x1000000];
>     bool zero = find_if( mem, end( mem ), []( char c ) -> bool {
> return c; } ) == end( mem );
>     cout << (zero ? "all zero" : "has non-zero") << endl;
> }

As it must by the standard.

Note, depending on the OS and implementation, that zeroing will either
be done by having the loaded program image just contain a great big
block of zeros, or (to save image size at the cost of CPU time) the
system might just put "static" objects that need to be zero initialized
into one common segment and zero-fill as an extension of the loading
process, or in some cases the OS can just promise that new segments for
the program are just always zero-filled.

Re: Thread-safe initialization of static objects

<bed7f83a-18a0-4b4a-bb8c-cb8ff4a72ab4n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1677&group=comp.lang.c%2B%2B#1677

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:4354:0:b0:63c:edce:c71e with SMTP id q20-20020ad44354000000b0063cedcec71emr140346qvs.3.1695297700515;
Thu, 21 Sep 2023 05:01:40 -0700 (PDT)
X-Received: by 2002:a05:6870:8c2f:b0:1d6:9bd7:d849 with SMTP id
ec47-20020a0568708c2f00b001d69bd7d849mr2033944oab.11.1695297700250; Thu, 21
Sep 2023 05:01:40 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 21 Sep 2023 05:01:39 -0700 (PDT)
In-Reply-To: <ueh8s7$3gh0c$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udafjf$2joeq$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com> <ueh0rk$3ebkg$1@dont-email.me>
<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com> <ueh8s7$3gh0c$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bed7f83a-18a0-4b4a-bb8c-cb8ff4a72ab4n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 21 Sep 2023 12:01:40 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 6386
 by: Michael S - Thu, 21 Sep 2023 12:01 UTC

On Thursday, September 21, 2023 at 2:19:59 PM UTC+3, David Brown wrote:
> On 21/09/2023 11:59, Michael S wrote:
> > On Thursday, September 21, 2023 at 12:03:01 PM UTC+3, Bonita Montero wrote:
> >> Am 21.09.2023 um 10:38 schrieb Michael S:
> >>
> >>> Actually, considering that contention is very rare in practice, ...
> >>
> >> The initialization might last an arbitrary time, so spinning
> >> is inacceptable.
> >
> > Why is it unacceptable? The impact of needless wake up of the waiting thread
> > once per tick is negligible in terms of system performance and not too
> > horrible in terms power consumption. The impact of needless sleep till the
> > next tick is also insignificant under assumption that initialization took long time.
> >
> > I'd call it non-ideal when better solutions available, but perfectly acceptable otherwise.
> >
> I don't know why Bonita dislikes spinning and sleeping, but I know why
> /I/ don't like it as a solution. Sleeping does not affect thread
> priority in the way mutexes do. That means if one low priority thread
> starts the initialisation and takes the lock, then gets pre-empted by a
> higher priority thread that tries to take the lock, your whole system
> can deadlock. (The high priority thread may be sleeping, but there may
> be a mid-priority thread that can run and blocks the low-priority thread
> from running.)
>
> This is going to be a more likely failure situation if you have fewer
> (or only one) cores. But the same compilers and toolchains are used for
> a wide variety of systems - you can't pick a solution that might fail on
> some systems. And note that these kinds of failures never turn up in
> testing - they are rare, and only happen at the worst possible moment
> after deployment.
>
> This is particularly a risk for real-time systems and embedded systems,
> where you are more likely to have fewer cores (perhaps only one), or
> maybe some of your cores are dedicated to real-time tasks and
> unavailable for other threads. You will also likely have high priority
> and real-time priority threads, which will block the low-priority
> threads. And on such systems there might be very serious costs or
> safety risks involved in system hangs - you want to be sure that they
> are not possible, to the best of practical abilities, rather than
> designing in a solution that has a definite non-zero chance of failure.
>
> Using mutexes avoids this problem, because if a high priority thread is
> waiting on a mutex held by a low priority thread, the holder's priority
> gets boosted to that of the high priority thread until it releases the lock.
>
> Spinning should only be used when you have full control of the
> priorities of threads that may take the spin lock, and can be sure it is
> safe.

The problem you mention does not apply to general-purpose systems
like Windows, Linux, BSD-linage Unixes, Solaris e.t.c. They all have
built-in avoidance of deadlocks caused by priority-inversion. Most
commonly it's done by applying random priority boosts.

Also I don't see how mutex semantics can possibly help. The common
problem scenario is that low-priority thread that is doing initialization is
preempted when it *does not* hold the mutex. The mutex is held for
two very short durations of flags update. The preemption is far more
likely to happen during the middle phase - constructor itself.

On real-time system with 1 or 2 processors you probably want
completely different design in which a mutex is held during all duration
of initialization, but you can't expect such solution to be included
in g++/clang++/MSVC compiler support libraries.

Which again brings us to the point made by myself and few others:
don't do it! Don't use function-local static objects with constructors.
Or, at least don't use them in multitasking scenarios. Compiler's one
time initialization infrastructure can be super-robust, but being generic
it's unlikely to be optimal answer in any concrete scenario.

Re: Thread-safe initialization of static objects

<uehd9e$3h8me$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1678&group=comp.lang.c%2B%2B#1678

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 14:34:55 +0200
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <uehd9e$3h8me$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
<ueh0rk$3ebkg$1@dont-email.me>
<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 12:34:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3711694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8r9h/Iz44iXcGL6kHXfshamknC9V77mQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:T3myFndUBW2+NuRNaMVwO+9M1b4=
Content-Language: de-DE
In-Reply-To: <3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
 by: Bonita Montero - Thu, 21 Sep 2023 12:34 UTC

Am 21.09.2023 um 11:59 schrieb Michael S:

> Why is it unacceptable? ...

Because the initializing thread might be scheduled away, thereby
keeping other threads spinning.

> I'd call it non-ideal when better solutions available, but perfectly acceptable otherwise.

It's inacceptable.

> Because SRW lock in exclusive mode is better critical section than critical
> section. ...

No, if you only lock exclusively there's no difference.

> Are you sure?

The fast path of mutexes is fast anyway, there's no need for further
speed ups. The futex accellerates the slow path and is a replacement
for the binary semaphore attached to a mutex.

Re: Thread-safe initialization of static objects

<uehdf3$3h8me$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1679&group=comp.lang.c%2B%2B#1679

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 14:37:56 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uehdf3$3h8me$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <ue4t6i$3um57$1@dont-email.me>
<ue5sp2$7ms9$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad>
<uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
<ueh0rk$3ebkg$1@dont-email.me>
<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
<ueh8s7$3gh0c$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 12:37:55 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3711694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18JMFDr50TH5QbYSmMvXD4fGNtWN5R57jc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:CQPtSoAEJ9pcjeN2GXnGKxVywZc=
Content-Language: de-DE
In-Reply-To: <ueh8s7$3gh0c$1@dont-email.me>
 by: Bonita Montero - Thu, 21 Sep 2023 12:37 UTC

Am 21.09.2023 um 13:19 schrieb David Brown:

> I don't know why Bonita dislikes spinning and sleeping, ...

I only dislike spinning in userspace except when there's a
limit to go for the slow path. The thread holding a spinlock
can be scheduled away an arbitrary time, that's unacceptable.

Re: Thread-safe initialization of static objects

<uehdjj$3h8me$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1680&group=comp.lang.c%2B%2B#1680

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 14:40:19 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uehdjj$3h8me$3@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<DUVOM.87557$noZ7.33800@fx13.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 12:40:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3711694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+iDW5nb2l4DYEENy+WAZPZsXx2zgnUR6Y="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ExFbYeihzedyrAOuY6wp5wfxPs8=
In-Reply-To: <DUVOM.87557$noZ7.33800@fx13.iad>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 12:40 UTC

Am 21.09.2023 um 13:36 schrieb Richard Damon:

> Why?

Because the initialization might only take a microsecond or less
and a yield is a full timeslice, on Linux and Windows usually one
millisecond.

Re: Thread-safe initialization of static objects

<ueheuu$3hkm3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1681&group=comp.lang.c%2B%2B#1681

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 15:03:25 +0200
Organization: A noiseless patient Spider
Lines: 111
Message-ID: <ueheuu$3hkm3$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
<ueh0rk$3ebkg$1@dont-email.me>
<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
<ueh8s7$3gh0c$1@dont-email.me>
<bed7f83a-18a0-4b4a-bb8c-cb8ff4a72ab4n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 21 Sep 2023 13:03:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2c607c57714c305f205dd4c1f27e305a";
logging-data="3723971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+p2YsUezbCf7Pd8mImcUMAZ2RcminA7iA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:1/i0n+1iIQy1IJAs6G6sUU39VRI=
In-Reply-To: <bed7f83a-18a0-4b4a-bb8c-cb8ff4a72ab4n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Thu, 21 Sep 2023 13:03 UTC

On 21/09/2023 14:01, Michael S wrote:
> On Thursday, September 21, 2023 at 2:19:59 PM UTC+3, David Brown wrote:
>> On 21/09/2023 11:59, Michael S wrote:
>>> On Thursday, September 21, 2023 at 12:03:01 PM UTC+3, Bonita Montero wrote:
>>>> Am 21.09.2023 um 10:38 schrieb Michael S:
>>>>
>>>>> Actually, considering that contention is very rare in practice, ...
>>>>
>>>> The initialization might last an arbitrary time, so spinning
>>>> is inacceptable.
>>>
>>> Why is it unacceptable? The impact of needless wake up of the waiting thread
>>> once per tick is negligible in terms of system performance and not too
>>> horrible in terms power consumption. The impact of needless sleep till the
>>> next tick is also insignificant under assumption that initialization took long time.
>>>
>>> I'd call it non-ideal when better solutions available, but perfectly acceptable otherwise.
>>>
>> I don't know why Bonita dislikes spinning and sleeping, but I know why
>> /I/ don't like it as a solution. Sleeping does not affect thread
>> priority in the way mutexes do. That means if one low priority thread
>> starts the initialisation and takes the lock, then gets pre-empted by a
>> higher priority thread that tries to take the lock, your whole system
>> can deadlock. (The high priority thread may be sleeping, but there may
>> be a mid-priority thread that can run and blocks the low-priority thread
>> from running.)
>>
>> This is going to be a more likely failure situation if you have fewer
>> (or only one) cores. But the same compilers and toolchains are used for
>> a wide variety of systems - you can't pick a solution that might fail on
>> some systems. And note that these kinds of failures never turn up in
>> testing - they are rare, and only happen at the worst possible moment
>> after deployment.
>>
>> This is particularly a risk for real-time systems and embedded systems,
>> where you are more likely to have fewer cores (perhaps only one), or
>> maybe some of your cores are dedicated to real-time tasks and
>> unavailable for other threads. You will also likely have high priority
>> and real-time priority threads, which will block the low-priority
>> threads. And on such systems there might be very serious costs or
>> safety risks involved in system hangs - you want to be sure that they
>> are not possible, to the best of practical abilities, rather than
>> designing in a solution that has a definite non-zero chance of failure.
>>
>> Using mutexes avoids this problem, because if a high priority thread is
>> waiting on a mutex held by a low priority thread, the holder's priority
>> gets boosted to that of the high priority thread until it releases the lock.
>>
>> Spinning should only be used when you have full control of the
>> priorities of threads that may take the spin lock, and can be sure it is
>> safe.
>
> The problem you mention does not apply to general-purpose systems
> like Windows, Linux, BSD-linage Unixes, Solaris e.t.c. They all have
> built-in avoidance of deadlocks caused by priority-inversion. Most
> commonly it's done by applying random priority boosts.

As far as I know, such random priority boosts will not boost a
non-realtime priority thread to real-time priority on Linux (and
presumably not on other systems). That would negate the whole concept
of realtime priority threads.

>
> Also I don't see how mutex semantics can possibly help. The common
> problem scenario is that low-priority thread that is doing initialization is
> preempted when it *does not* hold the mutex. The mutex is held for
> two very short durations of flags update. The preemption is far more
> likely to happen during the middle phase - constructor itself.
>
> On real-time system with 1 or 2 processors you probably want
> completely different design in which a mutex is held during all duration
> of initialization, but you can't expect such solution to be included
> in g++/clang++/MSVC compiler support libraries.
>

Yes, you would need the mutex to be held during critical stages of the
initialisation.

I can understand that this would be expensive compared to spinlocks that
would work fine for "normal" systems (especially with the random boosts
you described). And I understand that toolchains have to be optimised
for normal situations, not things that are extremely rare even in
particular niche situations.

But getting threading, locking and synchronisation details right is very
difficult - most programmers don't get it right. But you can't tell
that you have a problem by testing the code, as failures typically
require extreme bad luck in timing. So it always worries me when I see
something that is hidden, which most programmers will assume "just
works, by some compiler magic", but which can be a problem in some
circumstances.

(As another example, gcc's "libatomic" uses spinlocks - if you use
std::atomic types on a microcontroller in situations where atomics would
be useful, they will hang your system any time there is a coincidental
access.)

> Which again brings us to the point made by myself and few others:
> don't do it! Don't use function-local static objects with constructors.
> Or, at least don't use them in multitasking scenarios. Compiler's one
> time initialization infrastructure can be super-robust, but being generic
> it's unlikely to be optimal answer in any concrete scenario.
>

Yes, that is one option. You can also use "constinit" statics safely.
And gcc has a "-fno-threadsafe-statics" option (also useable as a
pragma, but unfortunately not as a neater __attribute__) that puts the
responsibility of ensuring things are threadsafe onto the user.

Re: Thread-safe initialization of static objects

<uehf2o$3hkm3$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1682&group=comp.lang.c%2B%2B#1682

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 15:05:27 +0200
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <uehf2o$3hkm3$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<DUVOM.87557$noZ7.33800@fx13.iad> <uehdjj$3h8me$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 13:05:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2c607c57714c305f205dd4c1f27e305a";
logging-data="3723971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Cqj6sMLce7bQUpKBVOIKr++Nlgxor7Bs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:QtlZGJ+1lvaBklv5CyX8Rqjbt6w=
In-Reply-To: <uehdjj$3h8me$3@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 21 Sep 2023 13:05 UTC

On 21/09/2023 14:40, Bonita Montero wrote:
> Am 21.09.2023 um 13:36 schrieb Richard Damon:
>
>> Why?
>
> Because the initialization might only take a microsecond or less
> and a yield is a full timeslice, on Linux and Windows usually one
> millisecond.

"Yield" won't take a timeslice unless there is something else of the
same priority, waiting to run on the same core. And if that's the case,
then fine - that thread is clearly equally important.

Re: Thread-safe initialization of static objects

<uehj9d$3idng$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1683&group=comp.lang.c%2B%2B#1683

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 16:17:17 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uehj9d$3idng$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<DUVOM.87557$noZ7.33800@fx13.iad> <uehdjj$3h8me$3@dont-email.me>
<uehf2o$3hkm3$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 14:17:17 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3749616"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KWVAtYMASFbUGYDykhqxdGeO9OUpy6NE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:MBCmm7mTgLNPpgg7Yi4OlvOT6xE=
In-Reply-To: <uehf2o$3hkm3$2@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 14:17 UTC

Am 21.09.2023 um 15:05 schrieb David Brown:

> "Yield" won't take a timeslice unless there is something else of the
> same priority, waiting to run on the same core. ...

Ok, you're right, but the code would be still unacceptable under load.

Re: Thread-safe initialization of static objects

<bbde3ce4-b969-431e-a3f6-d0b5ec6aafacn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1684&group=comp.lang.c%2B%2B#1684

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:622a:1a16:b0:410:6ea4:9662 with SMTP id f22-20020a05622a1a1600b004106ea49662mr68802qtb.2.1695306641607;
Thu, 21 Sep 2023 07:30:41 -0700 (PDT)
X-Received: by 2002:a05:6808:189b:b0:3a1:f2a4:3d7 with SMTP id
bi27-20020a056808189b00b003a1f2a403d7mr2997461oib.1.1695306641393; Thu, 21
Sep 2023 07:30:41 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 21 Sep 2023 07:30:40 -0700 (PDT)
In-Reply-To: <ueheuu$3hkm3$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udafjf$2joeq$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com> <ueh0rk$3ebkg$1@dont-email.me>
<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com> <ueh8s7$3gh0c$1@dont-email.me>
<bed7f83a-18a0-4b4a-bb8c-cb8ff4a72ab4n@googlegroups.com> <ueheuu$3hkm3$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bbde3ce4-b969-431e-a3f6-d0b5ec6aafacn@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 21 Sep 2023 14:30:41 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 187
 by: Michael S - Thu, 21 Sep 2023 14:30 UTC

On Thursday, September 21, 2023 at 4:03:46 PM UTC+3, David Brown wrote:
> On 21/09/2023 14:01, Michael S wrote:
> > On Thursday, September 21, 2023 at 2:19:59 PM UTC+3, David Brown wrote:
> >> On 21/09/2023 11:59, Michael S wrote:
> >>> On Thursday, September 21, 2023 at 12:03:01 PM UTC+3, Bonita Montero wrote:
> >>>> Am 21.09.2023 um 10:38 schrieb Michael S:
> >>>>
> >>>>> Actually, considering that contention is very rare in practice, ...
> >>>>
> >>>> The initialization might last an arbitrary time, so spinning
> >>>> is inacceptable.
> >>>
> >>> Why is it unacceptable? The impact of needless wake up of the waiting thread
> >>> once per tick is negligible in terms of system performance and not too
> >>> horrible in terms power consumption. The impact of needless sleep till the
> >>> next tick is also insignificant under assumption that initialization took long time.
> >>>
> >>> I'd call it non-ideal when better solutions available, but perfectly acceptable otherwise.
> >>>
> >> I don't know why Bonita dislikes spinning and sleeping, but I know why
> >> /I/ don't like it as a solution. Sleeping does not affect thread
> >> priority in the way mutexes do. That means if one low priority thread
> >> starts the initialisation and takes the lock, then gets pre-empted by a
> >> higher priority thread that tries to take the lock, your whole system
> >> can deadlock. (The high priority thread may be sleeping, but there may
> >> be a mid-priority thread that can run and blocks the low-priority thread
> >> from running.)
> >>
> >> This is going to be a more likely failure situation if you have fewer
> >> (or only one) cores. But the same compilers and toolchains are used for
> >> a wide variety of systems - you can't pick a solution that might fail on
> >> some systems. And note that these kinds of failures never turn up in
> >> testing - they are rare, and only happen at the worst possible moment
> >> after deployment.
> >>
> >> This is particularly a risk for real-time systems and embedded systems,
> >> where you are more likely to have fewer cores (perhaps only one), or
> >> maybe some of your cores are dedicated to real-time tasks and
> >> unavailable for other threads. You will also likely have high priority
> >> and real-time priority threads, which will block the low-priority
> >> threads. And on such systems there might be very serious costs or
> >> safety risks involved in system hangs - you want to be sure that they
> >> are not possible, to the best of practical abilities, rather than
> >> designing in a solution that has a definite non-zero chance of failure..
> >>
> >> Using mutexes avoids this problem, because if a high priority thread is
> >> waiting on a mutex held by a low priority thread, the holder's priority
> >> gets boosted to that of the high priority thread until it releases the lock.
> >>
> >> Spinning should only be used when you have full control of the
> >> priorities of threads that may take the spin lock, and can be sure it is
> >> safe.
> >
> > The problem you mention does not apply to general-purpose systems
> > like Windows, Linux, BSD-linage Unixes, Solaris e.t.c. They all have
> > built-in avoidance of deadlocks caused by priority-inversion. Most
> > commonly it's done by applying random priority boosts.
> As far as I know, such random priority boosts will not boost a
> non-realtime priority thread to real-time priority on Linux (and
> presumably not on other systems). That would negate the whole concept
> of realtime priority threads.

If you have enough of ready real time threads to occupy all CPUs for
unacceptably long time then, indeed, you have a problem.
But to me it looks unlikely that deadlock due to priority inversion is your
main problem in such scenario.

> >
> > Also I don't see how mutex semantics can possibly help. The common
> > problem scenario is that low-priority thread that is doing initialization is
> > preempted when it *does not* hold the mutex. The mutex is held for
> > two very short durations of flags update. The preemption is far more
> > likely to happen during the middle phase - constructor itself.
> >
> > On real-time system with 1 or 2 processors you probably want
> > completely different design in which a mutex is held during all duration
> > of initialization, but you can't expect such solution to be included
> > in g++/clang++/MSVC compiler support libraries.
> >
> Yes, you would need the mutex to be held during critical stages of the
> initialisation.
>
> I can understand that this would be expensive compared to spinlocks that
> would work fine for "normal" systems (especially with the random boosts
> you described).

What's advocated here by most posters and what's seems to be implemented
by major toolchains is not a choice between speenlocks and one or pair of mutexes
or equivalents. The spinlocks is mere something that we mention to argue against
Bonita's suggestion that the C++ Standard in its current form can be impossible
to implement.
Bonita aside, the real choice is between 4 options:
(A) Mutex per object. Held for all duration of the constructor.

(B) Serializing all initialization of function-local static objects with one global mutex.
Again, the mutex held throughout all duration of the constructor.

(C) Few flags and possible a little more of auxiliary states (like thread Id in my
proposed solution) per objects. Access to flags is guarded by global mutex,
but initialization itself is not guarded. The are several possible solution for
wake-up of contending threads of waiting for finish of initialization. Among such
solutions, spinlocks, either classical or amended by sleep() call are possible, but
least likely on full-featured OSes. The most natural solution [on f-f OS] is wait on
per-object conditional variable with global mutex.

(D) Creative combinations of A and B, e.g. small pool of mutexs distributed
dynamically between objects. Like A and B, mutex held throughout all duration
of the constructor.

All major toolchains appear to use variants of (C)

> And I understand that toolchains have to be optimised
> for normal situations, not things that are extremely rare even in
> particular niche situations.
>
> But getting threading, locking and synchronisation details right is very
> difficult - most programmers don't get it right. But you can't tell
> that you have a problem by testing the code, as failures typically
> require extreme bad luck in timing. So it always worries me when I see
> something that is hidden, which most programmers will assume "just
> works, by some compiler magic", but which can be a problem in some
> circumstances.
>
> (As another example, gcc's "libatomic" uses spinlocks - if you use
> std::atomic types on a microcontroller in situations where atomics would
> be useful, they will hang your system any time there is a coincidental
> access.)
> > Which again brings us to the point made by myself and few others:
> > don't do it! Don't use function-local static objects with constructors.
> > Or, at least don't use them in multitasking scenarios. Compiler's one
> > time initialization infrastructure can be super-robust, but being generic
> > it's unlikely to be optimal answer in any concrete scenario.
> >
> Yes, that is one option. You can also use "constinit" statics safely.

I am not aware of constinit. Is it something new, like C++14 or later?

> And gcc has a "-fno-threadsafe-statics" option (also useable as a
> pragma, but unfortunately not as a neater __attribute__) that puts the
> responsibility of ensuring things are threadsafe onto the user.

Re: Thread-safe initialization of static objects

<56e77942-b8ec-4e40-97ed-3340a6f2a01an@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1685&group=comp.lang.c%2B%2B#1685

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:6214:5182:b0:655:bc95:943d with SMTP id kl2-20020a056214518200b00655bc95943dmr191280qvb.4.1695307702574;
Thu, 21 Sep 2023 07:48:22 -0700 (PDT)
X-Received: by 2002:a05:6808:138e:b0:3a9:cd23:9853 with SMTP id
c14-20020a056808138e00b003a9cd239853mr2796848oiw.10.1695307702369; Thu, 21
Sep 2023 07:48:22 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Thu, 21 Sep 2023 07:48:21 -0700 (PDT)
In-Reply-To: <uehdjj$3h8me$3@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<DUVOM.87557$noZ7.33800@fx13.iad> <uehdjj$3h8me$3@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <56e77942-b8ec-4e40-97ed-3340a6f2a01an@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Thu, 21 Sep 2023 14:48:22 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2560
 by: Michael S - Thu, 21 Sep 2023 14:48 UTC

On Thursday, September 21, 2023 at 3:40:37 PM UTC+3, Bonita Montero wrote:
> Am 21.09.2023 um 13:36 schrieb Richard Damon:
>
> > Why?
>
> Because the initialization might only take a microsecond or less
> and a yield is a full timeslice, on Linux and Windows usually one
> millisecond.

If initialization is short then contention is very unlikely. Wasting a
a full timeslice once in a blue moon is o.k.
If initialization is long then contention is likely, however wasting a
a full timeslice after long initialization is o.k.
Not an excellent solution, but acceptable.

Re: Thread-safe initialization of static objects

<0PYOM.11220$%_f1.1502@fx06.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1686&group=comp.lang.c%2B%2B#1686

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx06.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me> <uegtd7$3dolc$1@dont-email.me> <r_VOM.87558$noZ7.69799@fx13.iad>
Lines: 30
Message-ID: <0PYOM.11220$%_f1.1502@fx06.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 21 Sep 2023 14:54:52 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 21 Sep 2023 14:54:52 GMT
X-Received-Bytes: 2348
 by: Scott Lurndal - Thu, 21 Sep 2023 14:54 UTC

Richard Damon <Richard@Damon-Family.org> writes:
>On 9/21/23 4:03 AM, Bonita Montero wrote:
>> Am 21.09.2023 um 09:58 schrieb Bonita Montero:
>>
>>>> They are only set to zero if they are not initialized explicitly.
>>
>>> I'm not talking about what the standad says but how it's implemented
>>> with all operating systems that support virtual memory.

Nonsense. As Pavel pointed out in the part this thread you
snipped, both pthread_once and pthread_mutex can be statically
initialized (and the value may or may not be zero).

>>
>> This shows "all zero" on all modern operating systems:

You're changing the topic. BSS has been guaranteed to
be initialized to zero since long before windows existed.

>Note, depending on the OS and implementation, that zeroing will either
>be done by having the loaded program image just contain a great big
>block of zeros, or (to save image size at the cost of CPU time) the
>system might just put "static" objects that need to be zero initialized
>into one common segment and zero-fill as an extension of the loading
>process, or in some cases the OS can just promise that new segments for
>the program are just always zero-filled.

If the bss section is page-aligned, demand paging will ensure
that the contents are zeroed before being accessed.

Re: Thread-safe initialization of static objects

<NQYOM.11221$%_f1.3150@fx06.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1687&group=comp.lang.c%2B%2B#1687

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx06.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad> <uegt3a$3dj0r$4@dont-email.me> <5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com> <ueh0rk$3ebkg$1@dont-email.me>
Lines: 13
Message-ID: <NQYOM.11221$%_f1.3150@fx06.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 21 Sep 2023 14:56:45 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 21 Sep 2023 14:56:45 GMT
X-Received-Bytes: 1448
 by: Scott Lurndal - Thu, 21 Sep 2023 14:56 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 21.09.2023 um 10:38 schrieb Michael S:
>
>> Actually, considering that contention is very rare in practice, ...
>
>The initialization might last an arbitrary time, so spinning
>is inacceptable.

It's rare in practice because good programmers very seldom[*] use
non-global static variables in threaded applications.

[*] Effectively never.

Re: Thread-safe initialization of static objects

<uehm0b$3iuv6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1688&group=comp.lang.c%2B%2B#1688

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 17:03:40 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uehm0b$3iuv6$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<DUVOM.87557$noZ7.33800@fx13.iad> <uehdjj$3h8me$3@dont-email.me>
<56e77942-b8ec-4e40-97ed-3340a6f2a01an@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 15:03:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3767270"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19n7mVcCW9rq5TsDtRTatt5Xc2JtbxZa0A="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:TkUxnalbfElkXIdswllEAhOCWf8=
Content-Language: de-DE
In-Reply-To: <56e77942-b8ec-4e40-97ed-3340a6f2a01an@googlegroups.com>
 by: Bonita Montero - Thu, 21 Sep 2023 15:03 UTC

Am 21.09.2023 um 16:48 schrieb Michael S:

> If initialization is short then contention is very unlikely.

Maybe, but no one would cose this solutions if mutexes are available.
Yielding in this situation is crap.

Re: Thread-safe initialization of static objects

<uehm1t$3iuv6$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1689&group=comp.lang.c%2B%2B#1689

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 17:04:29 +0200
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uehm1t$3iuv6$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad>
<uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
<ueh0rk$3ebkg$1@dont-email.me> <NQYOM.11221$%_f1.3150@fx06.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 15:04:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3767270"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZNL2/fIBHqaQWGBceWotUl87h9+oadjw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:5ifVLt+LUvY0Y9YawmKxw/mTuUY=
In-Reply-To: <NQYOM.11221$%_f1.3150@fx06.iad>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 15:04 UTC

Am 21.09.2023 um 16:56 schrieb Scott Lurndal:

> It's rare in practice because good programmers very seldom[*]
> use non-global static variables in threaded applications.

It's just your taste that this should be avoided.

Re: Thread-safe initialization of static objects

<uei0u9$3l0uh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1690&group=comp.lang.c%2B%2B#1690

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 20:10:16 +0200
Organization: A noiseless patient Spider
Lines: 181
Message-ID: <uei0u9$3l0uh$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <ue4t6i$3um57$1@dont-email.me>
<ue5sp2$7ms9$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me> <z1MOM.13123$TwR4.1703@fx46.iad>
<uegt3a$3dj0r$4@dont-email.me>
<5f5de245-c174-4f2d-9870-6a990123b524n@googlegroups.com>
<ueh0rk$3ebkg$1@dont-email.me>
<3af7b0b8-cfca-47d7-82cf-0d2bd3b8b321n@googlegroups.com>
<ueh8s7$3gh0c$1@dont-email.me>
<bed7f83a-18a0-4b4a-bb8c-cb8ff4a72ab4n@googlegroups.com>
<ueheuu$3hkm3$1@dont-email.me>
<bbde3ce4-b969-431e-a3f6-d0b5ec6aafacn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 21 Sep 2023 18:10:17 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5c6b6f64fdfdbbf114f5df1d8635f7ca";
logging-data="3834833"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19txmSbOgW8NsIU0lLyt8A7+MEo33TkCEI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:cI+/JLKyc+rb6NinyW0QNj87Q98=
In-Reply-To: <bbde3ce4-b969-431e-a3f6-d0b5ec6aafacn@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Thu, 21 Sep 2023 18:10 UTC

On 21/09/2023 16:30, Michael S wrote:
> On Thursday, September 21, 2023 at 4:03:46 PM UTC+3, David Brown wrote:
>> On 21/09/2023 14:01, Michael S wrote:
>>> On Thursday, September 21, 2023 at 2:19:59 PM UTC+3, David Brown wrote:
>>>> On 21/09/2023 11:59, Michael S wrote:
>>>>> On Thursday, September 21, 2023 at 12:03:01 PM UTC+3, Bonita Montero wrote:
>>>>>> Am 21.09.2023 um 10:38 schrieb Michael S:
>>>>>>
>>>>>>> Actually, considering that contention is very rare in practice, ...
>>>>>>
>>>>>> The initialization might last an arbitrary time, so spinning
>>>>>> is inacceptable.
>>>>>
>>>>> Why is it unacceptable? The impact of needless wake up of the waiting thread
>>>>> once per tick is negligible in terms of system performance and not too
>>>>> horrible in terms power consumption. The impact of needless sleep till the
>>>>> next tick is also insignificant under assumption that initialization took long time.
>>>>>
>>>>> I'd call it non-ideal when better solutions available, but perfectly acceptable otherwise.
>>>>>
>>>> I don't know why Bonita dislikes spinning and sleeping, but I know why
>>>> /I/ don't like it as a solution. Sleeping does not affect thread
>>>> priority in the way mutexes do. That means if one low priority thread
>>>> starts the initialisation and takes the lock, then gets pre-empted by a
>>>> higher priority thread that tries to take the lock, your whole system
>>>> can deadlock. (The high priority thread may be sleeping, but there may
>>>> be a mid-priority thread that can run and blocks the low-priority thread
>>>> from running.)
>>>>
>>>> This is going to be a more likely failure situation if you have fewer
>>>> (or only one) cores. But the same compilers and toolchains are used for
>>>> a wide variety of systems - you can't pick a solution that might fail on
>>>> some systems. And note that these kinds of failures never turn up in
>>>> testing - they are rare, and only happen at the worst possible moment
>>>> after deployment.
>>>>
>>>> This is particularly a risk for real-time systems and embedded systems,
>>>> where you are more likely to have fewer cores (perhaps only one), or
>>>> maybe some of your cores are dedicated to real-time tasks and
>>>> unavailable for other threads. You will also likely have high priority
>>>> and real-time priority threads, which will block the low-priority
>>>> threads. And on such systems there might be very serious costs or
>>>> safety risks involved in system hangs - you want to be sure that they
>>>> are not possible, to the best of practical abilities, rather than
>>>> designing in a solution that has a definite non-zero chance of failure.
>>>>
>>>> Using mutexes avoids this problem, because if a high priority thread is
>>>> waiting on a mutex held by a low priority thread, the holder's priority
>>>> gets boosted to that of the high priority thread until it releases the lock.
>>>>
>>>> Spinning should only be used when you have full control of the
>>>> priorities of threads that may take the spin lock, and can be sure it is
>>>> safe.
>>>
>>> The problem you mention does not apply to general-purpose systems
>>> like Windows, Linux, BSD-linage Unixes, Solaris e.t.c. They all have
>>> built-in avoidance of deadlocks caused by priority-inversion. Most
>>> commonly it's done by applying random priority boosts.
>> As far as I know, such random priority boosts will not boost a
>> non-realtime priority thread to real-time priority on Linux (and
>> presumably not on other systems). That would negate the whole concept
>> of realtime priority threads.
>
> If you have enough of ready real time threads to occupy all CPUs for
> unacceptably long time then, indeed, you have a problem.
> But to me it looks unlikely that deadlock due to priority inversion is your
> main problem in such scenario.
>

I always want to think through possible issues when it comes to
multi-threading - no matter how unlikely.

It is certainly not uncommon to have more ready real-time threads than
cpus in real-time systems - though perhaps not on real-time Linux
systems. In microcontroller systems, your OS (if you have one) is
usually an RTOS, and most of the threads are real-time (of different
priorities). And you usually only have one, occasionally two, cpu.

But I think for that kind of system, your recommended solution - don't
have function-local statics that need such initialisation
synchronisation - is far and away the most common solution. (It's
certainly the one I use myself.) Function-local statics with dynamic
initialisation have quite a bit of overhead, both for initialisation and
in use.

>>>
>>> Also I don't see how mutex semantics can possibly help. The common
>>> problem scenario is that low-priority thread that is doing initialization is
>>> preempted when it *does not* hold the mutex. The mutex is held for
>>> two very short durations of flags update. The preemption is far more
>>> likely to happen during the middle phase - constructor itself.
>>>
>>> On real-time system with 1 or 2 processors you probably want
>>> completely different design in which a mutex is held during all duration
>>> of initialization, but you can't expect such solution to be included
>>> in g++/clang++/MSVC compiler support libraries.
>>>
>> Yes, you would need the mutex to be held during critical stages of the
>> initialisation.
>>
>> I can understand that this would be expensive compared to spinlocks that
>> would work fine for "normal" systems (especially with the random boosts
>> you described).
>
> What's advocated here by most posters and what's seems to be implemented
> by major toolchains is not a choice between speenlocks and one or pair of mutexes
> or equivalents. The spinlocks is mere something that we mention to argue against
> Bonita's suggestion that the C++ Standard in its current form can be impossible
> to implement.
> Bonita aside, the real choice is between 4 options:
> (A) Mutex per object. Held for all duration of the constructor.
>
> (B) Serializing all initialization of function-local static objects with one global mutex.
> Again, the mutex held throughout all duration of the constructor.
>
> (C) Few flags and possible a little more of auxiliary states (like thread Id in my
> proposed solution) per objects. Access to flags is guarded by global mutex,
> but initialization itself is not guarded. The are several possible solution for
> wake-up of contending threads of waiting for finish of initialization. Among such
> solutions, spinlocks, either classical or amended by sleep() call are possible, but
> least likely on full-featured OSes. The most natural solution [on f-f OS] is wait on
> per-object conditional variable with global mutex.
>
> (D) Creative combinations of A and B, e.g. small pool of mutexs distributed
> dynamically between objects. Like A and B, mutex held throughout all duration
> of the constructor.
>
> All major toolchains appear to use variants of (C)

I must have a look at the language support library provided with the
embedded (32-bit arm-none-eabi-gcc) I use. (D) seems the natural choice
there, with less risk of conflict than (B) while avoiding the space
costs of (A). But my guess is that is actually done like (D) but with
simple spinlocks, since the compiler support library is independent of
the RTOS and therefore does not have access to mutexes.

Looking at generated code (using godbolt.org) for Linux, it seems there
is just a single byte boolean guard per object - there is no thread id
or anything else stored per object. I don't know what is going on
inside "__cxa_guard_acquire" and other such functions, however.

>
>> And I understand that toolchains have to be optimised
>> for normal situations, not things that are extremely rare even in
>> particular niche situations.
>>
>> But getting threading, locking and synchronisation details right is very
>> difficult - most programmers don't get it right. But you can't tell
>> that you have a problem by testing the code, as failures typically
>> require extreme bad luck in timing. So it always worries me when I see
>> something that is hidden, which most programmers will assume "just
>> works, by some compiler magic", but which can be a problem in some
>> circumstances.
>>
>> (As another example, gcc's "libatomic" uses spinlocks - if you use
>> std::atomic types on a microcontroller in situations where atomics would
>> be useful, they will hang your system any time there is a coincidental
>> access.)
>>> Which again brings us to the point made by myself and few others:
>>> don't do it! Don't use function-local static objects with constructors.
>>> Or, at least don't use them in multitasking scenarios. Compiler's one
>>> time initialization infrastructure can be super-robust, but being generic
>>> it's unlikely to be optimal answer in any concrete scenario.
>>>
>> Yes, that is one option. You can also use "constinit" statics safely.
>
> I am not aware of constinit. Is it something new, like C++14 or later?


Click here to read the complete article
Re: Thread-safe initialization of static objects

<uei142$3l13h$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1691&group=comp.lang.c%2B%2B#1691

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 11:13:22 -0700
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <uei142$3l13h$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
<ueff5s$32ncr$3@dont-email.me>
<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
<uegft8$3bf1v$2@dont-email.me>
<bd177a2a-9ffa-45bc-81d4-fe7a46b79024n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 21 Sep 2023 18:13:22 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3834993"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iydB+lnNipNsmVhcfX0mUylOQm6JXwDs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:7/OF3wPXgwcOgRYy9WljFRU+cds=
In-Reply-To: <bd177a2a-9ffa-45bc-81d4-fe7a46b79024n@googlegroups.com>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 21 Sep 2023 18:13 UTC

On 9/21/2023 1:04 AM, Michael S wrote:
> On Thursday, September 21, 2023 at 7:13:45 AM UTC+3, Chris M. Thomasson wrote:
>> On 9/20/2023 12:36 PM, Michael S wrote:
>>> On Wednesday, September 20, 2023 at 9:55:09 PM UTC+3, Chris M. Thomasson wrote:
>>>> On 9/20/2023 9:48 AM, Michael S wrote:
>>>>> On Wednesday, September 20, 2023 at 4:18:42 PM UTC+3, Bonita Montero wrote:
>>>>>> Am 20.09.2023 um 13:54 schrieb Richard Damon:
>> [...]
>>> It's unlikely to be unreliable in this particular circumstances.
>>> But apart from it, my code has bug (race condition) :(
>>> Just shows that this sort of crap is not easy.
>> [...]
>> Implement your algorithm in Relacy Race Detector. It can help. I know
>> the guy who created it. A good guy.
>
> I like my last version, the one that wakes up waiting threads in chained mannner
> by means of QueueUserAPC(), better.
> It's only disadvantage is higher memory footage - 6 bytes per guarded object instead of 1.
> Can be reduced to 5 bytes, but in practice compiler will allocate 8 bytes anyway, so let it
> be 6.

Here is Relacy, a pretty nice way to model sync algorithms:

https://www.1024cores.net/home/relacy-race-detector

Re: Thread-safe initialization of static objects

<uei1g6$3l13h$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=1692&group=comp.lang.c%2B%2B#1692

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 11:19:50 -0700
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <uei1g6$3l13h$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<ueggh5$3bq3f$1@dont-email.me> <uegsdo$3dj0r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 18:19:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3834993"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5cuG0KEJKgJ+SEoO+paWcq+QhSNdwt+o="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:gI2hYL649KG9jaPhwfGQE4hxWlo=
In-Reply-To: <uegsdo$3dj0r$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 21 Sep 2023 18:19 UTC

On 9/21/2023 12:47 AM, Bonita Montero wrote:
> Am 21.09.2023 um 06:24 schrieb Chris M. Thomasson:
>
>> On 9/20/2023 8:42 PM, Bonita Montero wrote:
>
>>> A yield yould be to inefficient.
>
>> Have you heard about an adaptive backoff, different than a
>> "traditional" adaptive mutex? ...
>
> We discussed yield with periodic polling.
>

An adaptive backoff tries to automatically set its spin limit based on
some statistics it gathers dynamically during runtime, aka before it
waits in the kernel. One way to do it, a very interesting way imvho, is
instead of spinning and yielding, it tries to do some other unrelated
work instead. Pretty nice, actually. Something akin to:

while (! try_lock_or_whatever())
{ if (! try_to_do_some_other_unrelated_work())
{
// wait in the kernel, really slow path...
}
}

{
// We are locked! :^D
}

unlock();

If your program can handle something like this, it actually works pretty
good...


devel / comp.lang.c++ / Thread-safe initialization of static objects

Pages:123456789101112131415161718192021222324252627
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor