Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

All programmers are playwrights and all computers are lousy actors.


devel / comp.lang.c++ / Re: 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

<uedp69$2orvq$1@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 05:33:29 +0200
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uedp69$2orvq$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> <ue7hb6$g6eb$1@dont-email.me>
<ue7hdn$g7tm$2@dont-email.me> <ue7hps$g6eb$4@dont-email.me>
<ue7htp$g7tm$6@dont-email.me> <ue7idv$gach$4@dont-email.me>
<ue7j3g$ggh4$1@dont-email.me> <ue7lnd$h1b4$1@dont-email.me>
<ue8lpq$q25a$1@dont-email.me> <uea7vf$1tgqs$1@dont-email.me>
<ueb3c7$2638l$1@dont-email.me> <ueb9up$271en$2@dont-email.me>
<uebn0a$293m3$1@dont-email.me> <uecsse$2g3hn$7@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:33:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="2912250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cGPOtqfFtIB3i/b9XOt3rhzuYqO9GQJE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:NpU4QUyfhPZLRwti6/vtw5KWtFk=
In-Reply-To: <uecsse$2g3hn$7@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Wed, 20 Sep 2023 03:33 UTC

Am 19.09.2023 um 21:30 schrieb Chris M. Thomasson:

> A futex allows a programmer to use a predicate, akin to a condition
> variable for lock/wait free algorithms. They are quite nice. Still, I
> don't think you know how they actually work...

I've read the paper, you didn't.

Re: Thread-safe initialization of static objects

<uedp93$2orvq$2@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 05:35:00 +0200
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <uedp93$2orvq$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:34:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="2912250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bsmXMUCySMU2UdmdhhIdf/GZFzdZ6H6k="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:oktCGa6NUwAE925pnV4KSTKxdBk=
In-Reply-To: <iwpOM.265$Sn81.181@fx08.iad>
Content-Language: de-DE
 by: Bonita Montero - Wed, 20 Sep 2023 03:35 UTC

Am 20.09.2023 um 00:45 schrieb Richard Damon:

> And there are things that work enough like a Mutex to do this, that
> aren't Mutexes ...

Tell me which things you're thinking about. The standard requires that
contenders while static initialization sleep, that is possible ony with
a mutex.

Re: Thread-safe initialization of static objects

<uedpah$2orvq$3@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 05:35:46 +0200
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uedpah$2orvq$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>
<3ba58128-f2f5-4639-8b9d-606782c2b486n@googlegroups.com>
<ueco83$2fd1j$1@dont-email.me> <uecr23$2g167$1@dont-email.me>
<uecrr6$2g3hn$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:35:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="2912250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+TK92KkjOxPlfSxoiTNGV2PkUOqIQarHE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ACocm6CTd5lv7r9PasNBbeH65AE=
Content-Language: de-DE
In-Reply-To: <uecrr6$2g3hn$4@dont-email.me>
 by: Bonita Montero - Wed, 20 Sep 2023 03:35 UTC

Am 19.09.2023 um 21:12 schrieb Chris M. Thomasson:

> Futex has all the kernel resources created up front, Bonita. Also,
> there was this interesting method I learned about many years ago:

Idiot.

Re: Thread-safe initialization of static objects

<uedpbr$2orvq$4@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 05:36:27 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uedpbr$2orvq$4@dont-email.me>
References: <udafjf$2joeq$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> <EjoNM.17221$DXgc.9861@fx36.iad>
<ue5tfc$7pns$1@dont-email.me> <zPGNM.14021$BMnd.11871@fx04.iad>
<ue7f6g$frod$2@dont-email.me> <ue7gdm$fumm$4@dont-email.me>
<ue7hc3$g7tm$1@dont-email.me> <ue7r4j$i3u1$1@dont-email.me>
<ue8mid$q534$2@dont-email.me> <O9XNM.16067$JX2f.12159@fx13.iad>
<20230918110147.326@kylheku.com> <uea42g$1sn0e$2@dont-email.me>
<20230918123203.723@kylheku.com> <ueb3q5$2638l$5@dont-email.me>
<20230918205859.263@kylheku.com> <uebn7l$293m3$3@dont-email.me>
<20230919094617.529@kylheku.com> <uecn60$2f5ud$2@dont-email.me>
<uecs4n$2g3hn$5@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:36:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="2912250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19aQSGdh9RCdZGUmmfKxdpMXw0Oqssexpg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:GWvZllmce9ynNR8iQk2ygyfLLcs=
Content-Language: de-DE
In-Reply-To: <uecs4n$2g3hn$5@dont-email.me>
 by: Bonita Montero - Wed, 20 Sep 2023 03:36 UTC

Am 19.09.2023 um 21:17 schrieb Chris M. Thomasson:

> A locking order is very important ...

There are no deadlocks with locking static initialization.

Re: Thread-safe initialization of static objects

<uedpdo$2orvq$5@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 05:37:28 +0200
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <uedpdo$2orvq$5@dont-email.me>
References: <udafjf$2joeq$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> <EjoNM.17221$DXgc.9861@fx36.iad>
<ue5tfc$7pns$1@dont-email.me> <zPGNM.14021$BMnd.11871@fx04.iad>
<ue7f6g$frod$2@dont-email.me> <ue7gdm$fumm$4@dont-email.me>
<ue7hc3$g7tm$1@dont-email.me> <ue7r4j$i3u1$1@dont-email.me>
<ue8mid$q534$2@dont-email.me> <O9XNM.16067$JX2f.12159@fx13.iad>
<20230918110147.326@kylheku.com> <uea42g$1sn0e$2@dont-email.me>
<20230918123203.723@kylheku.com> <ueb3q5$2638l$5@dont-email.me>
<20230918205859.263@kylheku.com> <uebn7l$293m3$3@dont-email.me>
<20230919094617.529@kylheku.com> <uecn60$2f5ud$2@dont-email.me>
<20230919140127.732@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:37:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="2912250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+oSiwZSpbkPSjvAy1sfJ7PPiurVKz4w8s="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:OTQWmsK1J4cZ3nRi0KNg/XLDj+I=
In-Reply-To: <20230919140127.732@kylheku.com>
Content-Language: de-DE
 by: Bonita Montero - Wed, 20 Sep 2023 03:37 UTC

Am 19.09.2023 um 23:08 schrieb Kaz Kylheku:

> int B();
> int A();
>
> int A()
> {
> static int a = B();
> return 'A';
> }
>
> int B()
> {
> static int b = A();
> return 'B';
> }
>
> int main()
> {
> A();
> return 0;
> }

Static initialization woudln't lock for trivial types.

Re: Thread-safe initialization of static objects

<uedpg0$2orvq$6@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 05:38:40 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uedpg0$2orvq$6@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>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<ueboet$29a2b$1@dont-email.me> <uebq3e$29k5u$1@dont-email.me>
<mUfOM.25777$ZkX3.20078@fx09.iad> <uec35s$2b8an$1@dont-email.me>
<RtpOM.264$Sn81.213@fx08.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:38:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="2912250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188mCXg40xYBhP7Q5QoPNVZAPgOOrtW+7Q="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:y2Q8RRc592LHy1Y3x9USlgQpKhM=
Content-Language: de-DE
In-Reply-To: <RtpOM.264$Sn81.213@fx08.iad>
 by: Bonita Montero - Wed, 20 Sep 2023 03:38 UTC

Am 20.09.2023 um 00:42 schrieb Richard Damon:

> Nope, all that shows is the system can't JUST use one global Mutex.

That's what I said.

Re: Thread-safe initialization of static objects

<uedq3h$2p0bo$1@dont-email.me>

  copy mid

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

  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: Tue, 19 Sep 2023 20:49:04 -0700
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <uedq3h$2p0bo$1@dont-email.me>
References: <udafjf$2joeq$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>
<ue7hb6$g6eb$1@dont-email.me> <ue7hdn$g7tm$2@dont-email.me>
<ue7hps$g6eb$4@dont-email.me> <ue7htp$g7tm$6@dont-email.me>
<ue7idv$gach$4@dont-email.me> <ue7j3g$ggh4$1@dont-email.me>
<ue7lnd$h1b4$1@dont-email.me> <ue8lpq$q25a$1@dont-email.me>
<uea7vf$1tgqs$1@dont-email.me> <ueb3c7$2638l$1@dont-email.me>
<ueb9up$271en$2@dont-email.me> <uebn0a$293m3$1@dont-email.me>
<uecsse$2g3hn$7@dont-email.me> <uedp69$2orvq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:49:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="2916728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19E8pNAvyTpU+YmGfdEnNsike9bWW+ABRA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:z0oClHkJw0L1gxWFizsa657Tf+M=
Content-Language: en-US
In-Reply-To: <uedp69$2orvq$1@dont-email.me>
 by: Chris M. Thomasson - Wed, 20 Sep 2023 03:49 UTC

On 9/19/2023 8:33 PM, Bonita Montero wrote:
> Am 19.09.2023 um 21:30 schrieb Chris M. Thomasson:
>
>> A futex allows a programmer to use a predicate, akin to a condition
>> variable for lock/wait free algorithms. They are quite nice. Still, I
>> don't think you know how they actually work...
>
> I've read the paper, you didn't.
>

Yawn.

Re: Thread-safe initialization of static objects

<uedq5m$2p0bo$2@dont-email.me>

  copy mid

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

  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: Tue, 19 Sep 2023 20:50:13 -0700
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <uedq5m$2p0bo$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> <ueb9vm$271en$3@dont-email.me>
<uebn4f$293m3$2@dont-email.me> <ueboet$29a2b$1@dont-email.me>
<uebq3e$29k5u$1@dont-email.me> <mUfOM.25777$ZkX3.20078@fx09.iad>
<uec35s$2b8an$1@dont-email.me> <RtpOM.264$Sn81.213@fx08.iad>
<uedpg0$2orvq$6@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 03:50:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="2916728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZO0YRZUHwKs0sQu4w4d6NGrVgvNJxbtc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:vfa3vW+Ubve1JNewk9+STGElQgM=
In-Reply-To: <uedpg0$2orvq$6@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 20 Sep 2023 03:50 UTC

On 9/19/2023 8:38 PM, Bonita Montero wrote:
> Am 20.09.2023 um 00:42 schrieb Richard Damon:
>
>> Nope, all that shows is the system can't JUST use one global Mutex.
>
> That's what I said.
>
>

So?

Re: Thread-safe initialization of static objects

<20230919204410.51@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Wed, 20 Sep 2023 03:52:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <20230919204410.51@kylheku.com>
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>
<EjoNM.17221$DXgc.9861@fx36.iad> <ue5tfc$7pns$1@dont-email.me>
<zPGNM.14021$BMnd.11871@fx04.iad> <ue7f6g$frod$2@dont-email.me>
<ue7gdm$fumm$4@dont-email.me> <ue7hc3$g7tm$1@dont-email.me>
<ue7r4j$i3u1$1@dont-email.me> <ue8mid$q534$2@dont-email.me>
<O9XNM.16067$JX2f.12159@fx13.iad> <20230918110147.326@kylheku.com>
<uea42g$1sn0e$2@dont-email.me> <20230918123203.723@kylheku.com>
<ueb3q5$2638l$5@dont-email.me> <20230918205859.263@kylheku.com>
<uebn7l$293m3$3@dont-email.me> <20230919094617.529@kylheku.com>
<uecn60$2f5ud$2@dont-email.me> <20230919140127.732@kylheku.com>
<uedpdo$2orvq$5@dont-email.me>
Injection-Date: Wed, 20 Sep 2023 03:52:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b0a008d922e8913c94514619367ad5e6";
logging-data="2915154"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19GfzYNJ0BItOVXUd9NsCJjW+CW5M5gU1A="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:uBE2UhRP8UwjS2qqh+klUgkwV40=
 by: Kaz Kylheku - Wed, 20 Sep 2023 03:52 UTC

On 2023-09-20, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 19.09.2023 um 23:08 schrieb Kaz Kylheku:
>
>> int B();
>> int A();
>>
>> int A()
>> {
>> static int a = B();
>> return 'A';
>> }
>>
>> int B()
>> {
>> static int b = A();
>> return 'B';
>> }
>>
>> int main()
>> {
>> A();
>> return 0;
>> }
>
> Static initialization woudln't lock for trivial types.

Why would you snip away an actual transcript showing that something is
happening, and claim it woudln't happen?

It has to lock.

If two threads execute this at around the same time:

static int a = B();

one of them has to be delayed until the other finishes
executing B(), initializes a, sets some flag indicating
that this was done, and releases the lock.

The type of a is trivial, but B() isn't trivial; B()
can be an arbitrarily complex calculation, and a is not
initialized until that calculation completes.

Here is part of the transcript again: the gdb backtrace:

(gdb) r
Starting program: /home/kaz/junk/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
^C
Program received signal SIGINT, Interrupt.
0xb7fd6d09 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fd6d09 in __kernel_vsyscall ()
#1 0xb7d07197 in syscall () at
.../sysdeps/unix/sysv/linux/i386/syscall.S:29
#2 0xb7ea379d in __cxa_guard_acquire () from
/usr/lib/i386-linux-gnu/libstdc++.so.6
#3 0x004006c2 in A() ()
#4 0x00400773 in B() ()
#5 0x004006d7 in A() ()
#6 0x004007e5 in main ()

The __cxa_guard_acquire that's making a __kernel_vsyscall is
the locking mechanism guarding the "static int a = B()".

It has recursively deadlocked. The main thread already owns
that lock and when it tries to acquire it again, it hangs.

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

Re: Thread-safe initialization of static objects

<uedqq6$2p0bo$5@dont-email.me>

  copy mid

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

  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: Tue, 19 Sep 2023 21:01:09 -0700
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <uedqq6$2p0bo$5@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 04:01:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="2916728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fyx1FqU3KGHKGW8GE/Lw9BFOj4Uk+ZwQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:UC5tEC7FNyGS34z1HpAlLt5Dpvw=
In-Reply-To: <uedp93$2orvq$2@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 20 Sep 2023 04:01 UTC

On 9/19/2023 8:35 PM, Bonita Montero wrote:
> Am 20.09.2023 um 00:45 schrieb Richard Damon:
>
>> And there are things that work enough like a Mutex to do this, that
>> aren't Mutexes ...
>
> Tell me which things you're thinking about. The standard requires that
> contenders while static initialization sleep, that is possible ony with
> a mutex.
>
>

HUH? Barf...

Re: Thread-safe initialization of static objects

<uedqqq$2p0bo$6@dont-email.me>

  copy mid

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

  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: Tue, 19 Sep 2023 21:01:30 -0700
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <uedqqq$2p0bo$6@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>
<3ba58128-f2f5-4639-8b9d-606782c2b486n@googlegroups.com>
<ueco83$2fd1j$1@dont-email.me> <uecr23$2g167$1@dont-email.me>
<uecrr6$2g3hn$4@dont-email.me> <uedpah$2orvq$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 20 Sep 2023 04:01:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="2916728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QAQBxwdHELxTnBT2e/J/Z3IXK5uDyyf0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:DfbgmVzMvggfXtP4SS3Ueniu+lg=
Content-Language: en-US
In-Reply-To: <uedpah$2orvq$3@dont-email.me>
 by: Chris M. Thomasson - Wed, 20 Sep 2023 04:01 UTC

On 9/19/2023 8:35 PM, Bonita Montero wrote:
> Am 19.09.2023 um 21:12 schrieb Chris M. Thomasson:
>
>> Futex has all the kernel resources created up front, Bonita. Also,
>> there  was this interesting method I learned about many years ago:
>
> Idiot.
>
>

Yawn.

Re: Thread-safe initialization of static objects

<uedqun$2p0bo$7@dont-email.me>

  copy mid

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

  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: Tue, 19 Sep 2023 21:03:34 -0700
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <uedqun$2p0bo$7@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> <EjoNM.17221$DXgc.9861@fx36.iad>
<ue5tfc$7pns$1@dont-email.me> <zPGNM.14021$BMnd.11871@fx04.iad>
<ue7f6g$frod$2@dont-email.me> <ue7gdm$fumm$4@dont-email.me>
<ue7hc3$g7tm$1@dont-email.me> <ue7r4j$i3u1$1@dont-email.me>
<ue8mid$q534$2@dont-email.me> <O9XNM.16067$JX2f.12159@fx13.iad>
<20230918110147.326@kylheku.com> <uea42g$1sn0e$2@dont-email.me>
<20230918123203.723@kylheku.com> <ueb3q5$2638l$5@dont-email.me>
<20230918205859.263@kylheku.com> <uebn7l$293m3$3@dont-email.me>
<20230919094617.529@kylheku.com> <uecn60$2f5ud$2@dont-email.me>
<20230919140127.732@kylheku.com> <uedpdo$2orvq$5@dont-email.me>
<20230919204410.51@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 04:03:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="2916728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VNApy+Y+e2wBLkzSN8DZ3/pJk4/f8I7k="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:pFm/sG1m1tkak8VzuDwVW1/Stoc=
Content-Language: en-US
In-Reply-To: <20230919204410.51@kylheku.com>
 by: Chris M. Thomasson - Wed, 20 Sep 2023 04:03 UTC

On 9/19/2023 8:52 PM, Kaz Kylheku wrote:
> On 2023-09-20, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 19.09.2023 um 23:08 schrieb Kaz Kylheku:
>>
>>> int B();
>>> int A();
>>>
>>> int A()
>>> {
>>> static int a = B();
>>> return 'A';
>>> }
>>>
>>> int B()
>>> {
>>> static int b = A();
>>> return 'B';
>>> }
>>>
>>> int main()
>>> {
>>> A();
>>> return 0;
>>> }
>>
>> Static initialization woudln't lock for trivial types.
>
> Why would you snip away an actual transcript showing that something is
> happening, and claim it woudln't happen?

Bonita is having some sort of a meltdown? Humm...

>
> It has to lock.
>
> If two threads execute this at around the same time:
>
> static int a = B();
>
> one of them has to be delayed until the other finishes
> executing B(), initializes a, sets some flag indicating
> that this was done, and releases the lock.
>
> The type of a is trivial, but B() isn't trivial; B()
> can be an arbitrarily complex calculation, and a is not
> initialized until that calculation completes.
>
> Here is part of the transcript again: the gdb backtrace:
>
> (gdb) r
> Starting program: /home/kaz/junk/a.out
> [Thread debugging using libthread_db enabled]
> Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
> ^C
> Program received signal SIGINT, Interrupt.
> 0xb7fd6d09 in __kernel_vsyscall ()
> (gdb) bt
> #0 0xb7fd6d09 in __kernel_vsyscall ()
> #1 0xb7d07197 in syscall () at
> ../sysdeps/unix/sysv/linux/i386/syscall.S:29
> #2 0xb7ea379d in __cxa_guard_acquire () from
> /usr/lib/i386-linux-gnu/libstdc++.so.6
> #3 0x004006c2 in A() ()
> #4 0x00400773 in B() ()
> #5 0x004006d7 in A() ()
> #6 0x004007e5 in main ()
>
> The __cxa_guard_acquire that's making a __kernel_vsyscall is
> the locking mechanism guarding the "static int a = B()".
>
> It has recursively deadlocked. The main thread already owns
> that lock and when it tries to acquire it again, it hangs.
>

Re: Thread-safe initialization of static objects

<uedrai$2p0bo$8@dont-email.me>

  copy mid

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

  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: Tue, 19 Sep 2023 21:09:53 -0700
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <uedrai$2p0bo$8@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 20 Sep 2023 04:09:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="2916728"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IsatpOWmpjBgjmVMfn1SyWJMm5m+rNfA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:pw1uJlzSg5pMfVc+07p6Dn9V1ac=
Content-Language: en-US
In-Reply-To: <iwpOM.265$Sn81.181@fx08.iad>
 by: Chris M. Thomasson - Wed, 20 Sep 2023 04:09 UTC

On 9/19/2023 3:45 PM, Richard Damon wrote:
> On 9/19/23 10:14 AM, Bonita Montero wrote:
>> Am 19.09.2023 um 15:49 schrieb Scott Lurndal:
>>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>> Am 19.09.2023 um 07:01 schrieb Chris M. Thomasson:
>>>>
>>>>>> I've shown that static initialization actually uses a mutex per
>>>>>> object.
>>>>
>>>>> Really?
>>>>
>>>> Here, a third time the code for you:
>>>
>>> This code doesn't show any mutex at all.
>>
>> It is guaranteed that if multiple threads go through a line of code
>> with static initialization, only one thread will actually initialize
>> the static object and others will sleep meanwhile. This can't be
>> handled without something that works like a mutex. It is irrelevant
>> whether a std::mutex or a native mutex of the operating system is
>> used. Others here have understood that this is the only way it works,
>> the point of the discussion was simply whether exactly one mutex is
>> responsible for all static initializations, which would make it
>> possible to get through almost always without a malfunction, or
>> whether it would be more static Initialization there is a  mutex.
>> I have proven the latter with my code.
>> So again: it is guaranteed that static initialization is thread-safe.
>> The standard also says that if more than one thread wants to initialize
>> statically, all others do not do so and ultimately sleep until the
>> initialization is complete. Please tell me how this should work
>> differently than with a mutex
>>
>>
>>> Disassemble the code and find the synchronization object.
>>
>> Idiot.
>
> And there are things that work enough like a Mutex to do this, that
> aren't Mutexes in the sense you are using, as they don't have the same
> failure modes.
>
> You are just showing how closed your mind is.

Basically, right on.

Re: Thread-safe initialization of static objects

<1bbc625d-fdc8-4e45-8659-4270ecf897d2n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:4e21:0:b0:656:262c:6928 with SMTP id dm1-20020ad44e21000000b00656262c6928mr18504qvb.2.1695197676750;
Wed, 20 Sep 2023 01:14:36 -0700 (PDT)
X-Received: by 2002:a05:6808:1514:b0:3ac:a376:707a with SMTP id
u20-20020a056808151400b003aca376707amr897862oiw.10.1695197676547; Wed, 20 Sep
2023 01:14:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!news.niel.me!glou.org!news.glou.org!fdn.fr!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Wed, 20 Sep 2023 01:14:36 -0700 (PDT)
In-Reply-To: <udkpmo$knnn$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> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbe19$2rk0p$2@dont-email.me> <2acKM.719164$U3w1.269152@fx09.iad>
<udbjfo$2s7cn$1@dont-email.me> <RllKM.1028889$SuUf.692033@fx14.iad>
<udcubm$325k4$1@dont-email.me> <wguKM.698884$xMqa.357246@fx12.iad>
<udebu2$3bknb$1@dont-email.me> <X5KKM.218705$ftCb.30173@fx34.iad>
<udgpnl$3tuni$1@dont-email.me> <udi0vu$488m$1@dont-email.me>
<udi9s5$5f8c$1@dont-email.me> <q82LM.1449511$GMN3.1347862@fx16.iad>
<udig8l$6ddc$1@dont-email.me> <zI4LM.170544$uEkc.45136@fx35.iad>
<udk0do$h1e9$1@dont-email.me> <hRjLM.1455228$GMN3.816986@fx16.iad>
<udkoc6$kdis$1@dont-email.me> <udkpmo$knnn$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1bbc625d-fdc8-4e45-8659-4270ecf897d2n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Wed, 20 Sep 2023 08:14:36 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Michael S - Wed, 20 Sep 2023 08:14 UTC

On Sunday, September 10, 2023 at 7:09:11 PM UTC+3, Paavo Helde wrote:
> 10.09.2023 18:46 Bonita Montero kirjutas:
> > Am 10.09.2023 um 15:51 schrieb Richard Damon:
> >
> >> You don't seem to understand that sincd it CAN be done in a way that
> >> always works, ...
> >
> > A standard shouldn't mandate that this never fails.
> So suppose the standard committee agrees with you and adds a sentence to
> the standard:
>
> Dynamic initialization of a non-local variable with static storage
> duration can fail even if its initializing function does not throw, in
> which case the behavior is ...undefined... / ..unspecified... /
> ...calling std::terminate() after unspecified time...
>
> Which variant do you prefer? And how would this help the programmer who
> needs to write programs in C++? Should they just avoid creating any
> static variables at all, in order to not trigger this failure scenario?

Avoidance of creation of function-scope static variables with non-trivial
constructors is a good guiding principle. Especially so in multithreaded
environment.
Of course, it is a good guiding principle for more important reasons than
one mentioned here.

Re: Thread-safe initialization of static objects

<M2BOM.2057$TwR4.712@fx46.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.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> <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> <ueb9vm$271en$3@dont-email.me>
<uebn4f$293m3$2@dont-email.me> <ueboet$29a2b$1@dont-email.me>
<uebq3e$29k5u$1@dont-email.me> <mUfOM.25777$ZkX3.20078@fx09.iad>
<uec35s$2b8an$1@dont-email.me> <RtpOM.264$Sn81.213@fx08.iad>
<uedpg0$2orvq$6@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <uedpg0$2orvq$6@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 19
Message-ID: <M2BOM.2057$TwR4.712@fx46.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: Wed, 20 Sep 2023 07:53:16 -0400
X-Received-Bytes: 2467
 by: Richard Damon - Wed, 20 Sep 2023 11:53 UTC

On 9/19/23 11:38 PM, Bonita Montero wrote:
> Am 20.09.2023 um 00:42 schrieb Richard Damon:
>
>> Nope, all that shows is the system can't JUST use one global Mutex.
>
> That's what I said.
>
>

No, you said it uses an actual full mutex per object.

That actual answer was reported that it uses a single global Mutex to
implement a simple locking primitive (which has a copy per object). The
global mutex is acquired at the very start, and is held only during the
updating of the simple locking primitive, not for the duration that
primitive is held.

But, since you don't understand that, you ignore that such a method is a
possibility.

Re: Thread-safe initialization of static objects

<k4BOM.2058$TwR4.78@fx46.iad>

  copy mid

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

  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!fx46.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> <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>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <uedp93$2orvq$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 16
Message-ID: <k4BOM.2058$TwR4.78@fx46.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: Wed, 20 Sep 2023 07:54:56 -0400
X-Received-Bytes: 1901
 by: Richard Damon - Wed, 20 Sep 2023 11:54 UTC

On 9/19/23 11:35 PM, Bonita Montero wrote:
> Am 20.09.2023 um 00:45 schrieb Richard Damon:
>
>> And there are things that work enough like a Mutex to do this, that
>> aren't Mutexes ...
>
> Tell me which things you're thinking about. The standard requires that
> contenders while static initialization sleep, that is possible ony with
> a mutex.
>
>

Nope, shows your lack of imagination.

I can make a thread wait before going past the initialization point
(which is what is required, not to "Sleep") without using a Mutex.

Re: Thread-safe initialization of static objects

<ueeret$2upib$1@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 15:18:22 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <ueeret$2upib$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 13:18:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="3106379"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19v7FmRNK/K2sgLYmEgZ4BVghZPZYDhtMA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:bb8onQAr1oGJVyYKUG8+6/UQbiA=
Content-Language: de-DE
In-Reply-To: <k4BOM.2058$TwR4.78@fx46.iad>
 by: Bonita Montero - Wed, 20 Sep 2023 13:18 UTC

Am 20.09.2023 um 13:54 schrieb Richard Damon:

> Nope, shows your lack of imagination.

Show me how sleeping works in situation without waiting for a semaphore
which is part of the slow path of a mutex.

Re: Thread-safe initialization of static objects

<IvCOM.31622$2ph4.7988@fx14.iad>

  copy mid

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

  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!fx14.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> <udebu2$3bknb$1@dont-email.me> <X5KKM.218705$ftCb.30173@fx34.iad> <udgpnl$3tuni$1@dont-email.me> <udi0vu$488m$1@dont-email.me> <udi9s5$5f8c$1@dont-email.me> <q82LM.1449511$GMN3.1347862@fx16.iad> <udig8l$6ddc$1@dont-email.me> <zI4LM.170544$uEkc.45136@fx35.iad> <udk0do$h1e9$1@dont-email.me> <hRjLM.1455228$GMN3.816986@fx16.iad> <udkoc6$kdis$1@dont-email.me> <udkpmo$knnn$1@dont-email.me> <1bbc625d-fdc8-4e45-8659-4270ecf897d2n@googlegroups.com>
Lines: 32
Message-ID: <IvCOM.31622$2ph4.7988@fx14.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 20 Sep 2023 13:32:24 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 20 Sep 2023 13:32:24 GMT
X-Received-Bytes: 2382
 by: Scott Lurndal - Wed, 20 Sep 2023 13:32 UTC

Michael S <already5chosen@yahoo.com> writes:
>On Sunday, September 10, 2023 at 7:09:11=E2=80=AFPM UTC+3, Paavo Helde wrot=
>e:
>> 10.09.2023 18:46 Bonita Montero kirjutas:=20
>> > Am 10.09.2023 um 15:51 schrieb Richard Damon:=20
>> >=20
>> >> You don't seem to understand that sincd it CAN be done in a way that=
>=20
>> >> always works, ...=20
>> >=20
>> > A standard shouldn't mandate that this never fails.
>> So suppose the standard committee agrees with you and adds a sentence to=
>=20
>> the standard:=20
>>=20
>> Dynamic initialization of a non-local variable with static storage=20
>> duration can fail even if its initializing function does not throw, in=20
>> which case the behavior is ...undefined... / ..unspecified... /=20
>> ...calling std::terminate() after unspecified time...=20
>>=20
>> Which variant do you prefer? And how would this help the programmer who=
>=20
>> needs to write programs in C++? Should they just avoid creating any=20
>> static variables at all, in order to not trigger this failure scenario?
>
>Avoidance of creation of function-scope static variables with non-trivial
>constructors is a good guiding principle. Especially so in multithreaded
>environment.
>Of course, it is a good guiding principle for more important reasons than
>one mentioned here.

Finally. That's indeed the case.

Re: Thread-safe initialization of static objects

<AcDOM.64148$%uv8.27450@fx15.iad>

  copy mid

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

  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!fx15.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> <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>
Lines: 10
Message-ID: <AcDOM.64148$%uv8.27450@fx15.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Wed, 20 Sep 2023 14:20:16 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Wed, 20 Sep 2023 14:20:16 GMT
X-Received-Bytes: 1366
 by: Scott Lurndal - Wed, 20 Sep 2023 14:20 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 20.09.2023 um 13:54 schrieb Richard Damon:
>
>> Nope, shows your lack of imagination.
>
>Show me how sleeping works in situation without waiting for a semaphore
>which is part of the slow path of a mutex.

Does the term 'spin lock' have any meaning to you?

Re: Thread-safe initialization of static objects

<ueevo0$2vkcq$1@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 16:31:29 +0200
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <ueevo0$2vkcq$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <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>
<AcDOM.64148$%uv8.27450@fx15.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 14:31:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8e0bfba3201d5af4f8e99f8f7cc7de9d";
logging-data="3133850"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18W+nxAyeUzgOxOyAzC54+xGelUrvr79sk="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:bBHSJoDWWyh9VXUtaloLGdgIIQY=
Content-Language: de-DE
In-Reply-To: <AcDOM.64148$%uv8.27450@fx15.iad>
 by: Bonita Montero - Wed, 20 Sep 2023 14:31 UTC

Am 20.09.2023 um 16:20 schrieb Scott Lurndal:

> Does the term 'spin lock' have any meaning to you?

Spinlocks don't work in userspace since a thread holding a spinlock
can be scheduled away, thereby keepin others spinning a long time.
Because of that no one uses pure spinlocks, but just mutexes with
limited spinning and a slow path.

Re: Thread-safe initialization of static objects

<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:590a:0:b0:647:2b82:5fd9 with SMTP id ez10-20020ad4590a000000b006472b825fd9mr46576qvb.10.1695228483748;
Wed, 20 Sep 2023 09:48:03 -0700 (PDT)
X-Received: by 2002:a05:6808:178d:b0:3a1:c163:6022 with SMTP id
bg13-20020a056808178d00b003a1c1636022mr1529467oib.4.1695228483422; Wed, 20
Sep 2023 09:48:03 -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: Wed, 20 Sep 2023 09:48:03 -0700 (PDT)
In-Reply-To: <ueeret$2upib$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> <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>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Wed, 20 Sep 2023 16:48:03 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 3717
 by: Michael S - Wed, 20 Sep 2023 16:48 UTC

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:
>
> > Nope, shows your lack of imagination.
> Show me how sleeping works in situation without waiting for a semaphore
> which is part of the slow path of a mutex.

That's approximately what I'd do using old (pre-Vista) Win32 API.
With newer APIs I can probably do better.

static CRITICAL_SECTION st_flagUpdatesCs;
static HANDLE st_initWaitEv;

void guarda_init_before_main()
{ InitializeCriticalSection(&st_flagUpdatesCs);
st_initWaitEv = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
}

int guard_acquire(volatile char* per_object)
{ int ret = 0; // tell to caller that initialization completed
if (*per_object != 1) {
EnterCriticalSection(&st_flagUpdatesCs);
if (*per_object != 1) {
// For sake of brevity let's ignore guarding/abortion
// against recursive initialization of the same object by the same thread
// According to C++ standard it's UB, anyway
while (*per_object & 2) { // init in progress
*per_object |= 4; // announce wait
LeaveCriticalSection(&st_flagUpdatesCs);
WaitForSingleObject(st_initWaitEv, INFINITE);
EnterCriticalSection(&st_flagUpdatesCs);
}
if (*per_object != 1) {
*per_object |= 2; // set init in progress
ret = 1; // tell to caller to perform initialization
}
}
LeaveCriticalSection(&st_flagUpdatesCs);
}
return ret;
}

void guard_release(volatile char* per_object)
{ EnterCriticalSection(&st_flagUpdatesCs);
char val = *per_object;
*per_object = 1; // mark object as initialized
LeaveCriticalSection(&st_flagUpdatesCs);

if (val & 4) // there are waiting threads
PulseEvent(st_initWaitEv); // wake them up
}

Re: Thread-safe initialization of static objects

<ueff0f$32ncr$1@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 11:51:58 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <ueff0f$32ncr$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbe19$2rk0p$2@dont-email.me> <2acKM.719164$U3w1.269152@fx09.iad>
<udbjfo$2s7cn$1@dont-email.me> <RllKM.1028889$SuUf.692033@fx14.iad>
<udcubm$325k4$1@dont-email.me> <wguKM.698884$xMqa.357246@fx12.iad>
<udebu2$3bknb$1@dont-email.me> <X5KKM.218705$ftCb.30173@fx34.iad>
<udgpnl$3tuni$1@dont-email.me> <udi0vu$488m$1@dont-email.me>
<udi9s5$5f8c$1@dont-email.me> <q82LM.1449511$GMN3.1347862@fx16.iad>
<udig8l$6ddc$1@dont-email.me> <zI4LM.170544$uEkc.45136@fx35.iad>
<udk0do$h1e9$1@dont-email.me> <hRjLM.1455228$GMN3.816986@fx16.iad>
<udkoc6$kdis$1@dont-email.me> <udkpmo$knnn$1@dont-email.me>
<1bbc625d-fdc8-4e45-8659-4270ecf897d2n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 20 Sep 2023 18:51:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="3235227"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185f6yhlhIrGItriZiOVJqqHbfzuf5TRHc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:V9pHrPKyUQx6TNzE8VYnDcB38Ng=
In-Reply-To: <1bbc625d-fdc8-4e45-8659-4270ecf897d2n@googlegroups.com>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 20 Sep 2023 18:51 UTC

On 9/20/2023 1:14 AM, Michael S wrote:
> On Sunday, September 10, 2023 at 7:09:11 PM UTC+3, Paavo Helde wrote:
>> 10.09.2023 18:46 Bonita Montero kirjutas:
>>> Am 10.09.2023 um 15:51 schrieb Richard Damon:
>>>
>>>> You don't seem to understand that sincd it CAN be done in a way that
>>>> always works, ...
>>>
>>> A standard shouldn't mandate that this never fails.
>> So suppose the standard committee agrees with you and adds a sentence to
>> the standard:
>>
>> Dynamic initialization of a non-local variable with static storage
>> duration can fail even if its initializing function does not throw, in
>> which case the behavior is ...undefined... / ..unspecified... /
>> ...calling std::terminate() after unspecified time...
>>
>> Which variant do you prefer? And how would this help the programmer who
>> needs to write programs in C++? Should they just avoid creating any
>> static variables at all, in order to not trigger this failure scenario?
>
> Avoidance of creation of function-scope static variables with non-trivial
> constructors is a good guiding principle. Especially so in multithreaded
> environment.
> Of course, it is a good guiding principle for more important reasons than
> one mentioned here.

You got it!!!

Re: Thread-safe initialization of static objects

<ueff33$32ncr$2@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 11:53:22 -0700
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <ueff33$32ncr$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <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>
<AcDOM.64148$%uv8.27450@fx15.iad> <ueevo0$2vkcq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 18:53:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="3235227"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/n081RIkCo+Eo/QSrwHvgBzmXyIlKMte8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:YMctfOwv5Vko1lGU4WGmCUpjep4=
In-Reply-To: <ueevo0$2vkcq$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 20 Sep 2023 18:53 UTC

On 9/20/2023 7:31 AM, Bonita Montero wrote:
> Am 20.09.2023 um 16:20 schrieb Scott Lurndal:
>
>> Does the term 'spin lock' have any meaning to you?
>
> Spinlocks don't work in userspace since a thread holding a spinlock
> can be scheduled away, thereby keepin others spinning a long time.
> Because of that no one uses pure spinlocks, but just mutexes with
> limited spinning and a slow path.
>

So, do actually do have a deep hatred of adaptive mutexes, right?

Re: Thread-safe initialization of static objects

<ueff5s$32ncr$3@dont-email.me>

  copy mid

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

  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: Wed, 20 Sep 2023 11:54:51 -0700
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <ueff5s$32ncr$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>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 20 Sep 2023 18:54:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="3235227"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mVYaHh79QYYrGa19tt0iW7NbcY43mPAs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:5InTTEZP98rfYcubwFDQEDQMqQA=
In-Reply-To: <5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 20 Sep 2023 18:54 UTC

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:
>>
>>> Nope, shows your lack of imagination.
>> Show me how sleeping works in situation without waiting for a semaphore
>> which is part of the slow path of a mutex.
>
> That's approximately what I'd do using old (pre-Vista) Win32 API.
> With newer APIs I can probably do better.
>
> static CRITICAL_SECTION st_flagUpdatesCs;
> static HANDLE st_initWaitEv;
>
> void guarda_init_before_main()
> {
> InitializeCriticalSection(&st_flagUpdatesCs);
> st_initWaitEv = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
> }
>
> int guard_acquire(volatile char* per_object)
> {
> int ret = 0; // tell to caller that initialization completed
> if (*per_object != 1) {
> EnterCriticalSection(&st_flagUpdatesCs);
> if (*per_object != 1) {
> // For sake of brevity let's ignore guarding/abortion
> // against recursive initialization of the same object by the same thread
> // According to C++ standard it's UB, anyway
> while (*per_object & 2) { // init in progress
> *per_object |= 4; // announce wait
> LeaveCriticalSection(&st_flagUpdatesCs);
> WaitForSingleObject(st_initWaitEv, INFINITE);
> EnterCriticalSection(&st_flagUpdatesCs);
> }
> if (*per_object != 1) {
> *per_object |= 2; // set init in progress
> ret = 1; // tell to caller to perform initialization
> }
> }
> LeaveCriticalSection(&st_flagUpdatesCs);
> }
> return ret;
> }
>
> void guard_release(volatile char* per_object)
> {
> EnterCriticalSection(&st_flagUpdatesCs);
> char val = *per_object;
> *per_object = 1; // mark object as initialized
> LeaveCriticalSection(&st_flagUpdatesCs);
>
> if (val & 4) // there are waiting threads
> PulseEvent(st_initWaitEv); // wake them up

^^^^^^^^^^^^^^^^^

PulseEvent has issues.... Take special note of:

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-pulseevent
__________
Note This function is unreliable and should not be used. It exists
mainly for backward compatibility. For more information, see Remarks.
__________

So, well, shit.

> }

Re: Thread-safe initialization of static objects

<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:48d0:0:b0:641:8875:22cb with SMTP id v16-20020ad448d0000000b00641887522cbmr42513qvx.5.1695238567158;
Wed, 20 Sep 2023 12:36:07 -0700 (PDT)
X-Received: by 2002:a05:6808:1309:b0:3ad:ba05:a3be with SMTP id
y9-20020a056808130900b003adba05a3bemr3024277oiv.4.1695238566934; Wed, 20 Sep
2023 12:36:06 -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: Wed, 20 Sep 2023 12:36:06 -0700 (PDT)
In-Reply-To: <ueff5s$32ncr$3@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:f871:9f24:c014:651;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:f871:9f24:c014:651
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>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Wed, 20 Sep 2023 19:36:07 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 7055
 by: Michael S - Wed, 20 Sep 2023 19:36 UTC

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:
> >>
> >>> Nope, shows your lack of imagination.
> >> Show me how sleeping works in situation without waiting for a semaphore
> >> which is part of the slow path of a mutex.
> >
> > That's approximately what I'd do using old (pre-Vista) Win32 API.
> > With newer APIs I can probably do better.
> >
> > static CRITICAL_SECTION st_flagUpdatesCs;
> > static HANDLE st_initWaitEv;
> >
> > void guarda_init_before_main()
> > {
> > InitializeCriticalSection(&st_flagUpdatesCs);
> > st_initWaitEv = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
> > }
> >
> > int guard_acquire(volatile char* per_object)
> > {
> > int ret = 0; // tell to caller that initialization completed
> > if (*per_object != 1) {
> > EnterCriticalSection(&st_flagUpdatesCs);
> > if (*per_object != 1) {
> > // For sake of brevity let's ignore guarding/abortion
> > // against recursive initialization of the same object by the same thread
> > // According to C++ standard it's UB, anyway
> > while (*per_object & 2) { // init in progress
> > *per_object |= 4; // announce wait
> > LeaveCriticalSection(&st_flagUpdatesCs);
> > WaitForSingleObject(st_initWaitEv, INFINITE);
> > EnterCriticalSection(&st_flagUpdatesCs);
> > }
> > if (*per_object != 1) {
> > *per_object |= 2; // set init in progress
> > ret = 1; // tell to caller to perform initialization
> > }
> > }
> > LeaveCriticalSection(&st_flagUpdatesCs);
> > }
> > return ret;
> > }
> >
> > void guard_release(volatile char* per_object)
> > {
> > EnterCriticalSection(&st_flagUpdatesCs);
> > char val = *per_object;
> > *per_object = 1; // mark object as initialized
> > LeaveCriticalSection(&st_flagUpdatesCs);
> >
> > if (val & 4) // there are waiting threads
> > PulseEvent(st_initWaitEv); // wake them up
> ^^^^^^^^^^^^^^^^^
>
> PulseEvent has issues.... Take special note of:
>
> https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-pulseevent
> __________
> Note This function is unreliable and should not be used. It exists
> mainly for backward compatibility. For more information, see Remarks.
> __________
>
> So, well, shit.
>
> > }

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.

The simple, if inelegant, fix is to replace
WaitForSingleObject(st_initWaitEv, INFINITE);
with
WaitForSingleObject(st_initWaitEv, 100);

More theoretically sound, but practically bad fix would be to
replace critical section with mutex. Like that:

static HANDLE st_flagUpdatesMtx;
static HANDLE st_initWaitEv;

void guarda_init_before_main()
{ st_flagUpdatesMtx = CreateMutex(NULL, FALSE, NULL);
st_initWaitEv = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
}

int guard_acquire(volatile char* per_object)
{ int ret = 0; // tell to caller that initialization completed
if (*per_object != 1) {
WaitForSingleObject(st_flagUpdatesMtx, INFINITE); // Enter guarded section
if (*per_object != 1) {
// For sake of brevity let's ignore guarding/abortion
// against recursive initialization of the same object by the same thread
// According to C++ standard it's UB, anyway
while (*per_object & 2) { // init in progress
*per_object |= 4; // announce wait

// combine ReleaseMutex(st_flagUpdatesMtx);
// and WaitForSingleObject(st_initWaitEv, INFINITE);
// in a single call thus avoiding race condition with guard_release()
SignalObjectAndWait(st_flagUpdatesMtx, st_initWaitEv, INFINITE, FALSE);

WaitForSingleObject(st_flagUpdatesMtx, INFINITE); // re-enter guarded section
}
if (*per_object != 1) {
*per_object |= 2; // set init in progress
ret = 1; // tell to caller to perform initialization
}
}
ReleaseMutex(st_flagUpdatesMtx); // Leave guarded section
}
return ret;
}

void guard_release(volatile char* per_object)
{ WaitForSingleObject(st_flagUpdatesMtx, INFINITE); // Enter guarded section
char val = *per_object;
*per_object = 1; // mark object as initialized
ReleaseMutex(st_flagUpdatesMtx); // Leave guarded section

if (val & 4) // there are waiting threads
PulseEvent(st_initWaitEv); // wake them up
}

In practice it is bad solution, because unlike in the first variant, here
we need system calls even in the most common un-contended case.

Naturally, for Vista and later it's all much easier.


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

Pages:123456789101112131415161718192021222324252627
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor