Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"I have five dollars for each of you." -- Bernhard Goetz


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

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

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

<uefhkh$3371r$1@dont-email.me>

  copy mid

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

  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, 20 Sep 2023 12:36:48 -0700
Organization: A noiseless patient Spider
Lines: 72
Message-ID: <uefhkh$3371r$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <EjoNM.17221$DXgc.9861@fx36.iad>
<ue5tfc$7pns$1@dont-email.me> <zPGNM.14021$BMnd.11871@fx04.iad>
<ue7f6g$frod$2@dont-email.me> <ue7gdm$fumm$4@dont-email.me>
<ue7hc3$g7tm$1@dont-email.me> <ue7r4j$i3u1$1@dont-email.me>
<ue8mid$q534$2@dont-email.me> <O9XNM.16067$JX2f.12159@fx13.iad>
<20230918110147.326@kylheku.com> <uea42g$1sn0e$2@dont-email.me>
<20230918123203.723@kylheku.com> <ueb3q5$2638l$5@dont-email.me>
<20230918205859.263@kylheku.com> <uebn7l$293m3$3@dont-email.me>
<20230919094617.529@kylheku.com> <uecn60$2f5ud$2@dont-email.me>
<20230919140127.732@kylheku.com> <uedpdo$2orvq$5@dont-email.me>
<20230919204410.51@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 19:36:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="3251259"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX188QFNTOWwl4YoiKLiT26D3uoUEGdnVnQs="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:x1+CYMyQ7FdiqOwghDaF43doY+g=
In-Reply-To: <20230919204410.51@kylheku.com>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 20 Sep 2023 19:36 UTC

On 9/19/2023 8:52 PM, Kaz Kylheku wrote:
> On 2023-09-20, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 19.09.2023 um 23:08 schrieb Kaz Kylheku:
>>
>>> int B();
>>> int A();
>>>
>>> int A()
>>> {
>>> static int a = B();
>>> return 'A';
>>> }
>>>
>>> int B()
>>> {
>>> static int b = A();
>>> return 'B';
>>> }
>>>
>>> int main()
>>> {
>>> A();
>>> return 0;
>>> }
>>
>> Static initialization woudln't lock for trivial types.
>
> Why would you snip away an actual transcript showing that something is
> happening, and claim it woudln't happen?
>
> It has to lock.
>
> If two threads execute this at around the same time:
>
> static int a = B();
>
> one of them has to be delayed until the other finishes
> executing B(), initializes a, sets some flag indicating
> that this was done, and releases the lock.
>
> The type of a is trivial, but B() isn't trivial; B()
> can be an arbitrarily complex calculation, and a is not
> initialized until that calculation completes.
>
> Here is part of the transcript again: the gdb backtrace:
>
> (gdb) r
> Starting program: /home/kaz/junk/a.out
> [Thread debugging using libthread_db enabled]
> Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
> ^C
> Program received signal SIGINT, Interrupt.
> 0xb7fd6d09 in __kernel_vsyscall ()
> (gdb) bt
> #0 0xb7fd6d09 in __kernel_vsyscall ()
> #1 0xb7d07197 in syscall () at
> ../sysdeps/unix/sysv/linux/i386/syscall.S:29
> #2 0xb7ea379d in __cxa_guard_acquire () from
> /usr/lib/i386-linux-gnu/libstdc++.so.6
> #3 0x004006c2 in A() ()
> #4 0x00400773 in B() ()
> #5 0x004006d7 in A() ()
> #6 0x004007e5 in main ()
>
> The __cxa_guard_acquire that's making a __kernel_vsyscall is
> the locking mechanism guarding the "static int a = B()".
>
> It has recursively deadlocked. The main thread already owns
> that lock and when it tries to acquire it again, it hangs.
>

A recursive lock would be interesting here...

Re: Thread-safe initialization of static objects

<e88fdf69-cac4-4b2a-a567-30f546d4cb41n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:4f93:0:b0:657:94c7:9be2 with SMTP id em19-20020ad44f93000000b0065794c79be2mr57805qvb.1.1695243777862;
Wed, 20 Sep 2023 14:02:57 -0700 (PDT)
X-Received: by 2002:a05:6870:1844:b0:1d6:3c76:e1c9 with SMTP id
u4-20020a056870184400b001d63c76e1c9mr1386675oaf.6.1695243777620; Wed, 20 Sep
2023 14:02:57 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Wed, 20 Sep 2023 14:02:57 -0700 (PDT)
In-Reply-To: <40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:f871:9f24:c014:651;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:f871:9f24:c014:651
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com> <ueff5s$32ncr$3@dont-email.me>
<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e88fdf69-cac4-4b2a-a567-30f546d4cb41n@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5chosen@yahoo.com (Michael S)
Injection-Date: Wed, 20 Sep 2023 21:02:57 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 9955
 by: Michael S - Wed, 20 Sep 2023 21:02 UTC

On Wednesday, September 20, 2023 at 10:36:18 PM UTC+3, Michael S wrote:
> On Wednesday, September 20, 2023 at 9:55:09 PM UTC+3, Chris M. Thomasson wrote:
> > On 9/20/2023 9:48 AM, Michael S wrote:
> > > On Wednesday, September 20, 2023 at 4:18:42 PM UTC+3, Bonita Montero wrote:
> > >> Am 20.09.2023 um 13:54 schrieb Richard Damon:
> > >>
> > >>> Nope, shows your lack of imagination.
> > >> Show me how sleeping works in situation without waiting for a semaphore
> > >> which is part of the slow path of a mutex.
> > >
> > > That's approximately what I'd do using old (pre-Vista) Win32 API.
> > > With newer APIs I can probably do better.
> > >
> > > static CRITICAL_SECTION st_flagUpdatesCs;
> > > static HANDLE st_initWaitEv;
> > >
> > > void guarda_init_before_main()
> > > {
> > > InitializeCriticalSection(&st_flagUpdatesCs);
> > > st_initWaitEv = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
> > > }
> > >
> > > int guard_acquire(volatile char* per_object)
> > > {
> > > int ret = 0; // tell to caller that initialization completed
> > > if (*per_object != 1) {
> > > EnterCriticalSection(&st_flagUpdatesCs);
> > > if (*per_object != 1) {
> > > // For sake of brevity let's ignore guarding/abortion
> > > // against recursive initialization of the same object by the same thread
> > > // According to C++ standard it's UB, anyway
> > > while (*per_object & 2) { // init in progress
> > > *per_object |= 4; // announce wait
> > > LeaveCriticalSection(&st_flagUpdatesCs);
> > > WaitForSingleObject(st_initWaitEv, INFINITE);
> > > EnterCriticalSection(&st_flagUpdatesCs);
> > > }
> > > if (*per_object != 1) {
> > > *per_object |= 2; // set init in progress
> > > ret = 1; // tell to caller to perform initialization
> > > }
> > > }
> > > LeaveCriticalSection(&st_flagUpdatesCs);
> > > }
> > > return ret;
> > > }
> > >
> > > void guard_release(volatile char* per_object)
> > > {
> > > EnterCriticalSection(&st_flagUpdatesCs);
> > > char val = *per_object;
> > > *per_object = 1; // mark object as initialized
> > > LeaveCriticalSection(&st_flagUpdatesCs);
> > >
> > > if (val & 4) // there are waiting threads
> > > PulseEvent(st_initWaitEv); // wake them up
> > ^^^^^^^^^^^^^^^^^
> >
> > PulseEvent has issues.... Take special note of:
> >
> > https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-pulseevent
> > __________
> > Note This function is unreliable and should not be used. It exists
> > mainly for backward compatibility. For more information, see Remarks.
> > __________
> >
> > So, well, shit.
> >
> > > }
> It's unlikely to be unreliable in this particular circumstances.
> But apart from it, my code has bug (race condition) :(
> Just shows that this sort of crap is not easy.
>
> The simple, if inelegant, fix is to replace
> WaitForSingleObject(st_initWaitEv, INFINITE);
> with
> WaitForSingleObject(st_initWaitEv, 100);
>
> More theoretically sound, but practically bad fix would be to
> replace critical section with mutex. Like that:
>
>
> static HANDLE st_flagUpdatesMtx;
> static HANDLE st_initWaitEv;
>
> void guarda_init_before_main()
> {
> st_flagUpdatesMtx = CreateMutex(NULL, FALSE, NULL);
> st_initWaitEv = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
> }
>
> int guard_acquire(volatile char* per_object)
> {
> int ret = 0; // tell to caller that initialization completed
> if (*per_object != 1) {
> WaitForSingleObject(st_flagUpdatesMtx, INFINITE); // Enter guarded section
> if (*per_object != 1) {
> // For sake of brevity let's ignore guarding/abortion
> // against recursive initialization of the same object by the same thread
> // According to C++ standard it's UB, anyway
> while (*per_object & 2) { // init in progress
> *per_object |= 4; // announce wait
> // combine ReleaseMutex(st_flagUpdatesMtx);
> // and WaitForSingleObject(st_initWaitEv, INFINITE);
> // in a single call thus avoiding race condition with guard_release()
> SignalObjectAndWait(st_flagUpdatesMtx, st_initWaitEv, INFINITE, FALSE);
>
> WaitForSingleObject(st_flagUpdatesMtx, INFINITE); // re-enter guarded section
> }
> if (*per_object != 1) {
> *per_object |= 2; // set init in progress
> ret = 1; // tell to caller to perform initialization
> }
> }
> ReleaseMutex(st_flagUpdatesMtx); // Leave guarded section
> }
> return ret;
> }
>
> void guard_release(volatile char* per_object)
> {
> WaitForSingleObject(st_flagUpdatesMtx, INFINITE); // Enter guarded section
> char val = *per_object;
> *per_object = 1; // mark object as initialized
> ReleaseMutex(st_flagUpdatesMtx); // Leave guarded section
> if (val & 4) // there are waiting threads
> PulseEvent(st_initWaitEv); // wake them up
> }
> In practice it is bad solution, because unlike in the first variant, here
> we need system calls even in the most common un-contended case.
>
> Naturally, for Vista and later it's all much easier.

Here is something that looks like better solution for pre-Vista Windows.
Both more robust and more performant than my previous attempts.
More performant in a sense that waiting threads are not awoken spuriously.

void guarda_init_before_main()
{ InitializeCriticalSection(&st_flagUpdatesCs);
}

static void doNothing(ULONG_PTR) {}

static void WakeUpWaitingThread(DWORD threadId)
{ HANDLE hThread = OpenThread(THREAD_SET_CONTEXT, FALSE, threadId);
QueueUserAPC(doNothing, hThread, 0);
CloseHandle(hThread);
}

int guard_acquire(volatile per_object_guard_t* pGuard)
{ if (pGuard->done)
return 0; // tell to caller that initialization completed

EnterCriticalSection(&st_flagUpdatesCs);
if (pGuard->done) {
LeaveCriticalSection(&st_flagUpdatesCs);
return 0; // tell to caller that initialization completed
}

// For sake of brevity let's ignore guarding/abortion
// against recursive initialization of the same object by the same thread
// According to C++ standard it's UB, anyway
if (!pGuard->inProgress) {
pGuard->inProgress = true;
pGuard->waitingThreadId = 0; // for illustration, not necessary
LeaveCriticalSection(&st_flagUpdatesCs);
return 1; // tell to caller to perform initialization
}
// init in progress

// insert current tread ID as the first in chain of waiting threads
DWORD previousWaitingThreadId = pGuard->waitingThreadId;
pGuard->waitingThreadId = GetCurrentThreadId();
// Wait for completion of the initialization
// most likely loop is not necessary. But it does no harm
do {
LeaveCriticalSection(&st_flagUpdatesCs);
SleepEx(INFINITE, TRUE);
EnterCriticalSection(&st_flagUpdatesCs);
} while (!pGuard->done);

// init completed by other thread
LeaveCriticalSection(&st_flagUpdatesCs);

if (previousWaitingThreadId != 0)
WakeUpWaitingThread(previousWaitingThreadId); // wake up next waiting thread in chain

return 0; // tell to caller that initialization completed
}

void guard_release(volatile per_object_guard_t* pGuard)
{ EnterCriticalSection(&st_flagUpdatesCs);
DWORD waitingThreadId = pGuard->waitingThreadId;
pGuard->done = true; // mark object as initialized
LeaveCriticalSection(&st_flagUpdatesCs);

if (waitingThreadId != 0)
WakeUpWaitingThread(waitingThreadId); // wake up first waiting thread in chain
}

But it's a night here, so bugs are very possible.

Re: Thread-safe initialization of static objects

<20230920140456.221@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Wed, 20 Sep 2023 21:09:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <20230920140456.221@kylheku.com>
References: <udafjf$2joeq$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<EjoNM.17221$DXgc.9861@fx36.iad> <ue5tfc$7pns$1@dont-email.me>
<zPGNM.14021$BMnd.11871@fx04.iad> <ue7f6g$frod$2@dont-email.me>
<ue7gdm$fumm$4@dont-email.me> <ue7hc3$g7tm$1@dont-email.me>
<ue7r4j$i3u1$1@dont-email.me> <ue8mid$q534$2@dont-email.me>
<O9XNM.16067$JX2f.12159@fx13.iad> <20230918110147.326@kylheku.com>
<uea42g$1sn0e$2@dont-email.me> <20230918123203.723@kylheku.com>
<ueb3q5$2638l$5@dont-email.me> <20230918205859.263@kylheku.com>
<uebn7l$293m3$3@dont-email.me> <20230919094617.529@kylheku.com>
<uecn60$2f5ud$2@dont-email.me> <20230919140127.732@kylheku.com>
<uedpdo$2orvq$5@dont-email.me> <20230919204410.51@kylheku.com>
<uefhkh$3371r$1@dont-email.me>
Injection-Date: Wed, 20 Sep 2023 21:09:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b0a008d922e8913c94514619367ad5e6";
logging-data="3283436"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kVaGdS/H7zjvWiTOsLyIVM9dGPTnhaaE="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:X9X3nOavONz4Y6iq8ZMtSoVdxtU=
 by: Kaz Kylheku - Wed, 20 Sep 2023 21:09 UTC

On 2023-09-20, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
> On 9/19/2023 8:52 PM, Kaz Kylheku wrote:
>> It has recursively deadlocked. The main thread already owns
>> that lock and when it tries to acquire it again, it hangs.
>>
>
> A recursive lock would be interesting here...

It wouldn't help; it might "aid and abet" the runaway recursion.

A tri-state flag could detect that case. The tri-state flag initially
shows "uninitialized", and is set into "in progress" during the
initialization and then into the "initialized" state. If it is found in
the "in progress" state, it aborts or throws or whatever, since bad
recursion has been detected.

That would not detect situations of mutual deadlock, like when we
unleash two threads to call A and B. That situation could be
caught by a deadlock-detecting mutex.

Since that would catch the first situation also, there is no need
for that tri-state flag.

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

Re: Thread-safe initialization of static objects

<uefne1$344fm$1@dont-email.me>

  copy mid

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

  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, 20 Sep 2023 14:15:44 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <uefne1$344fm$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <EjoNM.17221$DXgc.9861@fx36.iad>
<ue5tfc$7pns$1@dont-email.me> <zPGNM.14021$BMnd.11871@fx04.iad>
<ue7f6g$frod$2@dont-email.me> <ue7gdm$fumm$4@dont-email.me>
<ue7hc3$g7tm$1@dont-email.me> <ue7r4j$i3u1$1@dont-email.me>
<ue8mid$q534$2@dont-email.me> <O9XNM.16067$JX2f.12159@fx13.iad>
<20230918110147.326@kylheku.com> <uea42g$1sn0e$2@dont-email.me>
<20230918123203.723@kylheku.com> <ueb3q5$2638l$5@dont-email.me>
<20230918205859.263@kylheku.com> <uebn7l$293m3$3@dont-email.me>
<20230919094617.529@kylheku.com> <uecn60$2f5ud$2@dont-email.me>
<20230919140127.732@kylheku.com> <uedpdo$2orvq$5@dont-email.me>
<20230919204410.51@kylheku.com> <uefhkh$3371r$1@dont-email.me>
<20230920140456.221@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 20 Sep 2023 21:15:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f6fa32ff36e5d8cee5f47474b078ec61";
logging-data="3281398"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rwYZaILzFMSJ64D3sGkMPavrGOLxg0JQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:2hXdibzzFE76cwa/x3/0ypTE9d8=
In-Reply-To: <20230920140456.221@kylheku.com>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 20 Sep 2023 21:15 UTC

On 9/20/2023 2:09 PM, Kaz Kylheku wrote:
> On 2023-09-20, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>> On 9/19/2023 8:52 PM, Kaz Kylheku wrote:
>>> It has recursively deadlocked. The main thread already owns
>>> that lock and when it tries to acquire it again, it hangs.
>>>
>>
>> A recursive lock would be interesting here...
>
> It wouldn't help; it might "aid and abet" the runaway recursion.

Right. I do not like to use them, and this would be an interesting way
to get a "false" sense of "security" for some people. I have had to deal
with nightmare scenarios wrt recursive locking.

> A tri-state flag could detect that case. The tri-state flag initially
> shows "uninitialized", and is set into "in progress" during the
> initialization and then into the "initialized" state. If it is found in
> the "in progress" state, it aborts or throws or whatever, since bad
> recursion has been detected.

>
> That would not detect situations of mutual deadlock, like when we
> unleash two threads to call A and B. That situation could be
> caught by a deadlock-detecting mutex.
>
> Since that would catch the first situation also, there is no need
> for that tri-state flag.
>

I think so. Need to think on it. Runaway recursion is bad for the
program. To detect it we could throw this threads stack is about to
explode error. ;^)

The mutex says I am being locked by the same thread again... Recursion
sucks, so I will throw.

Re: Thread-safe initialization of static objects

<20230920151636.124@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Wed, 20 Sep 2023 22:23:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <20230920151636.124@kylheku.com>
References: <udafjf$2joeq$1@dont-email.me> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<AcDOM.64148$%uv8.27450@fx15.iad> <ueevo0$2vkcq$1@dont-email.me>
<ueff33$32ncr$2@dont-email.me>
Injection-Date: Wed, 20 Sep 2023 22:23:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d3d54e4b74191d06bda8ef3fd3367eb7";
logging-data="3310736"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bcJdQzXJ+4gpemoXYpbTdHV3+IWREAIs="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:XKGrALTTYniMgKx5GyjKIFDnY9k=
 by: Kaz Kylheku - Wed, 20 Sep 2023 22:23 UTC

On 2023-09-20, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
> On 9/20/2023 7:31 AM, Bonita Montero wrote:
>> Am 20.09.2023 um 16:20 schrieb Scott Lurndal:
>>
>>> Does the term 'spin lock' have any meaning to you?
>>
>> Spinlocks don't work in userspace since a thread holding a spinlock
>> can be scheduled away, thereby keepin others spinning a long time.
>> Because of that no one uses pure spinlocks, but just mutexes with
>> limited spinning and a slow path.
>>
>
> So, do actually do have a deep hatred of adaptive mutexes, right?

I might have possibly invented those. At least, I wasn't aware of prior
art when I wrote the patch for Glibc.

The reconstructed git history is screwy: the linuxthreads/ChangeLog diff is
in this commit:

https://sourceware.org/git/?p=glibc.git;a=commit;h=a88b96f496c0214424db1219d21ce669fbc102a0

The actual change it refers to is in another commit:

https://sourceware.org/git/?p=glibc.git;a=commit;h=4ad1d0cfbf96c3dbcf0ba104eb9f81bae6bdc6da

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

Re: Thread-safe initialization of static objects

<JCKOM.83797$2ph4.24937@fx14.iad>

  copy mid

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

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

On 9/20/23 9:18 AM, Bonita Montero wrote:
> Am 20.09.2023 um 13:54 schrieb Richard Damon:
>
>> Nope, shows your lack of imagination.
>
> Show me how sleeping works in situation without waiting for a semaphore
> which is part of the slow path of a mutex.
>

What make you think it needs to "sleep". It likely just needs to "Yield"
(like call SwitchToThread) to let other threads get a chance to run, and
then check the initialization status again.

You could also get a bit fancier and build a linked list of waiting
threads that suspend themselfs, and have the initializing thread check
if anyone is waiting and resume them.

You are just showing your lack of imagination.

Re: Thread-safe initialization of static objects

<z1MOM.13123$TwR4.1703@fx46.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.swapon.de!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx46.iad.POSTED!not-for-mail
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <udn883$135tl$1@dont-email.me>
<udnqhc$160ia$3@dont-email.me> <udoico$1d0tr$1@dont-email.me>
<udotj5$1e8do$2@dont-email.me> <udpbun$1gf8c$1@dont-email.me>
<udqatg$1mar3$1@dont-email.me> <udqbg3$1mdv6$1@dont-email.me>
<udqq8u$1p3q6$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <ue4t6i$3um57$1@dont-email.me>
<ue5sp2$7ms9$1@dont-email.me> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me>
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: <ueb3f7$2638l$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 37
Message-ID: <z1MOM.13123$TwR4.1703@fx46.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Thu, 21 Sep 2023 00:22:55 UTC
Date: Wed, 20 Sep 2023 20:22:49 -0400
X-Received-Bytes: 3107
 by: Pavel - Thu, 21 Sep 2023 00:22 UTC

Bonita Montero wrote:
> Am 19.09.2023 um 02:15 schrieb Pavel:
>
>> Or, when there is nothing to create, there is nothing to defer,
>
> I've shown that static initialization actually uses a mutex per object.
You did not even show that they use mutex at all (which they don't as it
turned out on either Windows or Linux)

> The runtimes for sure not pre-create a number of mutexes for that since
As per above, they don't have to be mutices and in fact are not mutices
on widely prevalent systems so this is irrelevant. More to the point, as
an example, Linux runtime creates a static pthread_once_t per static
variable.

> static objects are just zeroed memory before initialization.
This is irrelevant to the discussion, but still is wrong:

static objects that can be constant-initialized at all can be
initialized to any integral (at least) constant value or structure
thereof. They are only set to zero if they are not initialized
explicitly. Then the memory values are set up at program load time, i.e.
before runtime. This said, the initial value for pthread_once_t control
is chosen to be 0 on my system:

pthread_once_t once_control = PTHREAD_ONCE_INIT;

where

#define PTHREAD_ONCE_INIT 0

but it could be constant-initialized to anything else, e.g. to -1 if the
authors thought it were necessary.

Re: Thread-safe initialization of static objects

<ZjMOM.9398$EIy4.4982@fx48.iad>

  copy mid

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

  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!fx48.iad.POSTED!not-for-mail
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <udigbv$6ddc$2@dont-email.me>
<0M4LM.170545$uEkc.43152@fx35.iad> <udk0hm$h1e9$3@dont-email.me>
<udl0ti$lp5v$4@dont-email.me> <udn883$135tl$1@dont-email.me>
<udnqhc$160ia$3@dont-email.me> <udoico$1d0tr$1@dont-email.me>
<udotj5$1e8do$2@dont-email.me> <udpbun$1gf8c$1@dont-email.me>
<udqatg$1mar3$1@dont-email.me> <udqbg3$1mdv6$1@dont-email.me>
<udqq8u$1p3q6$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <EjoNM.17221$DXgc.9861@fx36.iad>
<ue5tfc$7pns$1@dont-email.me> <zPGNM.14021$BMnd.11871@fx04.iad>
<ue7f6g$frod$2@dont-email.me> <LWINM.13017$ZkX3.12109@fx09.iad>
<ue8mn0$q534$4@dont-email.me> <uY5OM.30730$Yxl8.13078@fx14.iad>
<ueb3sb$2638l$6@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: <ueb3sb$2638l$6@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 18
Message-ID: <ZjMOM.9398$EIy4.4982@fx48.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Thu, 21 Sep 2023 00:42:33 UTC
Date: Wed, 20 Sep 2023 20:42:32 -0400
X-Received-Bytes: 2379
 by: Pavel - Thu, 21 Sep 2023 00:42 UTC

Bonita Montero wrote:
> Am 19.09.2023 um 02:30 schrieb Pavel:
>
>>> Call it mutex or whatever: the standard requires that contenders
>>> sleep in place, and that's only possible with something that works
>>> like a mutex.
>
>> Define "works like a mutex".
>
> A lock which makes a thread to sleep on contention.

Then there are lots of these on prevalent platforms that either cannot
fail at all on either lock if initialized propoerty or on initialization
; or those that cannot fail if initialized in a certain way,
including some type of mutexes. All of these are perfectly suitable for
use in static initialization to make it thread-safe. Hence, no need to
change the standard.

Re: Thread-safe initialization of static objects

<ueg79a$36os2$1@dont-email.me>

  copy mid

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

  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, 20 Sep 2023 18:46:15 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <ueg79a$36os2$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<AcDOM.64148$%uv8.27450@fx15.iad> <ueevo0$2vkcq$1@dont-email.me>
<ueff33$32ncr$2@dont-email.me> <20230920151636.124@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 01:46:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3367810"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18pxf+NxkKM/Arzryok2435JYOMa3Sll5Y="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:GxJfYOrf6XYmZfnJUNFap6eX+QA=
Content-Language: en-US
In-Reply-To: <20230920151636.124@kylheku.com>
 by: Chris M. Thomasson - Thu, 21 Sep 2023 01:46 UTC

On 9/20/2023 3:23 PM, Kaz Kylheku wrote:
> On 2023-09-20, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>> On 9/20/2023 7:31 AM, Bonita Montero wrote:
>>> Am 20.09.2023 um 16:20 schrieb Scott Lurndal:
>>>
>>>> Does the term 'spin lock' have any meaning to you?
>>>
>>> Spinlocks don't work in userspace since a thread holding a spinlock
>>> can be scheduled away, thereby keepin others spinning a long time.
>>> Because of that no one uses pure spinlocks, but just mutexes with
>>> limited spinning and a slow path.
>>>
>>
>> So, do actually do have a deep hatred of adaptive mutexes, right?
>
> I might have possibly invented those. At least, I wasn't aware of prior
> art when I wrote the patch for Glibc.
>
> The reconstructed git history is screwy: the linuxthreads/ChangeLog diff is
> in this commit:
>
> https://sourceware.org/git/?p=glibc.git;a=commit;h=a88b96f496c0214424db1219d21ce669fbc102a0
>
> The actual change it refers to is in another commit:
>
> https://sourceware.org/git/?p=glibc.git;a=commit;h=4ad1d0cfbf96c3dbcf0ba104eb9f81bae6bdc6da
>

Nice! NPTL by Ulrich Drepper definitely rings a bell for me. Btw, have
you messed around with distributed mutexes before? I remember reading
about one a while back called the, iirc, M lock. I did an implementation
and posted it over on comp.arch, that thread might be hard to find.
Basically each thread contends on its local state. I wonder if that
paper is still around.

Re: Thread-safe initialization of static objects

<ueg7s6$3agvp$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Wed, 20 Sep 2023 18:56:20 -0700
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <ueg7s6$3agvp$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<AcDOM.64148$%uv8.27450@fx15.iad> <ueevo0$2vkcq$1@dont-email.me>
<ueff33$32ncr$2@dont-email.me> <20230920151636.124@kylheku.com>
<ueg79a$36os2$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 01:56:22 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3490809"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WVLKUk9yvbMhADCCZwYSI2mcFwdBJxcQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:ngOcUCdFasRvNCSJW6wPdWWUB2s=
Content-Language: en-US
In-Reply-To: <ueg79a$36os2$1@dont-email.me>
 by: Chris M. Thomasson - Thu, 21 Sep 2023 01:56 UTC

On 9/20/2023 6:46 PM, Chris M. Thomasson wrote:
> On 9/20/2023 3:23 PM, Kaz Kylheku wrote:
>> On 2023-09-20, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>> On 9/20/2023 7:31 AM, Bonita Montero wrote:
>>>> Am 20.09.2023 um 16:20 schrieb Scott Lurndal:
>>>>
>>>>> Does the term 'spin lock' have any meaning to you?
>>>>
>>>> Spinlocks don't work in userspace since a thread holding a spinlock
>>>> can be scheduled away, thereby keepin others spinning a long time.
>>>> Because of that no one uses pure spinlocks, but just mutexes with
>>>> limited spinning and a slow path.
>>>>
>>>
>>> So, do actually do have a deep hatred of adaptive mutexes, right?
>>
>> I might have possibly invented those. At least, I wasn't aware of prior
>> art when I wrote the patch for Glibc.
>>
>> The reconstructed git history is screwy: the linuxthreads/ChangeLog
>> diff is
>> in this commit:
>>
>> https://sourceware.org/git/?p=glibc.git;a=commit;h=a88b96f496c0214424db1219d21ce669fbc102a0
>>
>> The actual change it refers to is in another commit:
>>
>> https://sourceware.org/git/?p=glibc.git;a=commit;h=4ad1d0cfbf96c3dbcf0ba104eb9f81bae6bdc6da
>>
>
> Nice! NPTL by Ulrich Drepper definitely rings a bell for me. Btw, have
> you messed around with distributed mutexes before? I remember reading
> about one a while back called the, iirc, M lock. I did an implementation
> and posted it over on comp.arch, that thread might be hard to find.
> Basically each thread contends on its local state. I wonder if that
> paper is still around.

Have not found it yet, but I did find this old post of mine fro 2005:

https://groups.google.com/g/comp.arch/c/shshLdF1uqs

About Sun's KCSS, k compare single swap.

Re: Thread-safe initialization of static objects

<ueg8gt$3agvp$2@dont-email.me>

  copy mid

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

  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, 20 Sep 2023 19:07:22 -0700
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <ueg8gt$3agvp$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<AcDOM.64148$%uv8.27450@fx15.iad> <ueevo0$2vkcq$1@dont-email.me>
<ueff33$32ncr$2@dont-email.me> <20230920151636.124@kylheku.com>
<ueg79a$36os2$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 02:07:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3490809"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+J8j6kk8dU8HqbcWhSIqejb0iBEEIkvSc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:XqYGKO4mCZx0a8oCou4+P4AJl7A=
Content-Language: en-US
In-Reply-To: <ueg79a$36os2$1@dont-email.me>
 by: Chris M. Thomasson - Thu, 21 Sep 2023 02:07 UTC

On 9/20/2023 6:46 PM, Chris M. Thomasson wrote:
> On 9/20/2023 3:23 PM, Kaz Kylheku wrote:
>> On 2023-09-20, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>> On 9/20/2023 7:31 AM, Bonita Montero wrote:
>>>> Am 20.09.2023 um 16:20 schrieb Scott Lurndal:
>>>>
>>>>> Does the term 'spin lock' have any meaning to you?
>>>>
>>>> Spinlocks don't work in userspace since a thread holding a spinlock
>>>> can be scheduled away, thereby keepin others spinning a long time.
>>>> Because of that no one uses pure spinlocks, but just mutexes with
>>>> limited spinning and a slow path.
>>>>
>>>
>>> So, do actually do have a deep hatred of adaptive mutexes, right?
>>
>> I might have possibly invented those. At least, I wasn't aware of prior
>> art when I wrote the patch for Glibc.
>>
>> The reconstructed git history is screwy: the linuxthreads/ChangeLog
>> diff is
>> in this commit:
>>
>> https://sourceware.org/git/?p=glibc.git;a=commit;h=a88b96f496c0214424db1219d21ce669fbc102a0
>>
>> The actual change it refers to is in another commit:
>>
>> https://sourceware.org/git/?p=glibc.git;a=commit;h=4ad1d0cfbf96c3dbcf0ba104eb9f81bae6bdc6da
>>
>
> Nice! NPTL by Ulrich Drepper definitely rings a bell for me. Btw, have
> you messed around with distributed mutexes before? I remember reading
> about one a while back called the, iirc, M lock. I did an implementation
> and posted it over on comp.arch, that thread might be hard to find.
> Basically each thread contends on its local state. I wonder if that
> paper is still around.

I found it! YEAH!!!

https://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=251B3F934CAB552200421D0570D2D2A1?doi=10.1.1.48.6490&rep=rep1&type=pdf

Re: Thread-safe initialization of static objects

<ueg8o7$3agvp$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Wed, 20 Sep 2023 19:11:13 -0700
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <ueg8o7$3agvp$3@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<AcDOM.64148$%uv8.27450@fx15.iad> <ueevo0$2vkcq$1@dont-email.me>
<ueff33$32ncr$2@dont-email.me> <20230920151636.124@kylheku.com>
<ueg79a$36os2$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 02:11:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3490809"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OOusznglpKDBhKUnUT6LHtTlV6TOgugc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:iRMd9dpWZoBXtoedmvzMI+BeXxU=
Content-Language: en-US
In-Reply-To: <ueg79a$36os2$1@dont-email.me>
 by: Chris M. Thomasson - Thu, 21 Sep 2023 02:11 UTC

On 9/20/2023 6:46 PM, Chris M. Thomasson wrote:
> On 9/20/2023 3:23 PM, Kaz Kylheku wrote:
>> On 2023-09-20, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>>> On 9/20/2023 7:31 AM, Bonita Montero wrote:
>>>> Am 20.09.2023 um 16:20 schrieb Scott Lurndal:
>>>>
>>>>> Does the term 'spin lock' have any meaning to you?
>>>>
>>>> Spinlocks don't work in userspace since a thread holding a spinlock
>>>> can be scheduled away, thereby keepin others spinning a long time.
>>>> Because of that no one uses pure spinlocks, but just mutexes with
>>>> limited spinning and a slow path.
>>>>
>>>
>>> So, do actually do have a deep hatred of adaptive mutexes, right?
>>
>> I might have possibly invented those. At least, I wasn't aware of prior
>> art when I wrote the patch for Glibc.
>>
>> The reconstructed git history is screwy: the linuxthreads/ChangeLog
>> diff is
>> in this commit:
>>
>> https://sourceware.org/git/?p=glibc.git;a=commit;h=a88b96f496c0214424db1219d21ce669fbc102a0
>>
>> The actual change it refers to is in another commit:
>>
>> https://sourceware.org/git/?p=glibc.git;a=commit;h=4ad1d0cfbf96c3dbcf0ba104eb9f81bae6bdc6da
>>
>
> Nice! NPTL by Ulrich Drepper definitely rings a bell for me. Btw, have
> you messed around with distributed mutexes before? I remember reading
> about one a while back called the, iirc, M lock. I did an implementation
> and posted it over on comp.arch, that thread might be hard to find.
> Basically each thread contends on its local state. I wonder if that
> paper is still around.
Finally found it.

http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=251B3F934CAB552200421D0570D2D2A1?doi=10.1.1.48.6490&rep=rep1&type=pdf

Re: Thread-safe initialization of static objects

<uege22$3bfsj$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!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, 21 Sep 2023 05:41:56 +0200
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <uege22$3bfsj$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 21 Sep 2023 03:41:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3522451"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18N+Qv10V4KemotVWO9ljwHGfEph1NEXaY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:OQZfDLmTEjQNc6tqg8B/KvhMfqs=
In-Reply-To: <5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 03:41 UTC

Am 20.09.2023 um 18:48 schrieb Michael S:

> On Wednesday, September 20, 2023 at 4:18:42 PM UTC+3, Bonita Montero wrote:

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

> That's approximately what I'd do using old (pre-Vista) Win32 API.
> With newer APIs I can probably do better.
>
> static CRITICAL_SECTION st_flagUpdatesCs;
> static HANDLE st_initWaitEv;

CRITICAL sections include an event which in turn is a binary semaphore.
And the naked event itself is also a semaphore.

Re: Thread-safe initialization of static objects

<uege3r$3bfsj$2@dont-email.me>

  copy mid

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

  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, 21 Sep 2023 05:42:53 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uege3r$3bfsj$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 03:42:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3522451"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Q2okJ7TR6D4ARQGF3Vhqs0P0ADRi5sw8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:NjijS5nyFr1fsCnJRTcELYObGuk=
In-Reply-To: <JCKOM.83797$2ph4.24937@fx14.iad>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 03:42 UTC

Am 21.09.2023 um 00:46 schrieb Richard Damon:

> It likely just needs to "Yield"
> (like call SwitchToThread) to let other threads get a chance to run, and
> then check the initialization status again.

A yield yould be to inefficient.

Re: Thread-safe initialization of static objects

<uegea8$3bfsj$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.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, 21 Sep 2023 05:46:18 +0200
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uegea8$3bfsj$3@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<EjoNM.17221$DXgc.9861@fx36.iad> <ue5tfc$7pns$1@dont-email.me>
<zPGNM.14021$BMnd.11871@fx04.iad> <ue7f6g$frod$2@dont-email.me>
<ue7gdm$fumm$4@dont-email.me> <ue7hc3$g7tm$1@dont-email.me>
<ue7r4j$i3u1$1@dont-email.me> <ue8mid$q534$2@dont-email.me>
<O9XNM.16067$JX2f.12159@fx13.iad> <20230918110147.326@kylheku.com>
<uea42g$1sn0e$2@dont-email.me> <20230918123203.723@kylheku.com>
<ueb3q5$2638l$5@dont-email.me> <20230918205859.263@kylheku.com>
<uebn7l$293m3$3@dont-email.me> <20230919094617.529@kylheku.com>
<uecn60$2f5ud$2@dont-email.me> <20230919140127.732@kylheku.com>
<uedpdo$2orvq$5@dont-email.me> <20230919204410.51@kylheku.com>
<uefhkh$3371r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 03:46:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3522451"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vvR/YxhDZjOWYHBV6whVzHHfCo7A6k7U="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:RwBwD8b9CKgCYkKkkfySl4ZqDpo=
Content-Language: de-DE
In-Reply-To: <uefhkh$3371r$1@dont-email.me>
 by: Bonita Montero - Thu, 21 Sep 2023 03:46 UTC

Am 20.09.2023 um 21:36 schrieb Chris M. Thomasson:

> A recursive lock would be interesting here...

A recursive lock is impossible here since the object might have
been partitially initialized and when coming back this partitial
initialization would happen again.

Re: Thread-safe initialization of static objects

<uegfqq$3bf1v$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!feeder1.feed.usenet.farm!feed.usenet.farm!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, 20 Sep 2023 21:12:09 -0700
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <uegfqq$3bf1v$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
<ueff5s$32ncr$3@dont-email.me>
<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 21 Sep 2023 04:12:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3521599"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tG5vgh9EmfVmGM/5IvpqYuAkeXpeQiNk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:D/ZegTCS7QfrlBTi1wUR3rzOGCs=
In-Reply-To: <40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 21 Sep 2023 04:12 UTC

On 9/20/2023 12:36 PM, Michael S wrote:
> On Wednesday, September 20, 2023 at 9:55:09 PM UTC+3, Chris M. Thomasson wrote:
>> On 9/20/2023 9:48 AM, Michael S wrote:
>>> On Wednesday, September 20, 2023 at 4:18:42 PM UTC+3, Bonita Montero wrote:
>>>> Am 20.09.2023 um 13:54 schrieb Richard Damon:
>>>>
>>>>> Nope, shows your lack of imagination.
>>>> Show me how sleeping works in situation without waiting for a semaphore
>>>> which is part of the slow path of a mutex.
>>>
>>> That's approximately what I'd do using old (pre-Vista) Win32 API.
>>> With newer APIs I can probably do better.
>>>
>>> static CRITICAL_SECTION st_flagUpdatesCs;
>>> static HANDLE st_initWaitEv;
>>>
>>> void guarda_init_before_main()
>>> {
>>> InitializeCriticalSection(&st_flagUpdatesCs);
>>> st_initWaitEv = CreateEvent(NULL, TRUE, FALSE, NULL); // Manual Reset
>>> }
>>>
>>> int guard_acquire(volatile char* per_object)
>>> {
>>> int ret = 0; // tell to caller that initialization completed
>>> if (*per_object != 1) {
>>> EnterCriticalSection(&st_flagUpdatesCs);
>>> if (*per_object != 1) {
>>> // For sake of brevity let's ignore guarding/abortion
>>> // against recursive initialization of the same object by the same thread
>>> // According to C++ standard it's UB, anyway
>>> while (*per_object & 2) { // init in progress
>>> *per_object |= 4; // announce wait
>>> LeaveCriticalSection(&st_flagUpdatesCs);
>>> WaitForSingleObject(st_initWaitEv, INFINITE);
>>> EnterCriticalSection(&st_flagUpdatesCs);
>>> }
>>> if (*per_object != 1) {
>>> *per_object |= 2; // set init in progress
>>> ret = 1; // tell to caller to perform initialization
>>> }
>>> }
>>> LeaveCriticalSection(&st_flagUpdatesCs);
>>> }
>>> return ret;
>>> }
>>>
>>> void guard_release(volatile char* per_object)
>>> {
>>> EnterCriticalSection(&st_flagUpdatesCs);
>>> char val = *per_object;
>>> *per_object = 1; // mark object as initialized
>>> LeaveCriticalSection(&st_flagUpdatesCs);
>>>
>>> if (val & 4) // there are waiting threads
>>> PulseEvent(st_initWaitEv); // wake them up
>> ^^^^^^^^^^^^^^^^^
>>
>> PulseEvent has issues.... Take special note of:
>>
>> https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-pulseevent
>> __________
>> Note This function is unreliable and should not be used. It exists
>> mainly for backward compatibility. For more information, see Remarks.
>> __________
>>
>> So, well, shit.
>>
>>> }
>
> It's unlikely to be unreliable in this particular circumstances.
> But apart from it, my code has bug (race condition) :(
> Just shows that this sort of crap is not easy.

PulseEvent never worked right. Many years ago, I have seem my fair share
of completely broken windows condition variable "things", a lot of them,
sigh... Horrible code, infested with race-conditions that might lead to
deadlock or the nasty lost wake problem. PulseEvent is no good, avoid it
like the damn plague.

Re: Thread-safe initialization of static objects

<uegft8$3bf1v$2@dont-email.me>

  copy mid

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

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

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

Re: Thread-safe initialization of static objects

<ueggh5$3bq3f$1@dont-email.me>

  copy mid

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

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

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

Have you heard about an adaptive backoff, different than a "traditional"
adaptive mutex? It uses several different ways to avoid the kernel. They
are interesting to me.

Re: Thread-safe initialization of static objects

<ueggj7$3bq3f$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Wed, 20 Sep 2023 21:25:10 -0700
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <ueggj7$3bq3f$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <EjoNM.17221$DXgc.9861@fx36.iad>
<ue5tfc$7pns$1@dont-email.me> <zPGNM.14021$BMnd.11871@fx04.iad>
<ue7f6g$frod$2@dont-email.me> <ue7gdm$fumm$4@dont-email.me>
<ue7hc3$g7tm$1@dont-email.me> <ue7r4j$i3u1$1@dont-email.me>
<ue8mid$q534$2@dont-email.me> <O9XNM.16067$JX2f.12159@fx13.iad>
<20230918110147.326@kylheku.com> <uea42g$1sn0e$2@dont-email.me>
<20230918123203.723@kylheku.com> <ueb3q5$2638l$5@dont-email.me>
<20230918205859.263@kylheku.com> <uebn7l$293m3$3@dont-email.me>
<20230919094617.529@kylheku.com> <uecn60$2f5ud$2@dont-email.me>
<20230919140127.732@kylheku.com> <uedpdo$2orvq$5@dont-email.me>
<20230919204410.51@kylheku.com> <uefhkh$3371r$1@dont-email.me>
<uegea8$3bfsj$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 04:25:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3532911"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19EKnDB+aTSo11+h8VVvp6GfF+ihbja50o="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:BpcxEpRet04Dl80k+2BopTJc6ac=
In-Reply-To: <uegea8$3bfsj$3@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 21 Sep 2023 04:25 UTC

On 9/20/2023 8:46 PM, Bonita Montero wrote:
> Am 20.09.2023 um 21:36 schrieb Chris M. Thomasson:
>
>> A recursive lock would be interesting here...
>
> A recursive lock is impossible here since the object might have
> been partitially initialized and when coming back this partitial
> initialization would happen again.
>

A recursive would only provide a, so of, false sense of security wrt the
infinite recursion that is present in the user code.

Re: Thread-safe initialization of static objects

<20230920213647.190@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Thu, 21 Sep 2023 04:46:17 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <20230920213647.190@kylheku.com>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
<ueff5s$32ncr$3@dont-email.me>
<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
<uegfqq$3bf1v$1@dont-email.me>
Injection-Date: Thu, 21 Sep 2023 04:46:17 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d3d54e4b74191d06bda8ef3fd3367eb7";
logging-data="3535413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4Hq9H6l98oD8Y95jj9h5i8yOFzHrO/Gc="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:/vzY3qpS5CeZD8foEngacuBATco=
 by: Kaz Kylheku - Thu, 21 Sep 2023 04:46 UTC

On 2023-09-21, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
> On 9/20/2023 12:36 PM, Michael S wrote:
> PulseEvent never worked right.

PulseEvent might have worked under cooperative multitasking (i.e.
up until Windows 3).

Under cooperative multitasking, effectively, the running thread holds a
de-facto global mutex: one which prevents all other threads from
running. Yielding is like giving up that mutex.

Under cooperative multitasking, certain synchronization objects like
events effectively behave like condition variables due to the implicit
mutex.

You know that the thread which calls PulseEvent is the only one running,
and so after the operation, you know no new threads are waiting on the
event; no thread gets to run until that thread yields.

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

Re: Thread-safe initialization of static objects

<uegi72$3c0of$1@dont-email.me>

  copy mid

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

  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, 20 Sep 2023 21:52:49 -0700
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <uegi72$3c0of$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<5f8dc6ff-2ec2-408e-80e4-a0b6770ca164n@googlegroups.com>
<ueff5s$32ncr$3@dont-email.me>
<40e9de7b-193b-4dc2-a17a-915f1e225d9dn@googlegroups.com>
<uegfqq$3bf1v$1@dont-email.me> <20230920213647.190@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 04:52:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b6da6c06c8d4fdd39d62ecdcb8836b9";
logging-data="3539727"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19affbCnxEeRQbvQfZFwFwj/oYlRxvcMSQ="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:fzIH37jdAcTgPrB0402aIfYwWTI=
Content-Language: en-US
In-Reply-To: <20230920213647.190@kylheku.com>
 by: Chris M. Thomasson - Thu, 21 Sep 2023 04:52 UTC

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

Love your use of the word might here. It almost had to work at one time,
or else was it a forever bugged nightmare from the get go? Humm...

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

Touche. Sounds right, also I take careful note of your use of the word,
_might_. :^) Thanks.

Re: Thread-safe initialization of static objects

<uegsdo$3dj0r$1@dont-email.me>

  copy mid

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

  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, 21 Sep 2023 09:47:05 +0200
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <uegsdo$3dj0r$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue4p5v$3truj$1@dont-email.me>
<ue4t6i$3um57$1@dont-email.me> <ue5sp2$7ms9$1@dont-email.me>
<ue7gb8$fumm$3@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<ueggh5$3bq3f$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 07:47:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3591195"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pWcwnH2dWp1FJJ8HEM1aqhv0FexFdyac="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:iNV+Zjy5q30zC5iKfVntVLt+Zlg=
In-Reply-To: <ueggh5$3bq3f$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 07:47 UTC

Am 21.09.2023 um 06:24 schrieb Chris M. Thomasson:

> On 9/20/2023 8:42 PM, Bonita Montero wrote:

>> A yield yould be to inefficient.

> Have you heard about an adaptive backoff, different than a "traditional"
> adaptive mutex? ...

We discussed yield with periodic polling.

Re: Thread-safe initialization of static objects

<uegsia$3dj0r$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!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, 21 Sep 2023 09:49:31 +0200
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <uegsia$3dj0r$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <0M4LM.170545$uEkc.43152@fx35.iad>
<udk0hm$h1e9$3@dont-email.me> <udl0ti$lp5v$4@dont-email.me>
<udn883$135tl$1@dont-email.me> <udnqhc$160ia$3@dont-email.me>
<udoico$1d0tr$1@dont-email.me> <udotj5$1e8do$2@dont-email.me>
<udpbun$1gf8c$1@dont-email.me> <udqatg$1mar3$1@dont-email.me>
<udqbg3$1mdv6$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<EjoNM.17221$DXgc.9861@fx36.iad> <ue5tfc$7pns$1@dont-email.me>
<zPGNM.14021$BMnd.11871@fx04.iad> <ue7f6g$frod$2@dont-email.me>
<LWINM.13017$ZkX3.12109@fx09.iad> <ue8mn0$q534$4@dont-email.me>
<uY5OM.30730$Yxl8.13078@fx14.iad> <ueb3sb$2638l$6@dont-email.me>
<ZjMOM.9398$EIy4.4982@fx48.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 07:49:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3591195"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vPVwYBDwj90Awm87bwLHfZLNN5fl3FQM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:AusSSfig7XC9b/QJGenEhT0Rch8=
Content-Language: de-DE
In-Reply-To: <ZjMOM.9398$EIy4.4982@fx48.iad>
 by: Bonita Montero - Thu, 21 Sep 2023 07:49 UTC

Am 21.09.2023 um 02:42 schrieb Pavel:

> Then there are lots of these on prevalent platforms that either cannot
> fail ...

The standard shoudln't mandate such platform capabilities. And even
if the wait itself would never fail on all platforms: a mutex' costruc-
tor is noxcept, thereby enforcing delayed creation of the kernel part
of the mutex for the slow path. This might fail on all platforms.

Re: Thread-safe initialization of static objects

<uegsls$3dj0r$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!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, 21 Sep 2023 09:51:25 +0200
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <uegsls$3dj0r$3@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<EjoNM.17221$DXgc.9861@fx36.iad> <ue5tfc$7pns$1@dont-email.me>
<zPGNM.14021$BMnd.11871@fx04.iad> <ue7f6g$frod$2@dont-email.me>
<ue7gdm$fumm$4@dont-email.me> <ue7hc3$g7tm$1@dont-email.me>
<ue7r4j$i3u1$1@dont-email.me> <ue8mid$q534$2@dont-email.me>
<O9XNM.16067$JX2f.12159@fx13.iad> <20230918110147.326@kylheku.com>
<uea42g$1sn0e$2@dont-email.me> <20230918123203.723@kylheku.com>
<ueb3q5$2638l$5@dont-email.me> <20230918205859.263@kylheku.com>
<uebn7l$293m3$3@dont-email.me> <20230919094617.529@kylheku.com>
<uecn60$2f5ud$2@dont-email.me> <20230919140127.732@kylheku.com>
<uedpdo$2orvq$5@dont-email.me> <20230919204410.51@kylheku.com>
<uefhkh$3371r$1@dont-email.me> <uegea8$3bfsj$3@dont-email.me>
<ueggj7$3bq3f$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Sep 2023 07:51:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b85824009984185efe207db9733db046";
logging-data="3591195"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oyycrdfP4yinOVUYljJv26U+LXFg6lWQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:0aq4CnM7ylUflR80xZpGcKz5DVI=
In-Reply-To: <ueggj7$3bq3f$2@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 21 Sep 2023 07:51 UTC

Am 21.09.2023 um 06:25 schrieb Chris M. Thomasson:
> On 9/20/2023 8:46 PM, Bonita Montero wrote:

>> Am 20.09.2023 um 21:36 schrieb Chris M. Thomasson:

>> A recursive lock is impossible here since the object might have
>> been partitially initialized and when coming back this partitial
>> initialization would happen again.

> A recursive would only provide a, so of, false sense of security wrt the
> infinite recursion that is present in the user code.

A recursive lock wouldn't make sense here since the object might have
beein partitially created when it re-enters the mutex which guards the
initialization.

Re: Thread-safe initialization of static objects

<uegt3a$3dj0r$4@dont-email.me>

  copy mid

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

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

Am 21.09.2023 um 02:22 schrieb Pavel:

> You did not even show that they use mutex at all ...

This doesn't matter, because it's clear that static initialization
can't work any other way than with a mutex. Yielding is inacceptable
and spinlocking is inacceptable for reasons I've describet multiple
times.

> This is irrelevant to the discussion, but still is wrong:

There's a non-initialized BSS segment with every executable and every
shared library. This isn't physically part of the executable file but
there's only a declared size to reserve. This block is initially com-
pletely zero. This helps the C++ implementation since every static
local object has initially an initialized-flag which is zero. This
is stored along with the object itself.

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

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


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

Pages:123456789101112131415161718192021222324252627
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor