Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

In English, every word can be verbed. Would that it were so in our programming languages.


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
Thread-safe initialization of static objects

<udafjf$2joeq$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Thread-safe initialization of static objects
Date: Wed, 6 Sep 2023 20:15:12 +0200
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <udafjf$2joeq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 6 Sep 2023 18:15:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e0f7816222c6bdb3f46074addb6b82cd";
logging-data="2744794"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rzq00PKnkGhSzYcZPN/azGTIU8EQrBLM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:v3PHleOsYWizqB/UMcL+PIman9Y=
Content-Language: de-DE
 by: Bonita Montero - Wed, 6 Sep 2023 18:15 UTC

With C++11 static local or global data is initialized thread-safe.
This is usually done with double checked locking (DCL). DCL needs
a flag along with the static data which shows if the data already
is initialized and a mutex which guards the initalization.
I assumed that an implementation simply would use a central mutex
for all static data objects since it includes a kernel semaphore,
which is a costly resource.
To find out if this is true I wrote the below application:

#include <iostream>
#include <thread>
#include <vector>

using namespace std;

template<unsigned Thread>
struct SleepAtInitialize
{ SleepAtInitialize()
{
this_thread::sleep_for( 1s );
}
};

int main()
{ auto unroll = []<size_t ... Indices>( index_sequence<Indices ...>, auto
fn )
{
((fn.template operator ()<Indices>()), ...);
};
constexpr unsigned N_THREADS = 10;
vector<jthread> threads;
threads.reserve( N_THREADS );
unroll( make_index_sequence<N_THREADS>(),
[&]<unsigned Thread>()
{
threads.emplace_back( [&]<unsigned IObj>(integral_constant<unsigned,
IObj> )
{
static SleepAtInitialize<IObj> guard;
}, integral_constant<unsigned, Thread>() );
} );

}

SleepAtInitialize<IObj> is instantiated only once per thread with this
code. So the threads don't share a central object. If there would be
a central mutex used the above code would run for about 10s. But the
code does run about one second, i.e. there are indivual mutexes per
each instantiation of SleepAtInitiaize<>. I.e. the creation of the
mutex is also done with the static initialization. The mutex constructor
is noexcept, so mutexes always must be created on their first use. This
is done while the DCL-locked creation of the static object. So at last
static initialization should be declared to throw a system_errror. But
I can't find anything about that in the standard.

Re: Thread-safe initialization of static objects

<1h7KM.66324$Wk53.59929@fx01.iad>

  copy mid

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

  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!fx01.iad.POSTED!not-for-mail
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me>
From: pauldontspamtolk@removeyourself.dontspam.yahoo (Pavel)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17
MIME-Version: 1.0
In-Reply-To: <udafjf$2joeq$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 61
Message-ID: <1h7KM.66324$Wk53.59929@fx01.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Wed, 06 Sep 2023 22:44:13 UTC
Date: Wed, 6 Sep 2023 18:44:06 -0400
X-Received-Bytes: 3116
 by: Pavel - Wed, 6 Sep 2023 22:44 UTC

Bonita Montero wrote:
> With C++11 static local or global data is initialized thread-safe.
> This is  usually done with double checked locking (DCL). DCL needs
> a flag along with the static data which shows if the data already
> is initialized and a mutex which guards the initalization.
Not needed. A test-and-set instruction on a flag -- that is itself
constant-initialized -- is sufficient.

> I assumed that an implementation simply would use a central mutex
> for all static data objects since it includes a kernel semaphore,
> which is a costly resource.
> To find out if this is true I wrote the below application:
>
> #include <iostream>
> #include <thread>
> #include <vector>
>
> using namespace std;
>
> template<unsigned Thread>
> struct SleepAtInitialize
> {
>     SleepAtInitialize()
>     {
>         this_thread::sleep_for( 1s );
>     }
> };
>
> int main()
> {
>     auto unroll = []<size_t ... Indices>( index_sequence<Indices ...>,
> auto fn )
>     {
>         ((fn.template operator ()<Indices>()), ...);
>     };
>     constexpr unsigned N_THREADS = 10;
>     vector<jthread> threads;
>     threads.reserve( N_THREADS );
>     unroll( make_index_sequence<N_THREADS>(),
>         [&]<unsigned Thread>()
>         {
>             threads.emplace_back( [&]<unsigned
> IObj>(integral_constant<unsigned, IObj> )
>                 {
>                     static SleepAtInitialize<IObj> guard;
>                 }, integral_constant<unsigned, Thread>() );
>         } );
>
> }
>
> SleepAtInitialize<IObj> is instantiated only once per thread with this
> code. So the threads don't share a central object. If there would be
> a central mutex used the above code would run for about 10s. But the
> code does run about one second, i.e. there are indivual mutexes per
> each instantiation of SleepAtInitiaize<>. I.e. the creation of the
> mutex is also done with the static initialization. The mutex constructor
> is noexcept, so mutexes always must be created on their first use. This
> is done while the DCL-locked creation of the static object. So at last
> static initialization should be declared to throw a system_errror. But
> I can't find anything about that in the standard.

Re: Thread-safe initialization of static objects

<udbe19$2rk0p$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 04:54:35 +0200
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <udbe19$2rk0p$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 7 Sep 2023 02:54:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3002393"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8ZqPJ9/Qg0Qe/n492Y1n94/u7ftxorAM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:6Ew+DekcDvtTZq7yL2cyuBlCuRQ=
In-Reply-To: <1h7KM.66324$Wk53.59929@fx01.iad>
Content-Language: de-DE
 by: Bonita Montero - Thu, 7 Sep 2023 02:54 UTC

Am 07.09.2023 um 00:44 schrieb Pavel:
> Bonita Montero wrote:
>> With C++11 static local or global data is initialized thread-safe.
>> This is  usually done with double checked locking (DCL). DCL needs
>> a flag along with the static data which shows if the data already
>> is initialized and a mutex which guards the initalization.

> Not needed. A test-and-set instruction on a flag -- that is itself
> constant-initialized -- is sufficient.

Using only one flag would require spin-locking. However, spin-locking
is not possible in userspace because a thread holding a spinlock could
keep other threads spinning for a long time. Therefore, there is no
getting around a solution with a mutex. And creation and mutex synchro-
nization may fail.

Re: Thread-safe initialization of static objects

<2acKM.719164$U3w1.269152@fx09.iad>

  copy mid

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

  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!fx09.iad.POSTED!not-for-mail
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbe19$2rk0p$2@dont-email.me>
From: pauldontspamtolk@removeyourself.dontspam.yahoo (Pavel)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17
MIME-Version: 1.0
In-Reply-To: <udbe19$2rk0p$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 28
Message-ID: <2acKM.719164$U3w1.269152@fx09.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Thu, 07 Sep 2023 04:18:06 UTC
Date: Thu, 7 Sep 2023 00:18:06 -0400
X-Received-Bytes: 2023
 by: Pavel - Thu, 7 Sep 2023 04:18 UTC

Bonita Montero wrote:
> Am 07.09.2023 um 00:44 schrieb Pavel:
>> Bonita Montero wrote:
>>> With C++11 static local or global data is initialized thread-safe.
>>> This is  usually done with double checked locking (DCL). DCL needs
>>> a flag along with the static data which shows if the data already
>>> is initialized and a mutex which guards the initalization.
>
>> Not needed. A test-and-set instruction on a flag -- that is itself
>> constant-initialized -- is sufficient.
>
> Using only one flag would require spin-locking. However, spin-locking
> is not possible in userspace
not true, a loop with yield in the body is very possible.

> because a thread holding a spinlock could
> keep other threads spinning for a long time. Therefore, there is no
> getting around a solution with a mutex.
does not have to be a mutex, can be call_once, a semaphore, anything,
actually.

> And creation and mutex synchro-
> nization may fail.
If initialization synchronization fails, the initialization can catch
and terminate. No need to throw a system error. No need to use C++
synchronization primitives in the initialization code either; nothing
prevents the implementation from being implemented in a
platform-specific manner.

Re: Thread-safe initialization of static objects

<udbjfo$2s7cn$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 06:27:38 +0200
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <udbjfo$2s7cn$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 04:27:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3022231"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+daMqzfMirDUSkyVlbI2/LQMUXBFPMIec="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:YcpeZOXJ8xClAAJU6mRQAvLXuO0=
In-Reply-To: <2acKM.719164$U3w1.269152@fx09.iad>
Content-Language: de-DE
 by: Bonita Montero - Thu, 7 Sep 2023 04:27 UTC

Am 07.09.2023 um 06:18 schrieb Pavel:

> not true, a loop with yield in the body is very possible.

No one would accept that because that would make the waiters to wait
much more longer than necessary.

> does not have to be a mutex, can be call_once, a semaphore, anything,
> actually.

Every applicable facility for that would rely on kernel-synchronization.
You'd need to create a binary semaphore for that and you need so syn-
chronize on that; both may fail.

> If initialization synchronization fails, the initialization can catch
> and terminate. ...

Nothing like that is specified.

Re: Thread-safe initialization of static objects

<udbqs7$2t34f$1@dont-email.me>

  copy mid

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

  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.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Wed, 6 Sep 2023 23:33:40 -0700
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <udbqs7$2t34f$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 7 Sep 2023 06:33:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65de853b7ed379063ff15dfaaa9ff58f";
logging-data="3050639"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Pp73bX3p2jjj5WSUhRv+yHrqY7bl0PSQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:eT8qvW6CnacgzTKJoMX3MUx07bs=
Content-Language: en-US
In-Reply-To: <1h7KM.66324$Wk53.59929@fx01.iad>
 by: Chris M. Thomasson - Thu, 7 Sep 2023 06:33 UTC

On 9/6/2023 3:44 PM, Pavel wrote:
> Bonita Montero wrote:
>> With C++11 static local or global data is initialized thread-safe.
>> This is  usually done with double checked locking (DCL). DCL needs
>> a flag along with the static data which shows if the data already
>> is initialized and a mutex which guards the initalization.
> Not needed. A test-and-set instruction on a flag -- that is itself
> constant-initialized -- is sufficient.
[...]

You also need to use the appropriate memory barriers. An acquire after
the first check, and a release before making the object visible.

// pseudo code, its been a while.
// Damn, I used to work with threads all of the time.
___________________________
static foo* g_foo = nullptr;

foo* local = g_foo; // atomic load

if (! local)
{ hash_lock(&g_foo);

local = g_foo; // atomic load

if (! local)
{
local = new foo;

// release mb #LoadStore | #StoreStore

g_foo = local; // atomic store

}

else
{
// acquire mb #LoadStore | #LoadLoad
}

hash_unlock(&g_foo);
}

else
{ // acquire mb #LoadStore | #LoadLoad
}

local->foobar();
___________________________

Iirc, that is a bare bones DCL.

Re: Thread-safe initialization of static objects

<udbs1f$2t79o$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 08:53:37 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <udbs1f$2t79o$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbqs7$2t34f$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 06:53:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3054904"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6mZ08FSLBUCQyVBJqOZxpoyt/yfQJpkQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qLpgYWhM+vbdZPNsqer+IWaKmgw=
In-Reply-To: <udbqs7$2t34f$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 7 Sep 2023 06:53 UTC

Am 07.09.2023 um 08:33 schrieb Chris M. Thomasson:

> You also need to use the appropriate memory barriers. ...
That's all inside the Wikipedia example about DCL. But the discussion
was about whether the thead safe-initialization may fail before or
after the object's constructor is called because the mutex creation
or the kernel-synchronization may fail.

Re: Thread-safe initialization of static objects

<udbtda$2tdm8$1@dont-email.me>

  copy mid

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

  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.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 00:16:57 -0700
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <udbtda$2tdm8$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbqs7$2t34f$1@dont-email.me> <udbs1f$2t79o$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 07:16:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65de853b7ed379063ff15dfaaa9ff58f";
logging-data="3061448"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yLu3PLfx3V3mFERf2/DTTIyGE5sXTPGg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:YPjWBKN+4XXdRwRCNgjVZYFRyPs=
In-Reply-To: <udbs1f$2t79o$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 7 Sep 2023 07:16 UTC

On 9/6/2023 11:53 PM, Bonita Montero wrote:
> Am 07.09.2023 um 08:33 schrieb Chris M. Thomasson:
>
>> You also need to use the appropriate memory barriers. ...
> That's all inside the Wikipedia example about DCL. But the discussion
> was about whether the thead safe-initialization may fail before or
> after the object's constructor is called because the mutex creation
> or the kernel-synchronization may fail.

Usually, the hash table of mutexes is created before any of the programs
logic is executed...

Re: Thread-safe initialization of static objects

<udc6n0$2uo4t$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: eesnimi@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 12:55:43 +0300
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <udc6n0$2uo4t$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 7 Sep 2023 09:55:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="805d92aeec743aa1f8017ccbc75459e1";
logging-data="3104925"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/B+kLIa28e/IP33Lx4DBxcheR0iyoMYac="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:KNkjONGranWf7CSDn7RT+xy9jmg=
Content-Language: en-US
In-Reply-To: <udafjf$2joeq$1@dont-email.me>
 by: Paavo Helde - Thu, 7 Sep 2023 09:55 UTC

06.09.2023 21:15 Bonita Montero kirjutas:
> With C++11 static local or global data is initialized thread-safe.
> This is  usually done with double checked locking (DCL). DCL needs
> a flag along with the static data which shows if the data already
> is initialized and a mutex which guards the initalization.
> I assumed that an implementation simply would use a central mutex
> for all static data objects since it includes a kernel semaphore,
> which is a costly resource.
> To find out if this is true I wrote the below application:
[...]
> SleepAtInitialize<IObj> is instantiated only once per thread with this
> code. So the threads don't share a central object. If there would be
> a central mutex used the above code would run for about 10s. But the
> code does run about one second,

There is a note in the standard: "[Note: This definition permits
initialization of a sequence of ordered variables concurrently with
another sequence. —end note]"

i.e. there are indivual mutexes per
> each instantiation of SleepAtInitiaize<>. I.e. the creation of the
> mutex is also done with the static initialization. The mutex constructor
> is noexcept, so mutexes always must be created on their first use. This
> is done while the DCL-locked creation of the static object. So at last
> static initialization should be declared to throw a system_errror. But
> I can't find anything about that in the standard.

Some debugging with VS2022 seems to indicate it is using a Windows
critical section for thread-safe statics initialization.
EnterCriticalSection() does not return any error code and of course does
not throw any C++ exceptions either, so it is supposed to never fail.

Yes, it's true it can throw a Windows structured exception
EXCEPTION_POSSIBLE_DEADLOCK (after 30 days by default). But this would
be considered as a fault in the program. This is what the C++ standard
says about deadlocks (again a footnote):

"The implementation must not introduce any deadlock around execution of
the initializer. Deadlocks might still be caused by the program logic;
the implementation need only avoid deadlocks due to its own
synchronization operations."

So I gather that in case the thread-safe static init synchronization
fails, there must be a bug in the implementation. No C++ exceptions
would be thrown anyway.

Re: Thread-safe initialization of static objects

<udcf19$2vuf5$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 14:17:47 +0200
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <udcf19$2vuf5$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbqs7$2t34f$1@dont-email.me> <udbs1f$2t79o$1@dont-email.me>
<udbtda$2tdm8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 12:17:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3144165"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AHXsrVkRaG84wrSM3oeZLA7w4GI7y7ec="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qgHfhRg3ciLjA4/eTm3fvegdwhU=
Content-Language: de-DE
In-Reply-To: <udbtda$2tdm8$1@dont-email.me>
 by: Bonita Montero - Thu, 7 Sep 2023 12:17 UTC

Am 07.09.2023 um 09:16 schrieb Chris M. Thomasson:

> Usually, the hash table of mutexes is created before
> any of the programs logic is executed...

That sounds too complex for me. Creating individual mutexes on
demand would be o.k. and I think a lock-free stack with a pool
of mutexes would be fancy. A hashtable is too slow for that.

Re: Thread-safe initialization of static objects

<RllKM.1028889$SuUf.692033@fx14.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx14.iad.POSTED!not-for-mail
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
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>
From: pauldontspamtolk@removeyourself.dontspam.yahoo (Pavel)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17
MIME-Version: 1.0
In-Reply-To: <udbjfo$2s7cn$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 44
Message-ID: <RllKM.1028889$SuUf.692033@fx14.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Thu, 07 Sep 2023 14:45:05 UTC
Date: Thu, 7 Sep 2023 10:44:59 -0400
X-Received-Bytes: 2942
 by: Pavel - Thu, 7 Sep 2023 14:44 UTC

Bonita Montero wrote:
> Am 07.09.2023 um 06:18 schrieb Pavel:
>
>> not true, a loop with yield in the body is very possible.
>
> No one would accept that because that would make the waiters to wait
> much more longer than necessary.
How so? Waiters will wait for the time of initialization. The
initializing thread will be yielded to and receive virtually as many and
as complete time slices as it would under any other scheduling
discipline so it will complete its job in same or virtually same time
(actually, the higher system contention level is the more efficient
user-space waiting with yield becomes). Hence the time waiters will wait
is exactly or virtually same as when using mutex.

Also it should be taken into account that all above (and below) is only
relevant to the rare case when the initialization is contended; in other
words, "no one would accept" would be an exaggeration of the year even
if your speculation on "wait much longer" were true -- which it isn't.

>
>> does not have to be a mutex, can be call_once, a semaphore, anything,
>> actually.
>
> Every applicable facility for that would rely on kernel-synchronization.
> You'd need to create a binary semaphore for that and you need so syn-
> chronize on that; both may fail.
try_lock in a loop with a sleep or yield wouldn't fail. But as said
above, it's not needed.

Regardless, your argument assumes too much C++-morphism in the
implementation whereas the implementation can use any platform-specific
approach available to it. E.g. pthread_once on Linux does not fail if
given valid arguments (which C++ implementation can provide). Other
platforms may have tools of their own to do the job.

>
>> If initialization synchronization fails, the initialization can catch
>> and terminate. ...
>
> Nothing like that is specified.
Correct, the above was wrong. But initialization can catch and try
again. This does not have to be specified as it is not observable from
outside.

Re: Thread-safe initialization of static objects

<udcubm$325k4$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 18:39:20 +0200
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <udcubm$325k4$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 16:39:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3217028"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19K9sbjG7cnrbcItmK2pvc3K37kQQmsgFM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:HzhdD0Xjm94wgZTlN5x4q6Tyd+s=
Content-Language: de-DE
In-Reply-To: <RllKM.1028889$SuUf.692033@fx14.iad>
 by: Bonita Montero - Thu, 7 Sep 2023 16:39 UTC

Am 07.09.2023 um 16:44 schrieb Pavel:

> How so? Waiters will wait for the time of initialization.
> The initializing thread will be yielded ...

Initializing is usually much faster than a whole timeslice,
so yielding would be incacceptabel. That's just a stupid idea.

> try_lock in a loop with a sleep or yield wouldn't fail. ...

Do you really think someone would accept spinning with that ?
This means that an initializing thread which is scheduled away
while holding the mutex might keep other threads spinning for
a long time.

> Regardless, your argument assumes too much C++-morphism in the
> implementation whereas the implementation can use any platform
> -specific approach available to it. E.g. pthread_once on Linux
> does not fail if given valid arguments ...

pthread_once could be implemented with a single central semaphore for
all operations and if the implementers know that the synchronization
itself doesn't fail and the semphore is pre-allocated by the runtime
it's possible to survive that synchronization without error.
But check that code:

#include <iostream>
#include <thread>
#include <vector>

using namespace std;

template<unsigned Thread>
struct SleepAtInitialize
{ SleepAtInitialize()
{
this_thread::sleep_for( 1s );
}
};

int main()
{ auto unroll = []<size_t ... Indices>( index_sequence<Indices ...>, auto
fn )
{
((fn.template operator ()<Indices>()), ...);
};
constexpr unsigned N_THREADS = 10;
vector<jthread> threads;
threads.reserve( N_THREADS );
unroll( make_index_sequence<N_THREADS>(),
[&]<unsigned Thread>()
{
threads.emplace_back(
[&]<unsigned IObj>( integral_constant<unsigned, IObj> )
{
static SleepAtInitialize<IObj> guard;
}, integral_constant<unsigned, Thread>() );
} );

}

This code runs with individual mutexes per object, i.e. the time
taken is about one second (with MSVC, libc++ and libstdc++). So
when individual mutexes are used the initialization may fail.

> Correct, the above was wrong. But initialization can catch and try
> again. ...

That a bumbler solution.

Re: Thread-safe initialization of static objects

<kKnKM.340818$ens9.154535@fx45.iad>

  copy mid

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

  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!fx45.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@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> <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>
Lines: 14
Message-ID: <kKnKM.340818$ens9.154535@fx45.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 07 Sep 2023 17:27:44 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 07 Sep 2023 17:27:44 GMT
X-Received-Bytes: 1394
 by: Scott Lurndal - Thu, 7 Sep 2023 17:27 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 07.09.2023 um 16:44 schrieb Pavel:
>
>> How so? Waiters will wait for the time of initialization.
>> The initializing thread will be yielded ...
>
>Initializing is usually much faster than a whole timeslice,
>so yielding would be incacceptabel. That's just a stupid idea.

So tell us, on which operating systems will there be more than
one thread running when application static objects are
initialized (which happens generally before the application
'main' function is called, and thus before the application
has a chance to create any threads)?

Re: Thread-safe initialization of static objects

<XioKM.273960$8_8a.80813@fx48.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx48.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> <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> <kKnKM.340818$ens9.154535@fx45.iad>
From: Richard@Damon-Family.org (Richard Damon)
In-Reply-To: <kKnKM.340818$ens9.154535@fx45.iad>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 22
Message-ID: <XioKM.273960$8_8a.80813@fx48.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, 7 Sep 2023 11:06:46 -0700
X-Received-Bytes: 2017
 by: Richard Damon - Thu, 7 Sep 2023 18:06 UTC

On 9/7/23 10:27 AM, Scott Lurndal wrote:
> Bonita Montero <Bonita.Montero@gmail.com> writes:
>> Am 07.09.2023 um 16:44 schrieb Pavel:
>>
>>> How so? Waiters will wait for the time of initialization.
>>> The initializing thread will be yielded ...
>>
>> Initializing is usually much faster than a whole timeslice,
>> so yielding would be incacceptabel. That's just a stupid idea.
>
> So tell us, on which operating systems will there be more than
> one thread running when application static objects are
> initialized (which happens generally before the application
> 'main' function is called, and thus before the application
> has a chance to create any threads)?

While GLOBAL static objects get initialized before main starts, function
local static objects don't get initialized until the first call of the
function. These will need some synchronization if the function is called
from multiple threads at "the same time".

Also, some global object could start up a thread in its constructor.

Re: Thread-safe initialization of static objects

<udd4gg$334c7$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 20:24:18 +0200
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <udd4gg$334c7$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> <kKnKM.340818$ens9.154535@fx45.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 18:24:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3248519"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18RQDFI/VN4ExtmTsKq5PGU13d9bEwbF8I="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:9wNVV98d8JKWXDi/8UABPd5jhFw=
Content-Language: de-DE
In-Reply-To: <kKnKM.340818$ens9.154535@fx45.iad>
 by: Bonita Montero - Thu, 7 Sep 2023 18:24 UTC

Am 07.09.2023 um 19:27 schrieb Scott Lurndal:

> So tell us, on which operating systems will there be more
> than one thread running when application static objects are
> initialized (which happens generally before the application
> 'main' function is called, and thus before the application
> has a chance to create any threads)?

I've shown with my code that each static object gets its own mutex
with MSVC, libstdc++ and libc++. Here it is again in a simplified
version:

#include <iostream>
#include <thread>
#include <vector>

using namespace std;

int main()
{ struct SleepAtInitialize
{
SleepAtInitialize()
{
this_thread::sleep_for( 1s );
}
};
auto unroll = []<size_t ... Indices>( index_sequence<Indices ...>, auto
fn )
{
((fn.template operator ()<Indices>()), ...);
};
constexpr unsigned N_THREADS = 10;
vector<jthread> threads;
threads.reserve( N_THREADS );
unroll( make_index_sequence<N_THREADS>(),
[&]<unsigned Thread>()
{
threads.emplace_back(
[&]<unsigned IObj>( integral_constant<unsigned, IObj> )
{
static SleepAtInitialize guard;
}, integral_constant<unsigned, Thread>() );
} );

}

The code runs about one second with all three implementations,
so ther's a mutex per statically initialized object.

Re: Thread-safe initialization of static objects

<udd6vm$33f8r$1@dont-email.me>

  copy mid

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

  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.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 12:06:29 -0700
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <udd6vm$33f8r$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbqs7$2t34f$1@dont-email.me> <udbs1f$2t79o$1@dont-email.me>
<udbtda$2tdm8$1@dont-email.me> <udcf19$2vuf5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 19:06:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65de853b7ed379063ff15dfaaa9ff58f";
logging-data="3259675"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bJNY4VByJBiVcuW+FRDv0BmJ3DXNa+os="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:i6s+J1T+2k01k7UvNsExx14Ifhg=
Content-Language: en-US
In-Reply-To: <udcf19$2vuf5$1@dont-email.me>
 by: Chris M. Thomasson - Thu, 7 Sep 2023 19:06 UTC

On 9/7/2023 5:17 AM, Bonita Montero wrote:
> Am 07.09.2023 um 09:16 schrieb Chris M. Thomasson:
>
>> Usually, the hash table of mutexes is created before
>> any of the programs logic is executed...
>
> That sounds too complex for me. Creating individual mutexes on
> demand would be o.k. and I think a lock-free stack with a pool
> of mutexes would be fancy. A hashtable is too slow for that.
>
>

No. A simple hash of a pointer into an index works out okay, not too
slow at all. Fwiw, check this out, tell me what you think:

https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/wwYQCG2hAwAJ

It is a quick and crude example simulation of one way to do it. The hash
lock table is created before program logic is executed.

Any thoughts?

Also, we are talking about a slow path wrt DCL.

Re: Thread-safe initialization of static objects

<udd7q8$33i0n$1@dont-email.me>

  copy mid

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

  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.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 12:20:39 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <udd7q8$33i0n$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> <kKnKM.340818$ens9.154535@fx45.iad>
<XioKM.273960$8_8a.80813@fx48.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 19:20:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65de853b7ed379063ff15dfaaa9ff58f";
logging-data="3262487"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+eqfkjaIBpXdLSeRsJAG6XmWK0umhE4Xo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:TQJgcT2JYi/H1ueO1RHAdQhafs4=
In-Reply-To: <XioKM.273960$8_8a.80813@fx48.iad>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 7 Sep 2023 19:20 UTC

On 9/7/2023 11:06 AM, Richard Damon wrote:
> On 9/7/23 10:27 AM, Scott Lurndal wrote:
>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>> Am 07.09.2023 um 16:44 schrieb Pavel:
>>>
>>>> How so? Waiters will wait for the time of initialization.
>>>> The initializing thread will be yielded ...
>>>
>>> Initializing is usually much faster than a whole timeslice,
>>> so yielding would be incacceptabel. That's just a stupid idea.
>>
>> So tell us, on which operating systems will there be more than
>> one thread running when application static objects are
>> initialized (which happens generally before the application
>> 'main' function is called, and thus before the application
>> has a chance to create any threads)?
>
> While GLOBAL static objects get initialized before main starts, function
> local static objects don't get initialized until the first call of the
> function. These will need some synchronization if the function is called
> from multiple threads at "the same time".

A simple global hash lock scheme is where we can hash addresses directly
into a static locking table. The lock table is created _before_ any
program logic is executed.

https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/wwYQCG2hAwAJ

> Also, some global object could start up a thread in its constructor.

YIKES! Shit. I have had to debug other peoples code that did this. Many
points of errors... One was a rather common peach of a bug. The
constructor would create a thread that would in turn call into a virtual
function and start using the object before its constructor was
completed. A massive race condition, nasty ones!

Re: Thread-safe initialization of static objects

<udd87f$33lr1$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 21:27:45 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <udd87f$33lr1$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbqs7$2t34f$1@dont-email.me> <udbs1f$2t79o$1@dont-email.me>
<udbtda$2tdm8$1@dont-email.me> <udcf19$2vuf5$1@dont-email.me>
<udd6vm$33f8r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 19:27:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3266401"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/xMFywV1VtVIruu2NOVT1F5bfT/g2mL8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xy9YseSY/ZlHRewD4QGPi0ZtGcg=
Content-Language: de-DE
In-Reply-To: <udd6vm$33f8r$1@dont-email.me>
 by: Bonita Montero - Thu, 7 Sep 2023 19:27 UTC

Am 07.09.2023 um 21:06 schrieb Chris M. Thomasson:

> No. A simple hash of a pointer into an index works out okay, not
> too slow at all. Fwiw, check this out, tell me what you think.

Then the number of mutexes would be fixed.

Re: Thread-safe initialization of static objects

<udd90h$33peq$1@dont-email.me>

  copy mid

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

  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.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 12:41:04 -0700
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <udd90h$33peq$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbqs7$2t34f$1@dont-email.me> <udbs1f$2t79o$1@dont-email.me>
<udbtda$2tdm8$1@dont-email.me> <udcf19$2vuf5$1@dont-email.me>
<udd6vm$33f8r$1@dont-email.me> <udd87f$33lr1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 7 Sep 2023 19:41:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65de853b7ed379063ff15dfaaa9ff58f";
logging-data="3270106"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3+3/pMOdQve6lxeO/1GWMnKeLmZ6yuKM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:YicP5J7vIJOIl1aUT43m8cQtPXA=
Content-Language: en-US
In-Reply-To: <udd87f$33lr1$1@dont-email.me>
 by: Chris M. Thomasson - Thu, 7 Sep 2023 19:41 UTC

On 9/7/2023 12:27 PM, Bonita Montero wrote:
> Am 07.09.2023 um 21:06 schrieb Chris M. Thomasson:
>
>> No. A simple hash of a pointer into an index works out okay, not
>> too  slow at all. Fwiw, check this out, tell me what you think.
>
> Then the number of mutexes would be fixed.
>

Yup. It uses address based hashing into the fixed mutex table. It can be
used to simulate atomics when:

https://en.cppreference.com/w/cpp/atomic/atomic/is_lock_free

is false.

Re: Thread-safe initialization of static objects

<ARpKM.1180716$TPw2.772076@fx17.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx17.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> <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> <kKnKM.340818$ens9.154535@fx45.iad>
<XioKM.273960$8_8a.80813@fx48.iad> <udd7q8$33i0n$1@dont-email.me>
From: Richard@Damon-Family.org (Richard Damon)
In-Reply-To: <udd7q8$33i0n$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 44
Message-ID: <ARpKM.1180716$TPw2.772076@fx17.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, 7 Sep 2023 12:52:00 -0700
X-Received-Bytes: 3140
 by: Richard Damon - Thu, 7 Sep 2023 19:52 UTC

On 9/7/23 12:20 PM, Chris M. Thomasson wrote:
> On 9/7/2023 11:06 AM, Richard Damon wrote:
>> On 9/7/23 10:27 AM, Scott Lurndal wrote:
>>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>> Am 07.09.2023 um 16:44 schrieb Pavel:
>>>>
>>>>> How so? Waiters will wait for the time of initialization.
>>>>> The initializing thread will be yielded ...
>>>>
>>>> Initializing is usually much faster than a whole timeslice,
>>>> so yielding would be incacceptabel. That's just a stupid idea.
>>>
>>> So tell us, on which operating systems will there be more than
>>> one thread running when application static objects are
>>> initialized (which happens generally before the application
>>> 'main' function is called, and thus before the application
>>> has a chance to create any threads)?
>>
>> While GLOBAL static objects get initialized before main starts,
>> function local static objects don't get initialized until the first
>> call of the function. These will need some synchronization if the
>> function is called from multiple threads at "the same time".
>
> A simple global hash lock scheme is where we can hash addresses directly
> into a static locking table. The lock table is created _before_ any
> program logic is executed.
>
> https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/wwYQCG2hAwAJ
>
>
>> Also, some global object could start up a thread in its constructor.
>
> YIKES! Shit. I have had to debug other peoples code that did this. Many
> points of errors... One was a rather common peach of a bug. The
> constructor would create a thread that would in turn call into a virtual
> function and start using the object before its constructor was
> completed. A massive race condition, nasty ones!
>

Yes, such a thread needs to understand that the system isn't fully
configured. And yes, a base class creating a thread needs that thread to
understand that the object isn't fully created yet and do something to
handle that issue (which likely requires some help from the most derived
class).

Re: Thread-safe initialization of static objects

<udda1r$33t5b$1@dont-email.me>

  copy mid

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

  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.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 12:58:50 -0700
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <udda1r$33t5b$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> <kKnKM.340818$ens9.154535@fx45.iad>
<XioKM.273960$8_8a.80813@fx48.iad> <udd7q8$33i0n$1@dont-email.me>
<ARpKM.1180716$TPw2.772076@fx17.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 19:58:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="65de853b7ed379063ff15dfaaa9ff58f";
logging-data="3273899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/T6sxs9eL55TFDyS7KURumvN2sKJnt7XA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:SULmTn6hJfGf+8sDrmGHasd6qiM=
In-Reply-To: <ARpKM.1180716$TPw2.772076@fx17.iad>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 7 Sep 2023 19:58 UTC

On 9/7/2023 12:52 PM, Richard Damon wrote:
> On 9/7/23 12:20 PM, Chris M. Thomasson wrote:
>> On 9/7/2023 11:06 AM, Richard Damon wrote:
>>> On 9/7/23 10:27 AM, Scott Lurndal wrote:
>>>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>>> Am 07.09.2023 um 16:44 schrieb Pavel:
>>>>>
>>>>>> How so? Waiters will wait for the time of initialization.
>>>>>> The initializing thread will be yielded ...
>>>>>
>>>>> Initializing is usually much faster than a whole timeslice,
>>>>> so yielding would be incacceptabel. That's just a stupid idea.
>>>>
>>>> So tell us, on which operating systems will there be more than
>>>> one thread running when application static objects are
>>>> initialized (which happens generally before the application
>>>> 'main' function is called, and thus before the application
>>>> has a chance to create any threads)?
>>>
>>> While GLOBAL static objects get initialized before main starts,
>>> function local static objects don't get initialized until the first
>>> call of the function. These will need some synchronization if the
>>> function is called from multiple threads at "the same time".
>>
>> A simple global hash lock scheme is where we can hash addresses
>> directly into a static locking table. The lock table is created
>> _before_ any program logic is executed.
>>
>> https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/wwYQCG2hAwAJ
>>
>>
>>> Also, some global object could start up a thread in its constructor.
>>
>> YIKES! Shit. I have had to debug other peoples code that did this.
>> Many points of errors... One was a rather common peach of a bug. The
>> constructor would create a thread that would in turn call into a
>> virtual function and start using the object before its constructor was
>> completed. A massive race condition, nasty ones!
>>
>
> Yes, such a thread needs to understand that the system isn't fully
> configured. And yes, a base class creating a thread needs that thread to
> understand that the object isn't fully created yet and do something to
> handle that issue (which likely requires some help from the most derived
> class).

I actually got to a point where I said no creating threads in
constructors! I have seen to many problems. Create the object _first_,
then expose it to a thread, or create a thread that works with the
_fully_ constructed object. The types of race conditions I have had to
deal with wrt objects creating threads in constructors have tended to be
rather nasty in nature...

Re: Thread-safe initialization of static objects

<udda40$33ub2$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 7 Sep 2023 22:00:03 +0200
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <udda40$33ub2$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbqs7$2t34f$1@dont-email.me> <udbs1f$2t79o$1@dont-email.me>
<udbtda$2tdm8$1@dont-email.me> <udcf19$2vuf5$1@dont-email.me>
<udd6vm$33f8r$1@dont-email.me> <udd87f$33lr1$1@dont-email.me>
<udd90h$33peq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 7 Sep 2023 20:00:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="707347a35c765578889e4f3b790e82e5";
logging-data="3275106"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1oLH0w92mFE2Mr0WYtN4hvpz8x13UjB8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:oWwR4e06dwjTYJQSQJ60xYGk1PY=
In-Reply-To: <udd90h$33peq$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 7 Sep 2023 20:00 UTC

Am 07.09.2023 um 21:41 schrieb Chris M. Thomasson:

> Yup. It uses address based hashing into the fixed mutex table.

I guess that's not used at all in the runtimes.

Re: Thread-safe initialization of static objects

<NqqKM.1138349$AsA.720273@fx18.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx18.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> <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> <kKnKM.340818$ens9.154535@fx45.iad>
<XioKM.273960$8_8a.80813@fx48.iad> <udd7q8$33i0n$1@dont-email.me>
<ARpKM.1180716$TPw2.772076@fx17.iad> <udda1r$33t5b$1@dont-email.me>
From: Richard@Damon-Family.org (Richard Damon)
In-Reply-To: <udda1r$33t5b$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 65
Message-ID: <NqqKM.1138349$AsA.720273@fx18.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, 7 Sep 2023 13:31:40 -0700
X-Received-Bytes: 4448
 by: Richard Damon - Thu, 7 Sep 2023 20:31 UTC

On 9/7/23 12:58 PM, Chris M. Thomasson wrote:
> On 9/7/2023 12:52 PM, Richard Damon wrote:
>> On 9/7/23 12:20 PM, Chris M. Thomasson wrote:
>>> On 9/7/2023 11:06 AM, Richard Damon wrote:
>>>> On 9/7/23 10:27 AM, Scott Lurndal wrote:
>>>>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>>>> Am 07.09.2023 um 16:44 schrieb Pavel:
>>>>>>
>>>>>>> How so? Waiters will wait for the time of initialization.
>>>>>>> The initializing thread will be yielded ...
>>>>>>
>>>>>> Initializing is usually much faster than a whole timeslice,
>>>>>> so yielding would be incacceptabel. That's just a stupid idea.
>>>>>
>>>>> So tell us, on which operating systems will there be more than
>>>>> one thread running when application static objects are
>>>>> initialized (which happens generally before the application
>>>>> 'main' function is called, and thus before the application
>>>>> has a chance to create any threads)?
>>>>
>>>> While GLOBAL static objects get initialized before main starts,
>>>> function local static objects don't get initialized until the first
>>>> call of the function. These will need some synchronization if the
>>>> function is called from multiple threads at "the same time".
>>>
>>> A simple global hash lock scheme is where we can hash addresses
>>> directly into a static locking table. The lock table is created
>>> _before_ any program logic is executed.
>>>
>>> https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/wwYQCG2hAwAJ
>>>
>>>
>>>> Also, some global object could start up a thread in its constructor.
>>>
>>> YIKES! Shit. I have had to debug other peoples code that did this.
>>> Many points of errors... One was a rather common peach of a bug. The
>>> constructor would create a thread that would in turn call into a
>>> virtual function and start using the object before its constructor
>>> was completed. A massive race condition, nasty ones!
>>>
>>
>> Yes, such a thread needs to understand that the system isn't fully
>> configured. And yes, a base class creating a thread needs that thread
>> to understand that the object isn't fully created yet and do something
>> to handle that issue (which likely requires some help from the most
>> derived class).
>
> I actually got to a point where I said no creating threads in
> constructors! I have seen to many problems. Create the object _first_,
> then expose it to a thread, or create a thread that works with the
> _fully_ constructed object. The types of race conditions I have had to
> deal with wrt objects creating threads in constructors have tended to be
> rather nasty in nature...

I have ONE class that does this, but it is for a somewhat specific
embedded environment with deterministic scheduling and just a single
core, so the thread is created with a lower priority then the creator so
it can't start running until the creator blocks, which it isn't allowed
to do until the constructor finishes. It also is normally used early in
the program before the "OS" is turned on, so the created thread can't
actually run until the OS is turned on.

But, for the more general case where you can't control that, (under more
"normal" conditions on the typical "powerful" computer), yes, that would
be a risky thing to be doing.

Re: Thread-safe initialization of static objects

<V1rKM.216833$QQFb.22297@fx38.iad>

  copy mid

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

  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!fx38.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@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> <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> <kKnKM.340818$ens9.154535@fx45.iad> <XioKM.273960$8_8a.80813@fx48.iad> <udd7q8$33i0n$1@dont-email.me> <ARpKM.1180716$TPw2.772076@fx17.iad> <udda1r$33t5b$1@dont-email.me>
Lines: 105
Message-ID: <V1rKM.216833$QQFb.22297@fx38.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 07 Sep 2023 21:13:25 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 07 Sep 2023 21:13:25 GMT
X-Received-Bytes: 5480
 by: Scott Lurndal - Thu, 7 Sep 2023 21:13 UTC

"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>On 9/7/2023 12:52 PM, Richard Damon wrote:
>> On 9/7/23 12:20 PM, Chris M. Thomasson wrote:
>>> On 9/7/2023 11:06 AM, Richard Damon wrote:
>>>> On 9/7/23 10:27 AM, Scott Lurndal wrote:
>>>>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>>>> Am 07.09.2023 um 16:44 schrieb Pavel:
>>>>>>
>>>>>>> How so? Waiters will wait for the time of initialization.
>>>>>>> The initializing thread will be yielded ...
>>>>>>
>>>>>> Initializing is usually much faster than a whole timeslice,
>>>>>> so yielding would be incacceptabel. That's just a stupid idea.
>>>>>
>>>>> So tell us, on which operating systems will there be more than
>>>>> one thread running when application static objects are
>>>>> initialized (which happens generally before the application
>>>>> 'main' function is called, and thus before the application
>>>>> has a chance to create any threads)?
>>>>
>>>> While GLOBAL static objects get initialized before main starts,
>>>> function local static objects don't get initialized until the first
>>>> call of the function. These will need some synchronization if the
>>>> function is called from multiple threads at "the same time".
>>>
>>> A simple global hash lock scheme is where we can hash addresses
>>> directly into a static locking table. The lock table is created
>>> _before_ any program logic is executed.
>>>
>>> https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/wwYQCG2hAwAJ
>>>
>>>
>>>> Also, some global object could start up a thread in its constructor.
>>>
>>> YIKES! Shit. I have had to debug other peoples code that did this.
>>> Many points of errors... One was a rather common peach of a bug. The
>>> constructor would create a thread that would in turn call into a
>>> virtual function and start using the object before its constructor was
>>> completed. A massive race condition, nasty ones!
>>>
>>
>> Yes, such a thread needs to understand that the system isn't fully
>> configured. And yes, a base class creating a thread needs that thread to
>> understand that the object isn't fully created yet and do something to
>> handle that issue (which likely requires some help from the most derived
>> class).
>
>I actually got to a point where I said no creating threads in
>constructors! I have seen to many problems. Create the object _first_,
>then expose it to a thread, or create a thread that works with the
>_fully_ constructed object. The types of race conditions I have had to
>deal with wrt objects creating threads in constructors have tended to be
>rather nasty in nature...

The race condition is pretty easy to resolve. Turn thread initialization
into a two-phase operation.

When we have an object that requires its own thread, it will inherit
from a base class called c_thread. The constructor for c_thread will
create the thread which eventually executes the objects 'run' function. The
pthread_create call starts the thread executing at the static function
c_thread::run(void *obj), where the argument is a pointer to the object
itself.

That static function sets the process name (prctl(PR_SET_NAME)) and
then waits on a pthread_cond_t (thread_initialized) declared as a data member of
c_thread.

Meanwhile, the derived object constructor runs in the original thread after the c_thread
constructor returns and initializes the object. Once the object is fully
initialized (which may happen in the constructor, or may happen
at some later point in time during program startup), the c_thread::run
method condition variable (thread_initialized) is signaled and the
static ::run function transfers control to the derived class virtual ::run
function.

/**
* Static pthread start function. Invokes thread specific run virtual
* function.
*
* @param arg The class object pointer passed in from the pthread_create.
*/
void *
c_thread::run(void *arg)
{ c_thread *tp = (c_thread *)arg;
int diag;

pthread_mutex_lock(&tp->t_threadlock);
while (!tp->t_thread_initialized) {
pthread_cond_wait(&tp->t_ready, &tp->t_threadlock);
}
pthread_mutex_unlock(&tp->t_threadlock);

diag = prctl(PR_SET_NAME, tp->t_thread_name, NULL, NULL, NULL, NULL);
if (diag == -1) {
tp->t_logger->log("Unable to set thread name: %s\n", strerror(errno));
}

tp->t_running = true;
tp->run();
tp->t_running = false;

return NULL;
}

Re: Thread-safe initialization of static objects

<wguKM.698884$xMqa.357246@fx12.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!nntp.comgw.net!peer02.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx12.iad.POSTED!not-for-mail
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
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>
From: pauldontspamtolk@removeyourself.dontspam.yahoo (Pavel)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17
MIME-Version: 1.0
In-Reply-To: <udcubm$325k4$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 102
Message-ID: <wguKM.698884$xMqa.357246@fx12.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Fri, 08 Sep 2023 00:53:48 UTC
Date: Thu, 7 Sep 2023 20:53:48 -0400
X-Received-Bytes: 5095
 by: Pavel - Fri, 8 Sep 2023 00:53 UTC

Bonita Montero wrote:
> Am 07.09.2023 um 16:44 schrieb Pavel:
>
>> How so? Waiters will wait for the time of initialization.
>> The initializing thread will be yielded ...
>
> Initializing is usually much faster than a whole timeslice,
> so yielding would be incacceptabel. That's just a stupid idea.
Mutex makes thread waiting for initialization not runnable. Yield leaves
it runnable. Therefore, waking up a thread that acquired the mutex is
actually slower than the time needed by thread that yielded to get
running again. This is regardless of whether initializing is slow or fast.

>
>> try_lock in a loop with a sleep or yield wouldn't fail. ...
>
> Do you really think someone would accept spinning with that ?
I don't have to think about this because I know many do.

> This means that an initializing thread which is scheduled away
> while holding the mutex might keep other threads spinning for
> a long time.
No, it can be scheduled away only momentarily because the waiting
threads will relinquish the CPU instantly after being scheduled.

>
>> Regardless, your argument assumes too much C++-morphism in the
>> implementation whereas the implementation can use any platform
>> -specific  approach available to it. E.g. pthread_once on Linux
>> does not fail if given valid arguments ...
>
> pthread_once could be implemented with a single central semaphore
pthread_once takes a statically-initialized pthread_once_t "once
control" that the calling code must supply. It is against the purpose of
pthread_once to wait for a single central semaphore. On the opposite,
the goal is to let the client code control the concurrency by selecting
how many "once control" they want to use.

> for
> all operations and if the implementers know that the synchronization
> itself doesn't fail and the semphore is pre-allocated by the runtime
> it's possible to survive that synchronization without error.
correct. My point exactly.

> But check that code:
>
> #include <iostream>
> #include <thread>
> #include <vector>
>
> using namespace std;
>
> template<unsigned Thread>
> struct SleepAtInitialize
> {
>     SleepAtInitialize()
>     {
>         this_thread::sleep_for( 1s );
>     }
> };
>
> int main()
> {
>     auto unroll = []<size_t ... Indices>( index_sequence<Indices ...>,
> auto fn )
>     {
>         ((fn.template operator ()<Indices>()), ...);
>     };
>     constexpr unsigned N_THREADS = 10;
>     vector<jthread> threads;
>     threads.reserve( N_THREADS );
>     unroll( make_index_sequence<N_THREADS>(),
>         [&]<unsigned Thread>()
>         {
>             threads.emplace_back(
>                 [&]<unsigned IObj>( integral_constant<unsigned, IObj> )
>                 {
>                     static SleepAtInitialize<IObj> guard;
>                 }, integral_constant<unsigned, Thread>() );
>         } );
>
> }
>
> This code runs with individual mutexes per object, i.e. the time
> taken is about one second (with MSVC, libc++ and libstdc++). So
> when individual mutexes are used the initialization may fail.
See above for why pthread_once implementation should also take one
second with the individual pthread_once_t once controls per object.

>
>> Correct, the above was wrong. But initialization can catch and try
>> again. ...
>
> That a bumbler solution.
mutex locking does not fail for no reason. In practice on Linux if the
mutex is in the committed memory and is propertly prioritized and
permissioned and is non-robust (all of which one should assume to be
true for a mutex used by a C++ implementation for initialization) I am
unaware of any reason for why non-timed locking may fail other than
deadlock detection. C++ behavior for the scenario when static
initialization deadlocks is unspecified so the above solution is no
better or worse than any other.

Pages:123456789101112131415161718192021222324252627
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor