Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

System going down at 1:45 this afternoon for disk crashing.


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

<udebu2$3bknb$1@dont-email.me>

  copy mid

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

  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: Fri, 8 Sep 2023 07:37:09 +0200
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <udebu2$3bknb$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Sep 2023 05:37:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="81935248b04dd2486e428afa8c335f1e";
logging-data="3527403"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8bnRzb8wRIXu6gH6Nmomy2JRVxAr2Xvs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:PM7ng8X6/xl9ExCGDvMyk3kbCpE=
Content-Language: de-DE
In-Reply-To: <wguKM.698884$xMqa.357246@fx12.iad>
 by: Bonita Montero - Fri, 8 Sep 2023 05:37 UTC

Am 08.09.2023 um 02:53 schrieb Pavel:

> Bonita Montero wrote:

>> 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.

However, using sth. like yield is unacceptably slow.

> No, it can be scheduled away only momentarily because the waiting
> threads will relinquish the CPU instantly after being scheduled.

The thread might be scheduled away many timeslices - that's
unacceptable.

> See above for why pthread_once implementation should also take one
> second with the individual pthread_once_t once controls per object.

As you can see from my source the common runtimes use a mutex per
object. This creation may fail.

> mutex locking does not fail for no reason. ...

It's like when a memory allocation would fail and you would
spin until it doesn't fail again. That's also unacceptable.

Re: Thread-safe initialization of static objects

<udfjmh$j0bi$3@gwaiyur.mb-net.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From: news.5.m...@spamgourmet.org (Marcel Mueller)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Fri, 8 Sep 2023 18:55:45 +0200
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <udfjmh$j0bi$3@gwaiyur.mb-net.net>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Sep 2023 16:55:45 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net;
logging-data="622962"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:npaq2QVUyLeoioWmaayXEh8QpQM= sha256:/OG6Tj6dYNXyNhya2KSxkcn4pDmx3AyvbmgE1pv3Ztk=
sha1:co4zuzWjfWpAlk2WwGPjbVZmeQ0= sha256:Z83ZCn59qEprosxEANrE/ceduXvug8WumG0JrHRa7Lg=
Content-Language: de-DE, en-US
In-Reply-To: <udcubm$325k4$1@dont-email.me>
 by: Marcel Mueller - Fri, 8 Sep 2023 16:55 UTC

Am 07.09.23 um 18:39 schrieb Bonita Montero:
> 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.

You do not loose a whole time slice.
This is only true if all available CPU cores are already at 100% load.
In this case you likely loose some time slices anyway.

Normally the spin lock has only the overhead of the additional context
switches. This turns into some short delay if all CPU cores are at full
load, which is of course intended.

> 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.

This is almost impossible since the spinning thread will block the CPU
for only one additional context switch. So the maximum impact is the
number of spinning threads vs. the number of working threads. Assuming
the the working threads likely do not call yield all the time they get
much more resources than this worst case scenario.

Marcel

Re: Thread-safe initialization of static objects

<X5KKM.218705$ftCb.30173@fx34.iad>

  copy mid

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

  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!fx34.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> <wguKM.698884$xMqa.357246@fx12.iad>
<udebu2$3bknb$1@dont-email.me>
From: pauldont...@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: <udebu2$3bknb$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 56
Message-ID: <X5KKM.218705$ftCb.30173@fx34.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Fri, 08 Sep 2023 18:54:47 UTC
Date: Fri, 8 Sep 2023 14:54:41 -0400
X-Received-Bytes: 3068
 by: Pavel - Fri, 8 Sep 2023 18:54 UTC

Bonita Montero wrote:
> Am 08.09.2023 um 02:53 schrieb Pavel:
>
>> Bonita Montero wrote:
>
>>> 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.
>
> However, using sth. like yield is unacceptably slow.
As per the above yield-based waiting for initialization is not slower
than mutex-based. Hence, this "unacceptably slow is nonsense.

>
>> No, it can be scheduled away only momentarily because the waiting
>> threads will relinquish the CPU instantly after being scheduled.
>
> The thread might be scheduled away many timeslices -
A thread that waits for the contended mutex is, upon locking the mutex,
scheduled identically to yield so no difference with mutex

> that's
> unacceptable.
as per the above, this "unacceptale" is nonsense, too.

>
>
>> See above for why pthread_once implementation should also take one
>> second with the individual pthread_once_t once controls per object.
>
> As you can see from my source the common runtimes use a mutex per
> object.
On the opposite, I can see in glibc source code that pthread_once for
linux (which is most common) does not use mutex. There is no "creation",
pthread_once_t is an int.

> This creation may fail.
as per the above, there is nothing to fail unless someone decides to
allocate the int in free memory -- which is not done for static
initialization.

>
>> mutex locking does not fail for no reason. ...
>
> It's like when a memory allocation would fail and you would
> spin until it doesn't fail again.
as per the above, there is nothing to fail unless someone decides to
allocate the int in free memory -- which is not done for static
initialization.

> That's also unacceptable.
as per the above, this "unacceptale" is nonsense, too.

Re: Thread-safe initialization of static objects

<udfvll$3m1ik$1@dont-email.me>

  copy mid

https://www.novabbs.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: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Fri, 8 Sep 2023 13:20:02 -0700
Organization: A noiseless patient Spider
Lines: 86
Message-ID: <udfvll$3m1ik$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> <udda1r$33t5b$1@dont-email.me>
<NqqKM.1138349$AsA.720273@fx18.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Sep 2023 20:20:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f40bafe90f44499e935ee13347bf360e";
logging-data="3868244"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fPKwhdUF7K4UI+/SWS+zfGChUDwMsPU0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:nTAeaLmC6XTaCbOaxrs7380avD0=
In-Reply-To: <NqqKM.1138349$AsA.720273@fx18.iad>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 8 Sep 2023 20:20 UTC

On 9/7/2023 1:31 PM, Richard Damon wrote:
> 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.

Actually, one way that works pretty good, aka not make me freak out,
lol, wrt creating a thread in a constructor is to create a little proxy
object. Something along the lines of:

template<typename T>
struct thread_proxy
{ T m_object;
std::thread m_thread;
};

We can launch the thread in the constructor of thread_proxy<T>

The thread calls a function in T called thread_entry or whatever. This
ensures that T is 100% _fully_ constructed before the thread is created
and starts working with the damn thing. Imvvvho, this is a lot better
than creating the thread directly in T's constructor.

Actually, I need to get back into threads for some rendering work of mine.

Re: Thread-safe initialization of static objects

<udg00r$3m3qc$1@dont-email.me>

  copy mid

https://www.novabbs.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!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: Fri, 8 Sep 2023 13:26:03 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <udg00r$3m3qc$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbe19$2rk0p$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 8 Sep 2023 20:26:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f40bafe90f44499e935ee13347bf360e";
logging-data="3870540"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+I035A4m6KxD76VjhBPBtUR8B2YxDWEj4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:NumJuJzoc1yBlBKD1dIOg/74+0M=
In-Reply-To: <udbe19$2rk0p$2@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 8 Sep 2023 20:26 UTC

On 9/6/2023 7:54 PM, 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 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.
>

Limited spin locking in user space is fine. Think about it for a
moment... Think of adaptive mutex logic... They will try to spin wait a
couple of times on a contended state before using the kernel to actually
wait. No problem with that, right?

Re: Thread-safe initialization of static objects

<udg01c$3m3qc$2@dont-email.me>

  copy mid

https://www.novabbs.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!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: Fri, 8 Sep 2023 13:26:20 -0700
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <udg01c$3m3qc$2@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> <udda40$33ub2$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Sep 2023 20:26:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f40bafe90f44499e935ee13347bf360e";
logging-data="3870540"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+z8rmHHtPfi/wlVVipIOyQMcqvK6WQtYs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:pQWXOw5N2qGNrsGAd8z2WgZBz+s=
In-Reply-To: <udda40$33ub2$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 8 Sep 2023 20:26 UTC

On 9/7/2023 1:00 PM, Bonita Montero wrote:
> 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.
>

Why not?

Re: Thread-safe initialization of static objects

<udg06h$3m1ik$2@dont-email.me>

  copy mid

https://www.novabbs.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: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Fri, 8 Sep 2023 13:29:05 -0700
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <udg06h$3m1ik$2@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Sep 2023 20:29:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f40bafe90f44499e935ee13347bf360e";
logging-data="3868244"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/lBVbON29ynDUQXjj4o7MayP7bh/jGkc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:lYRsvxC4oA+25y0rMjzW9uPkMAw=
In-Reply-To: <udebu2$3bknb$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 8 Sep 2023 20:29 UTC

On 9/7/2023 10:37 PM, Bonita Montero wrote:
> Am 08.09.2023 um 02:53 schrieb Pavel:
>
>> Bonita Montero wrote:
>
>>> 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.
>
> However, using sth. like yield is unacceptably slow.

Huh? Think of an adaptive mutex for a moment... :^)

>
>> No, it can be scheduled away only momentarily because the waiting
>> threads will relinquish the CPU instantly after being scheduled.
>
> The thread might be scheduled away many timeslices - that's
> unacceptable.
>
>
>> See above for why pthread_once implementation should also take one
>> second with the individual pthread_once_t once controls per object.
>
> As you can see from my source the common runtimes use a mutex per
> object. This creation may fail.
>
>> mutex locking does not fail for no reason. ...
>
> It's like when a memory allocation would fail and you would
> spin until it doesn't fail again. That's also unacceptable.
>
>

Re: Thread-safe initialization of static objects

<4OOKM.167484$uEkc.38090@fx35.iad>

  copy mid

https://www.novabbs.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!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx35.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>
<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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me>
From: pauldont...@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: <udg01c$3m3qc$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 38
Message-ID: <4OOKM.167484$uEkc.38090@fx35.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Sat, 09 Sep 2023 00:14:56 UTC
Date: Fri, 8 Sep 2023 20:14:48 -0400
X-Received-Bytes: 2394
 by: Pavel - Sat, 9 Sep 2023 00:14 UTC

Chris M. Thomasson wrote:
> On 9/7/2023 1:00 PM, Bonita Montero wrote:
>> 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.
>>
>
> Why not?

I think it's not needed in general for static initialization purpose.

This is because, if our design choice for our "C++ compiler" is to use a
dedicated mutex (or once_flag control) per a static variable that
requires dynamic initialization, nothing prevents our compiler from
allocating a static instance of such mutex or control next to the static
variable.

Because mutex and once_flag can be constant-initialized (at least in
POSIX, notably Linux), the code that dynamically initializes a variable
can directly use the address of its respective constant-initialized
mutex or control, as in the following example:

code in C++ source file:

static type1 var1 = InitVar1();

**pseudo-code** actually generated by the compiler:

pthread_once_t __var1_once_control = PTHREAD_ONCE_INIT; /* generated
by the compiler when */

static type1 var1 = InitVar1(); /* this is compiled to,
**in pseudo-code**, to something like

pthread_once( [&var1]() { var1 = InitVar1(); } );
*/

Re: Thread-safe initialization of static objects

<udggc7$3ou77$1@dont-email.me>

  copy mid

https://www.novabbs.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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Fri, 8 Sep 2023 18:05:11 -0700
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <udggc7$3ou77$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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me> <4OOKM.167484$uEkc.38090@fx35.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 9 Sep 2023 01:05:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="3963111"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18N5Xgi3cRrQsBi/XkcsF59AjKl9tOdx/s="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:K2nbsjXIpRvueiZaIOK8wo4npPQ=
In-Reply-To: <4OOKM.167484$uEkc.38090@fx35.iad>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 9 Sep 2023 01:05 UTC

On 9/8/2023 5:14 PM, Pavel wrote:
> Chris M. Thomasson wrote:
>> On 9/7/2023 1:00 PM, Bonita Montero wrote:
>>> 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.
>>>
>>
>> Why not?
>
> I think it's not needed in general for static initialization purpose.
>
> This is because, if our design choice for our "C++ compiler" is to use a
> dedicated mutex (or once_flag control) per a static variable that
> requires dynamic initialization, nothing prevents our compiler from
> allocating a static instance of such mutex or control next to the static
> variable.
>
> Because mutex and once_flag can be constant-initialized (at least in
> POSIX, notably Linux), the code that dynamically initializes a variable
> can directly use the address of its respective constant-initialized
> mutex or control, as in the following example:
>
> code in C++ source file:
>
> static type1 var1 = InitVar1();
>
> **pseudo-code** actually generated by the compiler:
>
> pthread_once_t __var1_once_control = PTHREAD_ONCE_INIT; /* generated
> by the compiler when  */
>
> static type1 var1 = InitVar1(); /* this is compiled to,
> **in pseudo-code**, to something like
>
>  pthread_once( [&var1]() { var1 = InitVar1(); } );
> */

pthread_once is perfectly fine. Just wondering, are you creating the
POSIX impl directly here?

Re: Thread-safe initialization of static objects

<udggmi$3ou77$2@dont-email.me>

  copy mid

https://www.novabbs.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: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Fri, 8 Sep 2023 18:10:41 -0700
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <udggmi$3ou77$2@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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me> <4OOKM.167484$uEkc.38090@fx35.iad>
<udggc7$3ou77$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 9 Sep 2023 01:10:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="3963111"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/X7WvqnTGIA1Hogd2VfmLqxjHcvm8/b5U="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:3ABjmfrEtLA6UzV2jlgaayfXs3k=
In-Reply-To: <udggc7$3ou77$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 9 Sep 2023 01:10 UTC

On 9/8/2023 6:05 PM, Chris M. Thomasson wrote:
> On 9/8/2023 5:14 PM, Pavel wrote:
>> Chris M. Thomasson wrote:
>>> On 9/7/2023 1:00 PM, Bonita Montero wrote:
>>>> 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.
>>>>
>>>
>>> Why not?
>>
>> I think it's not needed in general for static initialization purpose.
>>
>> This is because, if our design choice for our "C++ compiler" is to use
>> a dedicated mutex (or once_flag control) per a static variable that
>> requires dynamic initialization, nothing prevents our compiler from
>> allocating a static instance of such mutex or control next to the
>> static variable.
>>
>> Because mutex and once_flag can be constant-initialized (at least in
>> POSIX, notably Linux), the code that dynamically initializes a
>> variable can directly use the address of its respective
>> constant-initialized mutex or control, as in the following example:
>>
>> code in C++ source file:
>>
>> static type1 var1 = InitVar1();
>>
>> **pseudo-code** actually generated by the compiler:
>>
>> pthread_once_t __var1_once_control = PTHREAD_ONCE_INIT; /* generated
>> by the compiler when  */
>>
>> static type1 var1 = InitVar1(); /* this is compiled to,
>> **in pseudo-code**, to something like
>>
>>   pthread_once( [&var1]() { var1 = InitVar1(); } );
>> */
>
> pthread_once is perfectly fine. Just wondering, are you creating the
> POSIX impl directly here?

Think of how you would directly implement pthread_once... There are many
different ways.

Re: Thread-safe initialization of static objects

<udghd4$3p30t$1@dont-email.me>

  copy mid

https://www.novabbs.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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Fri, 8 Sep 2023 18:22:43 -0700
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <udghd4$3p30t$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udc6n0$2uo4t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 9 Sep 2023 01:22:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="3968029"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CGPgmpbPwrWgkeBkZ0frlm7LC8PdydJ0="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:EhEW6iblM1vWBt0ahtUvpAnai/E=
In-Reply-To: <udc6n0$2uo4t$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 9 Sep 2023 01:22 UTC

On 9/7/2023 2:55 AM, Paavo Helde wrote:
> 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.

If it does fail, then some rather radical shit has hit the fan.

>
> 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.

Right. The implementation must get it right using fine grain locking,
amortized table locking, or some other means. The compiler and POSIX
worth together like a system. Iirc, I saw another way that was
lock-free, but allowed a thread to create an object, then might have to
delete it because it was not the first one to be installed in the sense
of being visible to other threads. This was decades ago, iirc it used
CAS. No mutex.

Re: Thread-safe initialization of static objects

<udgiom$3p7e1$1@dont-email.me>

  copy mid

https://www.novabbs.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!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: Fri, 8 Sep 2023 18:45:57 -0700
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <udgiom$3p7e1$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udc6n0$2uo4t$1@dont-email.me>
<udghd4$3p30t$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 9 Sep 2023 01:45:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="3972545"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vC7cAfBAY2bezFGJUs7BKtPiaJ8p/v24="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:TfUsXoPEvoLq8tSj0udpY3D8XRI=
In-Reply-To: <udghd4$3p30t$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 9 Sep 2023 01:45 UTC

On 9/8/2023 6:22 PM, Chris M. Thomasson wrote:
> On 9/7/2023 2:55 AM, Paavo Helde wrote:
>> 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.
>
> If it does fail, then some rather radical shit has hit the fan.
>
>
>>
>> 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.
>
> Right. The implementation must get it right using fine grain locking,
> amortized table locking, or some other means. The compiler and POSIX
> worth together like a system. Iirc, I saw another way that was
> lock-free, but allowed a thread to create an object, then might have to
> delete it because it was not the first one to be installed in the sense
> of being visible to other threads. This was decades ago, iirc it used
> CAS. No mutex.

Iirc it was something like:

static foo* g_foo = nullptr;

foo* l_foo = g_foo; // atomic load

if (! l_foo)
{ foo* t_foo = new foo;
foo* cmp = nullptr;

// CAS would update the comparand on failure.
// Take special note of that...

// mb release

if (! CAS(&g_foo, &cmp, t_foo)) // atomic rmw
{
// failed!!! well, shit happens!
delete t_foo;
t_foo = cmp;
// mb acquire
}
}

else
{ // mb acquire
}

l_foo->bar();

Re: Thread-safe initialization of static objects

<udgold$3tquh$1@dont-email.me>

  copy mid

https://www.novabbs.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: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 9 Sep 2023 05:26:40 +0200
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <udgold$3tquh$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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 03:26:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="40fc38809065ec861f987de259190c0a";
logging-data="4123601"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1868Pg70RVoUJTe02ua6F/rWrBOT/JGsso="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:z6wYzvg+m2GI+EqIB9wRTADa99s=
In-Reply-To: <udg01c$3m3qc$2@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 9 Sep 2023 03:26 UTC

Am 08.09.2023 um 22:26 schrieb Chris M. Thomasson:

> Why not?

Because there are simpler ideas that work.

Re: Thread-safe initialization of static objects

<udgpnl$3tuni$1@dont-email.me>

  copy mid

https://www.novabbs.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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 9 Sep 2023 05:44:55 +0200
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <udgpnl$3tuni$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 03:44:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="40fc38809065ec861f987de259190c0a";
logging-data="4127474"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AE9MTQ1N3jSn/ZVGtW5h9B2Dp5kgw0bg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:RzD5kx7ED30nDGO1ZG+JaM1OG2U=
Content-Language: de-DE
In-Reply-To: <X5KKM.218705$ftCb.30173@fx34.iad>
 by: Bonita Montero - Sat, 9 Sep 2023 03:44 UTC

Am 08.09.2023 um 20:54 schrieb Pavel:

> As per the above yield-based waiting for initialization is not slower
> than mutex-based. Hence, this "unacceptably slow is nonsense.

Of course it is slower because you wait N timeslices.

> A thread that waits for the contended mutex is, upon locking the mutex,
> scheduled identically to yield so no difference with mutex

No, a thread waiting for a mutex doesn's spin when the thread holding
the spinlock is scheduled away but it is sleeping inside the kernel.
That's more efficient.

> On the opposite, I can see in glibc source code that pthread_once for
> linux (which is most common) does not use mutex. There is no "creation",
> pthread_once_t is an int.

pthread_once uses a mutex internally for sure.

Re: Thread-safe initialization of static objects

<udi0vu$488m$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: eesn...@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 9 Sep 2023 17:54:52 +0300
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <udi0vu$488m$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 14:54:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0d36706b697ad2c8a892f9a52886dcec";
logging-data="139542"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jN3TekXr+A2mbLUMtxAgMiXCzJFX4Qys="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:uX9UHaW/hPbUTpcVLIhtKu9/et4=
Content-Language: en-US
In-Reply-To: <udgpnl$3tuni$1@dont-email.me>
 by: Paavo Helde - Sat, 9 Sep 2023 14:54 UTC

09.09.2023 06:44 Bonita Montero kirjutas:
>
> pthread_once uses a mutex internally for sure.
>

pthread_once source code can be found by google:
https://elixir.bootlin.com/glibc/latest/source/nptl/pthread_once.c

From there I can see that it calls atomic_load_acquire(). Only if this
fails, will it call __pthread_once_slow() in the slow branch, which uses
futexes. Some other googling reveals that: "a futex ("Fast Userspace
Mutex") isn't a mutex at all"

Re: Thread-safe initialization of static objects

<udi9s5$5f8c$1@dont-email.me>

  copy mid

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

  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: Sat, 9 Sep 2023 19:26:29 +0200
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <udi9s5$5f8c$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 17:26:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="40fc38809065ec861f987de259190c0a";
logging-data="179468"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ijmBvBpenrPOMAy0CHSvtKmnyd9Zx81Q="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:oiNeNU5W8d8GfqHlDMRV2gjiycE=
In-Reply-To: <udi0vu$488m$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 9 Sep 2023 17:26 UTC

Am 09.09.2023 um 16:54 schrieb Paavo Helde:

> From there I can see that it calls atomic_load_acquire(). Only if this
> fails, will it call __pthread_once_slow() in the slow branch, which uses
> futexes. Some other googling reveals that: "a futex ("Fast Userspace
> Mutex") isn't a mutex at all"

Doesn't matter since the standard should honor static initialization
that fails because of synchronization woes. And as I've shown with
my code MSVC, libstdc++ and libc++ have a mutex per static initiali-
zation, so pthread_once doesn't apply here.

Re: Thread-safe initialization of static objects

<q82LM.1449511$GMN3.1347862@fx16.iad>

  copy mid

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

  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!fx16.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> <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>
From: pauldont...@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: <udi9s5$5f8c$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 15
Message-ID: <q82LM.1449511$GMN3.1347862@fx16.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Sat, 09 Sep 2023 17:42:46 UTC
Date: Sat, 9 Sep 2023 13:42:46 -0400
X-Received-Bytes: 1799
 by: Pavel - Sat, 9 Sep 2023 17:42 UTC

Bonita Montero wrote:
> Am 09.09.2023 um 16:54 schrieb Paavo Helde:
>
>>  From there I can see that it calls atomic_load_acquire(). Only if
>> this fails, will it call __pthread_once_slow() in the slow branch,
>> which uses futexes. Some other googling reveals that: "a futex ("Fast
>> Userspace Mutex") isn't a mutex at all"
>
> Doesn't matter since the standard should honor static initialization
> that fails because of synchronization woes. And as I've shown with
> my code MSVC, libstdc++ and libc++ have a mutex per static initiali-
> zation, so pthread_once doesn't apply here.

If Microsoft implemented standard wrongly, it's Microsoft's failure, not
the standard's.

Re: Thread-safe initialization of static objects

<1a2LM.1449512$GMN3.489417@fx16.iad>

  copy mid

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

  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!fx16.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>
<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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me> <4OOKM.167484$uEkc.38090@fx35.iad>
<udggc7$3ou77$1@dont-email.me>
From: pauldont...@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: <udggc7$3ou77$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 46
Message-ID: <1a2LM.1449512$GMN3.489417@fx16.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Sat, 09 Sep 2023 17:44:29 UTC
Date: Sat, 9 Sep 2023 13:44:29 -0400
X-Received-Bytes: 2739
 by: Pavel - Sat, 9 Sep 2023 17:44 UTC

Chris M. Thomasson wrote:
> On 9/8/2023 5:14 PM, Pavel wrote:
>> Chris M. Thomasson wrote:
>>> On 9/7/2023 1:00 PM, Bonita Montero wrote:
>>>> 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.
>>>>
>>>
>>> Why not?
>>
>> I think it's not needed in general for static initialization purpose.
>>
>> This is because, if our design choice for our "C++ compiler" is to use
>> a dedicated mutex (or once_flag control) per a static variable that
>> requires dynamic initialization, nothing prevents our compiler from
>> allocating a static instance of such mutex or control next to the
>> static variable.
>>
>> Because mutex and once_flag can be constant-initialized (at least in
>> POSIX, notably Linux), the code that dynamically initializes a
>> variable can directly use the address of its respective
>> constant-initialized mutex or control, as in the following example:
>>
>> code in C++ source file:
>>
>> static type1 var1 = InitVar1();
>>
>> **pseudo-code** actually generated by the compiler:
>>
>> pthread_once_t __var1_once_control = PTHREAD_ONCE_INIT; /* generated
>> by the compiler when  */
>>
>> static type1 var1 = InitVar1(); /* this is compiled to,
>> **in pseudo-code**, to something like
>>
>>   pthread_once( [&var1]() { var1 = InitVar1(); } );
>> */
>
> pthread_once is perfectly fine. Just wondering, are you creating the
> POSIX impl directly here?
I am not creating, just demonstrating in pseudo-code how Linux
implementation could work.

Re: Thread-safe initialization of static objects

<7p2LM.1449513$GMN3.1325499@fx16.iad>

  copy mid

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

  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!fx16.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>
<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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me> <4OOKM.167484$uEkc.38090@fx35.iad>
<udggc7$3ou77$1@dont-email.me> <udggmi$3ou77$2@dont-email.me>
From: pauldont...@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: <udggmi$3ou77$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 55
Message-ID: <7p2LM.1449513$GMN3.1325499@fx16.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Sat, 09 Sep 2023 18:00:35 UTC
Date: Sat, 9 Sep 2023 14:00:28 -0400
X-Received-Bytes: 3306
 by: Pavel - Sat, 9 Sep 2023 18:00 UTC

Chris M. Thomasson wrote:
> On 9/8/2023 6:05 PM, Chris M. Thomasson wrote:
>> On 9/8/2023 5:14 PM, Pavel wrote:
>>> Chris M. Thomasson wrote:
>>>> On 9/7/2023 1:00 PM, Bonita Montero wrote:
>>>>> 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.
>>>>>
>>>>
>>>> Why not?
>>>
>>> I think it's not needed in general for static initialization purpose.
>>>
>>> This is because, if our design choice for our "C++ compiler" is to
>>> use a dedicated mutex (or once_flag control) per a static variable
>>> that requires dynamic initialization, nothing prevents our compiler
>>> from allocating a static instance of such mutex or control next to
>>> the static variable.
>>>
>>> Because mutex and once_flag can be constant-initialized (at least in
>>> POSIX, notably Linux), the code that dynamically initializes a
>>> variable can directly use the address of its respective
>>> constant-initialized mutex or control, as in the following example:
>>>
>>> code in C++ source file:
>>>
>>> static type1 var1 = InitVar1();
>>>
>>> **pseudo-code** actually generated by the compiler:
>>>
>>> pthread_once_t __var1_once_control = PTHREAD_ONCE_INIT; /* generated
>>> by the compiler when  */
>>>
>>> static type1 var1 = InitVar1(); /* this is compiled to,
>>> **in pseudo-code**, to something like
>>>
>>>   pthread_once( [&var1]() { var1 = InitVar1(); } );
>>> */
>>
>> pthread_once is perfectly fine. Just wondering, are you creating the
>> POSIX impl directly here?
>
> Think of how you would directly implement pthread_once... There are many
> different ways.
You are correct, there are. Fortunately, I don't have to reinvent this
particular wheel as the implementation source code is available for
everyone to read. On Linux, Futex is a natural choice.

Other platforms can have theirs. I did not program for Windows for long
time but I would at least try to implement call_once with a critical
section (whose creation, contrary to OP's insinuations, cannot fail
since Windows Vista).

Re: Thread-safe initialization of static objects

<udie8t$63u7$1@dont-email.me>

  copy mid

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

  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: Sat, 9 Sep 2023 11:41:32 -0700
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <udie8t$63u7$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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me> <4OOKM.167484$uEkc.38090@fx35.iad>
<udggc7$3ou77$1@dont-email.me> <udggmi$3ou77$2@dont-email.me>
<7p2LM.1449513$GMN3.1325499@fx16.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 9 Sep 2023 18:41:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="200647"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18gHYoCa5ODQqjDDg4OuKsv0OStatI8qw8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:Z2p4myFwIiuc5L387OfxnZSnVWo=
Content-Language: en-US
In-Reply-To: <7p2LM.1449513$GMN3.1325499@fx16.iad>
 by: Chris M. Thomasson - Sat, 9 Sep 2023 18:41 UTC

On 9/9/2023 11:00 AM, Pavel wrote:
> Chris M. Thomasson wrote:
>> On 9/8/2023 6:05 PM, Chris M. Thomasson wrote:
>>> On 9/8/2023 5:14 PM, Pavel wrote:
>>>> Chris M. Thomasson wrote:
>>>>> On 9/7/2023 1:00 PM, Bonita Montero wrote:
>>>>>> 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.
>>>>>>
>>>>>
>>>>> Why not?
>>>>
>>>> I think it's not needed in general for static initialization purpose.
>>>>
>>>> This is because, if our design choice for our "C++ compiler" is to
>>>> use a dedicated mutex (or once_flag control) per a static variable
>>>> that requires dynamic initialization, nothing prevents our compiler
>>>> from allocating a static instance of such mutex or control next to
>>>> the static variable.
>>>>
>>>> Because mutex and once_flag can be constant-initialized (at least in
>>>> POSIX, notably Linux), the code that dynamically initializes a
>>>> variable can directly use the address of its respective
>>>> constant-initialized mutex or control, as in the following example:
>>>>
>>>> code in C++ source file:
>>>>
>>>> static type1 var1 = InitVar1();
>>>>
>>>> **pseudo-code** actually generated by the compiler:
>>>>
>>>> pthread_once_t __var1_once_control = PTHREAD_ONCE_INIT; /* generated
>>>> by the compiler when  */
>>>>
>>>> static type1 var1 = InitVar1(); /* this is compiled to,
>>>> **in pseudo-code**, to something like
>>>>
>>>>   pthread_once( [&var1]() { var1 = InitVar1(); } );
>>>> */
>>>
>>> pthread_once is perfectly fine. Just wondering, are you creating the
>>> POSIX impl directly here?
>>
>> Think of how you would directly implement pthread_once... There are
>> many different ways.
> You are correct, there are. Fortunately, I don't have to reinvent this
> particular wheel as the implementation source code is available for
> everyone to read. On Linux, Futex is a natural choice.
>
> Other platforms can have theirs. I did not program for Windows for long
> time but I would at least try to implement call_once with a critical
> section (whose creation, contrary to OP's insinuations, cannot fail
> since Windows Vista).

Iirc, Windows has a futex like "thing". I cannot remember the API right
now. Will get back to you.

Re: Thread-safe initialization of static objects

<udiegf$63u7$2@dont-email.me>

  copy mid

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

  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: Sat, 9 Sep 2023 11:45:34 -0700
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <udiegf$63u7$2@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> <udda40$33ub2$1@dont-email.me>
<udg01c$3m3qc$2@dont-email.me> <udgold$3tquh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 18:45:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="200647"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rIX2gRmtaa3zysHTqbKHAB+yzROCEv5M="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:iAc3fwrWRYo77Yqu+WwM1BlQKQI=
Content-Language: en-US
In-Reply-To: <udgold$3tquh$1@dont-email.me>
 by: Chris M. Thomasson - Sat, 9 Sep 2023 18:45 UTC

On 9/8/2023 8:26 PM, Bonita Montero wrote:
> Am 08.09.2023 um 22:26 schrieb Chris M. Thomasson:
>
>> Why not?
>
> Because there are simpler ideas that work.
>

Not exactly sure how to parse that, but sure. As long as it works.

Re: Thread-safe initialization of static objects

<udiema$63u7$3@dont-email.me>

  copy mid

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

  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: Sat, 9 Sep 2023 11:48:41 -0700
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <udiema$63u7$3@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 18:48:42 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="200647"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QLlKfxREmMq8BshPO+NNJmhFtX+W+TCI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:DTbUToUwJ2UWQRWmH9EBQ2tdZcg=
In-Reply-To: <udgpnl$3tuni$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 9 Sep 2023 18:48 UTC

On 9/8/2023 8:44 PM, Bonita Montero wrote:
> Am 08.09.2023 um 20:54 schrieb Pavel:
>
>> As per the above yield-based waiting for initialization is not slower
>> than mutex-based. Hence, this "unacceptably slow is nonsense.
>
> Of course it is slower because you wait N timeslices.
>
>> A thread that waits for the contended mutex is, upon locking the
>> mutex, scheduled identically to yield so no difference with mutex
>
> No, a thread waiting for a mutex doesn's spin when the thread holding
> the spinlock is scheduled away but it is sleeping inside the kernel.
> That's more efficient.

So, you must really dislike adaptive mutex designs, right? If so, why?
They can spin several times before waiting in the kernel... Really slow
path, so to speak. Btw, do you think that adaptive mutex are really bad?
If so, why?

>
>> On the opposite, I can see in glibc source code that pthread_once for
>> linux (which is most common) does not use mutex. There is no
>> "creation", pthread_once_t is an int.
>
> pthread_once uses a mutex internally for sure.
>
>

Re: Thread-safe initialization of static objects

<udieo4$63u7$4@dont-email.me>

  copy mid

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

  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: Sat, 9 Sep 2023 11:49:39 -0700
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <udieo4$63u7$4@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 9 Sep 2023 18:49:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6a3b350a6648114514a7c92794ed6367";
logging-data="200647"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//wStmmNH8L+7msn8aFIfhhu7Gnw6Q4fE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:vttesbUBfaK+fMNIYCsKvXyXaJU=
In-Reply-To: <udi9s5$5f8c$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 9 Sep 2023 18:49 UTC

On 9/9/2023 10:26 AM, Bonita Montero wrote:
> Am 09.09.2023 um 16:54 schrieb Paavo Helde:
>
>>  From there I can see that it calls atomic_load_acquire(). Only if
>> this fails, will it call __pthread_once_slow() in the slow branch,
>> which uses futexes. Some other googling reveals that: "a futex ("Fast
>> Userspace Mutex") isn't a mutex at all"
>
> Doesn't matter since the standard should honor static initialization
> that fails because of synchronization woes. And as I've shown with
> my code MSVC, libstdc++ and libc++ have a mutex per static initiali-
> zation, so pthread_once doesn't apply here.

Huh? pthread_once totally applies here. Windows has futex like abilities.

Re: Thread-safe initialization of static objects

<udig8l$6ddc$1@dont-email.me>

  copy mid

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

  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: Sat, 9 Sep 2023 21:15:33 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <udig8l$6ddc$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 19:15:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="40fc38809065ec861f987de259190c0a";
logging-data="210348"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19c0OkxFPlsLnuJ9xIy4EYpYk07qsWwYD0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:H3TKV5K9bLOJtFIHP4sNfFDn8dg=
Content-Language: de-DE
In-Reply-To: <q82LM.1449511$GMN3.1347862@fx16.iad>
 by: Bonita Montero - Sat, 9 Sep 2023 19:15 UTC

Am 09.09.2023 um 19:42 schrieb Pavel:

> If Microsoft implemented standard wrongly, it's Microsoft's failure, not
> the standard's.

The standard doesn't say anything to the issue I've shown.

Re: Thread-safe initialization of static objects

<udigbv$6ddc$2@dont-email.me>

  copy mid

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

  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: Sat, 9 Sep 2023 21:17:19 +0200
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <udigbv$6ddc$2@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> <udieo4$63u7$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 19:17:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="40fc38809065ec861f987de259190c0a";
logging-data="210348"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ggfybl14UvYpzqw2Z7W3EjiEBpcfRzY4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:PTgj1StDZYIO90NjouM4/C3jxV4=
In-Reply-To: <udieo4$63u7$4@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 9 Sep 2023 19:17 UTC

Am 09.09.2023 um 20:49 schrieb Chris M. Thomasson:

> Huh? pthread_once totally applies here. Windows has futex like abilities.

It doesn't matter what Posix or Win32 says. The standard should
honour that static initialization might fail because of creating
a synchronization primitive or synchronizing on them. The standard
shoudln't require that this never fails.


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

Pages:123456789101112131415161718192021222324252627
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor