Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The meek are contesting the will.


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

<uem8h8$nmhb$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 10:44:26 +0200
Organization: A noiseless patient Spider
Lines: 147
Message-ID: <uem8h8$nmhb$1@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <uem0vd$miuj$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 23 Sep 2023 08:44:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2ef0c2489862b938887f0220cc4fc605";
logging-data="776747"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/UoINDT/kR69EqUCGwwDXEPZn/Adp/D4w="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Ywxqsuc74vEs+vrO/FwF4xvJGyw=
Content-Language: de-DE
In-Reply-To: <uem0vd$miuj$2@dont-email.me>
 by: Bonita Montero - Sat, 23 Sep 2023 08:44 UTC

Am 23.09.2023 um 08:35 schrieb Bonita Montero:
> Am 23.09.2023 um 06:41 schrieb Bonita Montero:
>
>> Ok, then Posix threads also can do delayed initialization of the
>> kernel  part for the slow path. And I see that InitializeCriticalSection
>> doesn't fail but EnterCriticalSection can, i.e. there's also delayed
>> initialization.
>
> This is how a mutex whose constructor is noexcept works.
> The code demonstrates the delayed initialization of the slow path:
>
> #if defined(_WIN32)
>     #include <Windows.h>
> #elif defined(__unix__)
>     #include <fcntl.h>
>     #include <semaphore.h>
>  #endif
> #include <iostream>
> #include <atomic>
> #include <cassert>
> #include <system_error>
> #include <memory>
>
> using namespace std;
>
> struct DelayedMutex
> {
>     DelayedMutex() noexcept;
>     ~DelayedMutex();
>     void lock();
>     void unlock();
> private:
> #if defined(_WIN32)
>     HANDLE getSem();
> #elif defined(__unix__)
>     sem_t *getSem();
> #endif
>     atomic<unsigned> m_lockCounter;
> #if defined(_WIN32)
>     static_assert(atomic<HANDLE>::is_always_lock_free, "atomic<HANDLE>
> must be lock-free");
>     atomic<HANDLE> m_hSem;
> #elif defined(__unix__)
>     atomic<sem_t *> m_sem;
> #endif
> };
>
> DelayedMutex::DelayedMutex() noexcept :
>     m_lockCounter( 0 ),
> #if defined(_WIN32)
>     m_hSem( NULL )
> #elif defined(__unix__)
>     m_sem( nullptr )
> #endif
> {
> }
>
> DelayedMutex::~DelayedMutex()
> {
>     assert(!m_lockCounter.load( memory_order_relaxed ));
> #if defined(_WIN32)
>     HANDLE hSem = m_hSem.load( memory_order_relaxed );
>     if( hSem )
>         CloseHandle( hSem );
> #elif defined(__unix__)
>     sem_t *sem = m_sem.load( memory_order_relaxed );
>     if( sem )
>         sem_destroy( sem ),
>         delete sem;
> #endif
> }
>
> void DelayedMutex::lock()
> {
>     unsigned before = m_lockCounter.fetch_add( 1, memory_order_acquire );
>     assert(before != -1);
>     if( !before ) [[likely]]
>         return;
> #if defined(_WIN32)
>     if( WaitForSingleObject( getSem(), INFINITE ) != WAIT_OBJECT_0 )
> [[unlikely]]
> #elif defined(__unix__)
>     if( sem_wait( getSem() ) ) [[unlikely]]
> #endif
>         terminate();
> }
>
> void DelayedMutex::unlock()
> {
>     unsigned before = m_lockCounter.fetch_sub( 1, memory_order_acquire );

release

>     assert(before >= 1);
>     if( before == 1 ) [[likely]]
>         return;
> #if defined(_WIN32)
>     if( !SetEvent( getSem() ) )[[unlikely]]
> #elif defined(__unix__)
>     if( sem_post( getSem() ) ) [[unlikely]]
> #endif
>         terminate();
> }
>
> #if defined(_WIN32)
> HANDLE DelayedMutex::getSem()
> {
>     HANDLE hSem = m_hSem.load( memory_order_relaxed );
>     if( hSem ) [[likely]]
>         return hSem;
>     hSem = CreateEventA( nullptr, FALSE, FALSE, nullptr );
>     if( !hSem ) [[unlikely]]
>         throw system_error( GetLastError(), system_category(), "kernel
> semaphore creation failed" );
>     HANDLE hSemExpected = NULL;
>     if( !m_hSem.compare_exchange_weak( hSemExpected, hSem,
> memory_order_relaxed, memory_order_relaxed ) ) [[unlikely]]
>         CloseHandle( hSem ),
>         hSem = hSemExpected;
>     return hSem;
> }
> #elif defined(__unix__)
> sem_t *DelayedMutex::getSem()
> {
>     sem_t *sem = m_sem.load( memory_order_relaxed );
>     if( sem ) [[likely]]
>         return sem;
>     unique_ptr<sem_t> newSem( make_unique<sem_t>() );
>     if( sem_init( &*newSem, 0, 0 ) ) [[unlikely]]
>         throw system_error( errno, system_category(), "kernel semaphore
> creation failed" );
>     sem_t *semExpected = nullptr;
>     if( m_sem.compare_exchange_weak( semExpected, &*newSem,
> memory_order_relaxed, memory_order_relaxed ) ) [[likely]]
>         sem = &*newSem,
>         newSem.release();
>     else
>         sem_destroy( &*newSem ),
>         sem = semExpected;
>     return sem;
> }
> #endif
>
> int main()
> {
> }

Re: Thread-safe initialization of static objects

<20230923041951.728@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 11:20:21 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <20230923041951.728@kylheku.com>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me>
Injection-Date: Sat, 23 Sep 2023 11:20:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e8f8233b20761decb562e8180f1e470b";
logging-data="820612"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX189rdFnstxBx9n00L6leyXowtFhz7SknD4="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:XXC7ZfstjxCzpxH7DtX3g8yzY0I=
 by: Kaz Kylheku - Sat, 23 Sep 2023 11:20 UTC

On 2023-09-23, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 22.09.2023 um 20:38 schrieb Kaz Kylheku:
>
>> POSIX provides a PTHREAD_MUTEX_INITIALIZER which lets you do just this:
>> pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
>
> Ok, then Posix threads also can do delayed initialization of the
> kernel part for the slow path.

Not in any way that can report a resource error, while remaining
POSIX compliant.

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

<uemlbj$pl2d$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 14:23:17 +0200
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uemlbj$pl2d$1@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Sep 2023 12:23:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2ef0c2489862b938887f0220cc4fc605";
logging-data="840781"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+PX0jXbN1XAG+sbxfGBjNWpt+gbRyVNUo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:GoukYlcZeNDZoJ3bHwCxDZThrVU=
Content-Language: de-DE
In-Reply-To: <20230923041951.728@kylheku.com>
 by: Bonita Montero - Sat, 23 Sep 2023 12:23 UTC

Am 23.09.2023 um 13:20 schrieb Kaz Kylheku:

> Not in any way that can report a resource error, while remaining
> POSIX compliant.

If there's no kernel call while initializing the mutex the slow path
kernel part has to be initialized on contention. So if the initiali-
zation is noexcept, the synchronization needs to be capable to throw
a system_error at least for the delayed inittialization of this mutex
part.
And waiting on the slow path may also fail if there's no space for
en entry in the wait queue of the semaphore. This may not apply for
all operating systems, but this is a theoretical possibility the
standard shout honor.

Re: Thread-safe initialization of static objects

<_UBPM.4607$Lmc1.3371@fx44.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx44.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> <ue7gb8$fumm$3@dont-email.me>
<ue7gkn$g3o5$1@dont-email.me> <20230917121015.206@kylheku.com>
<ue7k2n$gmqv$1@dont-email.me> <20230917123203.524@kylheku.com>
<ue8lst$q25a$2@dont-email.me> <20230918101705.91@kylheku.com>
<uea1rj$1saml$1@dont-email.me> <hK5OM.30729$Yxl8.9621@fx14.iad>
<ueb3f7$2638l$2@dont-email.me> <ueb9vm$271en$3@dont-email.me>
<uebn4f$293m3$2@dont-email.me> <1GhOM.7399$H0Ge.6907@fx05.iad>
<uecac1$2cj86$1@dont-email.me> <iwpOM.265$Sn81.181@fx08.iad>
<uedp93$2orvq$2@dont-email.me> <k4BOM.2058$TwR4.78@fx46.iad>
<ueeret$2upib$1@dont-email.me> <JCKOM.83797$2ph4.24937@fx14.iad>
<uege3r$3bfsj$2@dont-email.me> <ueggh5$3bq3f$1@dont-email.me>
<uegsdo$3dj0r$1@dont-email.me> <uei1g6$3l13h$2@dont-email.me>
<ueiu42$3tima$1@dont-email.me> <eE7PM.16350$3vM.3605@fx37.iad>
<uej5q1$3uji0$1@dont-email.me> <gWePM.25327$fUu6.21270@fx47.iad>
<uek0g4$7fl1$1@dont-email.me> <jBoPM.25452$fUu6.14009@fx47.iad>
<uelq44$lo9j$1@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
Content-Language: en-US
In-Reply-To: <uelq44$lo9j$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 12
Message-ID: <_UBPM.4607$Lmc1.3371@fx44.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: Sat, 23 Sep 2023 09:40:09 -0400
X-Received-Bytes: 2192
 by: Richard Damon - Sat, 23 Sep 2023 13:40 UTC

On 9/23/23 12:38 AM, Bonita Montero wrote:
> Am 23.09.2023 um 00:31 schrieb Richard Damon:
>
>> The fact that you are too stupid to understand how these things
>> actually work and give performant results doesn't mean they don't do
>> it. The fact that they do shows your ignorance.
>
> Yielding at this place and spin-locking in userspace is unprofessional.
>

As is saying you should be able to fail at something that actually can
be done without failing.

Re: Thread-safe initialization of static objects

<24DPM.126389$Hih7.28245@fx11.iad>

  copy mid

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

  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!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx11.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <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> <uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad> <uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad> <uekler$bqj8$1@dont-email.me> <IsoPM.20139$wO91.15127@fx39.iad> <uelqb1$lo9j$3@dont-email.me>
Lines: 14
Message-ID: <24DPM.126389$Hih7.28245@fx11.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 23 Sep 2023 15:00:14 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 23 Sep 2023 15:00:14 GMT
X-Received-Bytes: 1433
 by: Scott Lurndal - Sat, 23 Sep 2023 15:00 UTC

Bonita Montero <Bonita.Montero@gmail.com> writes:
>Am 23.09.2023 um 00:22 schrieb Scott Lurndal:
>
>> That is beside the point. The mutex can be statically initialized
>> ...
>
>This depends on the program which uses the mutex.

Nice topic shift. Not.

We're talking specifically about the implementation mechanism
used to serialize static initialization of a function-scope
static object.

Re: Thread-safe initialization of static objects

<uemvt2$rcgo$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 17:23:16 +0200
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <uemvt2$rcgo$1@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <IsoPM.20139$wO91.15127@fx39.iad>
<uelqb1$lo9j$3@dont-email.me> <24DPM.126389$Hih7.28245@fx11.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Sep 2023 15:23:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2ef0c2489862b938887f0220cc4fc605";
logging-data="897560"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/fAvxAaT5ecG//HP83oddRZnWw4hsOpU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:86Ykn0CGSURqKudR1K17lUjiw88=
In-Reply-To: <24DPM.126389$Hih7.28245@fx11.iad>
Content-Language: de-DE
 by: Bonita Montero - Sat, 23 Sep 2023 15:23 UTC

Am 23.09.2023 um 17:00 schrieb Scott Lurndal:

> We're talking specifically about the implementation mechanism
> used to serialize static initialization of a function-scope
> static object.

I've noticed that a Pthread mutex can be statically initialized and
Win32's CRITICAL_SECTION never fails on initialization either. But
this mean that the semaphore for the slow path on contention has to
be initialized delayed. So if there's contention the sychronization
can still fail.
And even if the semaphore creation doesn't fail the synchronization
on it may fail, depending on the implementation inside the kernel,
f.e. if there's no memory for an additional entry in the wait-queue
on the semaphore.
I think it's ridiculous to say "look at operating system X, it would
never fail" since that's not the way the standard should be designed.
As I've shown this may be true for the synchronization part only but
for sure not for the creation of the binary semaphore.
If std::mutex may fail on synchronization and implementations have
to create the kernel part of the "mutexes" for static initialization
on demand, static initialization should be definedto throw a
system_error.
The probability of this happening is very low, simply because the
probability of contention at this point is usually low and creating
a semaphore may fail under rare conditions, but what happens then
needs to be clearly defined for me. std::mutex has defined means
to handle that, static initialization not.

Re: Thread-safe initialization of static objects

<uenbb7$tfau$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 11:38:30 -0700
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <uenbb7$tfau$1@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
<uemlbj$pl2d$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Sep 2023 18:38:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68142c7755e948501d20eaef3fa69779";
logging-data="965982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/tpg1y3I7pZWAYHNLJaJnWTdywjFu9xH4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:TiAoLYRpE5cqxNR6YTi0X8PPDIc=
Content-Language: en-US
In-Reply-To: <uemlbj$pl2d$1@dont-email.me>
 by: Chris M. Thomasson - Sat, 23 Sep 2023 18:38 UTC

On 9/23/2023 5:23 AM, Bonita Montero wrote:
> Am 23.09.2023 um 13:20 schrieb Kaz Kylheku:
>
>> Not in any way that can report a resource error, while remaining
>> POSIX compliant.
>
> If there's no kernel call while initializing the mutex the slow path
> kernel part has to be initialized on contention. So if the initiali-
> zation is noexcept, the synchronization needs to be capable to throw
> a system_error at least for the delayed inittialization of this mutex
> part.
> And waiting on the slow path may also fail if there's no space for
> en entry in the wait queue of the semaphore. This may not apply for
> all operating systems, but this is a theoretical possibility the
> standard shout honor.
>

Sigh.

Re: Thread-safe initialization of static objects

<uenbdv$tfau$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 11:39:58 -0700
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uenbdv$tfau$2@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <ue7gkn$g3o5$1@dont-email.me>
<20230917121015.206@kylheku.com> <ue7k2n$gmqv$1@dont-email.me>
<20230917123203.524@kylheku.com> <ue8lst$q25a$2@dont-email.me>
<20230918101705.91@kylheku.com> <uea1rj$1saml$1@dont-email.me>
<hK5OM.30729$Yxl8.9621@fx14.iad> <ueb3f7$2638l$2@dont-email.me>
<ueb9vm$271en$3@dont-email.me> <uebn4f$293m3$2@dont-email.me>
<1GhOM.7399$H0Ge.6907@fx05.iad> <uecac1$2cj86$1@dont-email.me>
<iwpOM.265$Sn81.181@fx08.iad> <uedp93$2orvq$2@dont-email.me>
<k4BOM.2058$TwR4.78@fx46.iad> <ueeret$2upib$1@dont-email.me>
<JCKOM.83797$2ph4.24937@fx14.iad> <uege3r$3bfsj$2@dont-email.me>
<ueggh5$3bq3f$1@dont-email.me> <uegsdo$3dj0r$1@dont-email.me>
<uei1g6$3l13h$2@dont-email.me> <ueiu42$3tima$1@dont-email.me>
<eE7PM.16350$3vM.3605@fx37.iad> <uej5q1$3uji0$1@dont-email.me>
<gWePM.25327$fUu6.21270@fx47.iad> <uek0g4$7fl1$1@dont-email.me>
<jBoPM.25452$fUu6.14009@fx47.iad> <uelq44$lo9j$1@dont-email.me>
<_UBPM.4607$Lmc1.3371@fx44.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Sep 2023 18:39:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68142c7755e948501d20eaef3fa69779";
logging-data="965982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bdoQPv/NhHJU87EaDHg96FKbtWTtXkbM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:8vgooiSd2TTMRJcRk2RC52t+IUg=
In-Reply-To: <_UBPM.4607$Lmc1.3371@fx44.iad>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 23 Sep 2023 18:39 UTC

On 9/23/2023 6:40 AM, Richard Damon wrote:
> On 9/23/23 12:38 AM, Bonita Montero wrote:
>> Am 23.09.2023 um 00:31 schrieb Richard Damon:
>>
>>> The fact that you are too stupid to understand how these things
>>> actually work and give performant results doesn't mean they don't do
>>> it. The fact that they do shows your ignorance.
>>
>> Yielding at this place and spin-locking in userspace is unprofessional.
>>
>
> As is saying you should be able to fail at something that actually can
> be done without failing.

Basically. Yikes!

Re: Thread-safe initialization of static objects

<uenc7c$tfau$5@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 11:53:31 -0700
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <uenc7c$tfau$5@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
<uemlbj$pl2d$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Sep 2023 18:53:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68142c7755e948501d20eaef3fa69779";
logging-data="965982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NXMrCjBxospyl4MoZmfPXCXQqRALHkaA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:MIOSlQFQurqU7swEuOtmJQBj3A4=
In-Reply-To: <uemlbj$pl2d$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 23 Sep 2023 18:53 UTC

On 9/23/2023 5:23 AM, Bonita Montero wrote:
> Am 23.09.2023 um 13:20 schrieb Kaz Kylheku:
>
[...]
> And waiting on the slow path may also fail if there's no space for
> en entry in the wait queue of the semaphore.

Huh? I don't think you know how a wait queue works. A node can be in
per-thread memory, even on its stack. Study up Buddy!

Re: Thread-safe initialization of static objects

<uenel9$ttb5$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 12:35:04 -0700
Organization: A noiseless patient Spider
Lines: 122
Message-ID: <uenel9$ttb5$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <1h7KM.66324$Wk53.59929@fx01.iad>
<udbe19$2rk0p$2@dont-email.me> <2acKM.719164$U3w1.269152@fx09.iad>
<udbjfo$2s7cn$1@dont-email.me> <RllKM.1028889$SuUf.692033@fx14.iad>
<udcubm$325k4$1@dont-email.me> <kKnKM.340818$ens9.154535@fx45.iad>
<XioKM.273960$8_8a.80813@fx48.iad> <udd7q8$33i0n$1@dont-email.me>
<ARpKM.1180716$TPw2.772076@fx17.iad> <udda1r$33t5b$1@dont-email.me>
<V1rKM.216833$QQFb.22297@fx38.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Sep 2023 19:35:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="68142c7755e948501d20eaef3fa69779";
logging-data="980325"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+f8VOlQiZqWBZCcx4FVSzRaRVD0xz2+EI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:Vtri3jWEyowXsCCkc5iNYHIQ04M=
Content-Language: en-US
In-Reply-To: <V1rKM.216833$QQFb.22297@fx38.iad>
 by: Chris M. Thomasson - Sat, 23 Sep 2023 19:35 UTC

On 9/7/2023 2:13 PM, Scott Lurndal wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
>> On 9/7/2023 12:52 PM, Richard Damon wrote:
>>> On 9/7/23 12:20 PM, Chris M. Thomasson wrote:
>>>> On 9/7/2023 11:06 AM, Richard Damon wrote:
>>>>> On 9/7/23 10:27 AM, Scott Lurndal wrote:
>>>>>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>>>>>> Am 07.09.2023 um 16:44 schrieb Pavel:
>>>>>>>
>>>>>>>> How so? Waiters will wait for the time of initialization.
>>>>>>>> The initializing thread will be yielded ...
>>>>>>>
>>>>>>> Initializing is usually much faster than a whole timeslice,
>>>>>>> so yielding would be incacceptabel. That's just a stupid idea.
>>>>>>
>>>>>> So tell us, on which operating systems will there be more than
>>>>>> one thread running when application static objects are
>>>>>> initialized (which happens generally before the application
>>>>>> 'main' function is called, and thus before the application
>>>>>> has a chance to create any threads)?
>>>>>
>>>>> While GLOBAL static objects get initialized before main starts,
>>>>> function local static objects don't get initialized until the first
>>>>> call of the function. These will need some synchronization if the
>>>>> function is called from multiple threads at "the same time".
>>>>
>>>> A simple global hash lock scheme is where we can hash addresses
>>>> directly into a static locking table. The lock table is created
>>>> _before_ any program logic is executed.
>>>>
>>>> https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/wwYQCG2hAwAJ
>>>>
>>>>
>>>>> Also, some global object could start up a thread in its constructor.
>>>>
>>>> YIKES! Shit. I have had to debug other peoples code that did this.
>>>> Many points of errors... One was a rather common peach of a bug. The
>>>> constructor would create a thread that would in turn call into a
>>>> virtual function and start using the object before its constructor was
>>>> completed. A massive race condition, nasty ones!
>>>>
>>>
>>> Yes, such a thread needs to understand that the system isn't fully
>>> configured. And yes, a base class creating a thread needs that thread to
>>> understand that the object isn't fully created yet and do something to
>>> handle that issue (which likely requires some help from the most derived
>>> class).
>>
>> I actually got to a point where I said no creating threads in
>> constructors! I have seen to many problems. Create the object _first_,
>> then expose it to a thread, or create a thread that works with the
>> _fully_ constructed object. The types of race conditions I have had to
>> deal with wrt objects creating threads in constructors have tended to be
>> rather nasty in nature...
>
> The race condition is pretty easy to resolve. Turn thread initialization
> into a two-phase operation.
>
> When we have an object that requires its own thread, it will inherit
> from a base class called c_thread. The constructor for c_thread will
> create the thread which eventually executes the objects 'run' function. The
> pthread_create call starts the thread executing at the static function
> c_thread::run(void *obj), where the argument is a pointer to the object
> itself.
>
> That static function sets the process name (prctl(PR_SET_NAME)) and
> then waits on a pthread_cond_t (thread_initialized) declared as a data member of
> c_thread.
>
> Meanwhile, the derived object constructor runs in the original thread after the c_thread
> constructor returns and initializes the object. Once the object is fully
> initialized (which may happen in the constructor, or may happen
> at some later point in time during program startup), the c_thread::run
> method condition variable (thread_initialized) is signaled and the
> static ::run function transfers control to the derived class virtual ::run
> function.
>
> /**
> * Static pthread start function. Invokes thread specific run virtual
> * function.
> *
> * @param arg The class object pointer passed in from the pthread_create.
> */
> void *
> c_thread::run(void *arg)
> {
> c_thread *tp = (c_thread *)arg;
> int diag;
>
> pthread_mutex_lock(&tp->t_threadlock);
> while (!tp->t_thread_initialized) {
> pthread_cond_wait(&tp->t_ready, &tp->t_threadlock);
> }
> pthread_mutex_unlock(&tp->t_threadlock);
>
> diag = prctl(PR_SET_NAME, tp->t_thread_name, NULL, NULL, NULL, NULL);
> if (diag == -1) {
> tp->t_logger->log("Unable to set thread name: %s\n", strerror(errno));
> }
>
> tp->t_running = true;
> tp->run();
> tp->t_running = false;
>
> return NULL;
> }

Actually, I like to ensure that the "object" is 100% initialized before
any thread is able to acquire a pointer to it. A little proxy object in
C++ tends to work fine for me:

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

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

thread_proxy<T>::m_object is 100% fully initialized before the thread is
created. Simple and works for me. Fair enough?

Re: Thread-safe initialization of static objects

<XbJPM.157838$Hih7.40116@fx11.iad>

  copy mid

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

  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!fx11.iad.POSTED!not-for-mail
Subject: Re: Thread-safe initialization of static objects
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <udoico$1d0tr$1@dont-email.me>
<udotj5$1e8do$2@dont-email.me> <udpbun$1gf8c$1@dont-email.me>
<udqatg$1mar3$1@dont-email.me> <udqbg3$1mdv6$1@dont-email.me>
<udqq8u$1p3q6$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <VnsPM.122464$Hih7.52058@fx11.iad>
<uelqeq$lo9j$4@dont-email.me>
From: pauldont...@removeyourself.dontspam.yahoo (Pavel)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.17
MIME-Version: 1.0
In-Reply-To: <uelqeq$lo9j$4@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 59
Message-ID: <XbJPM.157838$Hih7.40116@fx11.iad>
X-Complaints-To: https://www.astraweb.com/aup
NNTP-Posting-Date: Sat, 23 Sep 2023 21:58:15 UTC
Date: Sat, 23 Sep 2023 17:58:09 -0400
X-Received-Bytes: 4622
 by: Pavel - Sat, 23 Sep 2023 21:58 UTC

Bonita Montero wrote:
> Am 23.09.2023 um 04:50 schrieb Pavel:
>
>> for some mutex it may fail, for some not. C++ standard never says that
>> std::mutex can fail. ...
>
> std::mutex::lock() can throw a system_error.
Wrong. The standard says that lock can throw while discussing general
requirements for all types of mutices (mutex, recursive_mutex,
timed_mutex, recursive_timed_mutex, shared_mutex, and
shared_timed_mutex). Then, when discussing specific mutices, the
standard says that recursive_mutex, recursive_timed_mutex and any of the
shared mutices can throw system_error). About std::mutex the standard
says no such thing.

The only mentioning of an error for std::mutex is in a non-normative
note that explains: "If the
implementation can detect the deadlock, a resource_deadlock_would_occur
error condition might be observed". We knew that already (using
deadlock-detecting mutex is asking for error) -- as well as that no C++
implementer would use a more complex error-throwing deadlock-detecting
mutex given a simpler alternative for static initialization.

In particular, on UNIX, an implementer will use
PTHREAD_MUTEX_INITIALIZER that initializes a non-ERRORCHECK mutex.

On Windows, critical section is essentially a recursive
deadlock-detecting (timed) mutex so it is relatively expensive and can fail.

All it means that, on Windows, to implement static initialization, a C++
implementor has to use other primitives, most conveniently --
InitOnceExecuteOnce -- that cannot fail unless the callback it executes
fails.

>
>> "delayed" or "deferred" initialization of synchronization primitives
>> that can be used to initialize static C++ object is entirely your
>> fantasy. It cannot be used for this purpose, period.
>
> The mutex' constructor doesn't throw, so there must be delayed
> initialization.
Wrong. a mutex can be created during static initialization entirely in
user space (like POSIX mutex on linux is created)

> But as we've seen recently pthread Mutexes also
> allow delayed initialization
"Allow" does not mean C++ static initialization implementer has to use
it. On Linux, mutex_lock tries light-weight user-space initialization
internally so no need to reinvent that wheel for implementers. But of
course on Linux any reasonable implementer will use pthread_once (which
also does not have any delayed iniitialization), rather than a mutex.

> of the slow path and Win32 always
> does the same.
Yes, on Win32 C++ static initialization does not have to use mutex
either. INIT_ONCE can be initialized statically to INIT_ONCE_STATIC_INIT
-- which is all one needs for static initialization (coincidentally, on
Win32 specifically InitOnceInitialize does not fail either but this is
irrelevant to the issue of static C++ initialization).

Re: Thread-safe initialization of static objects

<ueo57s$15ub1$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 19:00:27 -0700
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <ueo57s$15ub1$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udotj5$1e8do$2@dont-email.me>
<udpbun$1gf8c$1@dont-email.me> <udqatg$1mar3$1@dont-email.me>
<udqbg3$1mdv6$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<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> <uegsia$3dj0r$2@dont-email.me>
<fp7PM.104556$GHI6.80009@fx17.iad> <uej631$3uji0$4@dont-email.me>
<VnsPM.122464$Hih7.52058@fx11.iad> <uelqeq$lo9j$4@dont-email.me>
<XbJPM.157838$Hih7.40116@fx11.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 02:00:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4cc3e7dd6bc9b0456f53a6522e03ebcd";
logging-data="1243489"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Fm3wcwVYurArVNrgDlqb+7FPBWEFrUU4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:dd1gqHs3DFWdB/MfNc9TSNGOj/0=
In-Reply-To: <XbJPM.157838$Hih7.40116@fx11.iad>
Content-Language: en-US
 by: Chris M. Thomasson - Sun, 24 Sep 2023 02:00 UTC

On 9/23/2023 2:58 PM, Pavel wrote:
> Bonita Montero wrote:
>> Am 23.09.2023 um 04:50 schrieb Pavel:
>>
>>> for some mutex it may fail, for some not. C++ standard never says
>>> that std::mutex can fail. ...
>>
>> std::mutex::lock() can throw a system_error.
> Wrong. The standard says that lock can throw while discussing general
> requirements for all types of mutices (mutex, recursive_mutex,
> timed_mutex, recursive_timed_mutex, shared_mutex, and
> shared_timed_mutex). Then, when discussing specific mutices, the
> standard says that recursive_mutex, recursive_timed_mutex and any of the
> shared mutices can throw system_error). About std::mutex the standard
> says no such thing.
>
> The only mentioning of an error for std::mutex is in a non-normative
> note that explains: "If the
> implementation can detect the deadlock, a resource_deadlock_would_occur
> error condition might be observed". We knew that already (using
> deadlock-detecting mutex is asking for error) -- as well as that no C++
> implementer would use a more complex error-throwing deadlock-detecting
> mutex given a simpler alternative for static initialization.
>
> In particular, on UNIX, an implementer will use
> PTHREAD_MUTEX_INITIALIZER that initializes a non-ERRORCHECK mutex.
>
> On Windows, critical section is essentially a recursive
> deadlock-detecting (timed) mutex so it is relatively expensive and can
> fail.
[...]

I don't think there is a timed wait for a windows critical section. I
know it has a try, but I cannot remember a timed wait...

Re: Thread-safe initialization of static objects

<ueo5ea$162e7$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 19:03:52 -0700
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <ueo5ea$162e7$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udotj5$1e8do$2@dont-email.me>
<udpbun$1gf8c$1@dont-email.me> <udqatg$1mar3$1@dont-email.me>
<udqbg3$1mdv6$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<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> <uegsia$3dj0r$2@dont-email.me>
<fp7PM.104556$GHI6.80009@fx17.iad> <uej631$3uji0$4@dont-email.me>
<VnsPM.122464$Hih7.52058@fx11.iad> <uelqeq$lo9j$4@dont-email.me>
<XbJPM.157838$Hih7.40116@fx11.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 24 Sep 2023 02:03:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4cc3e7dd6bc9b0456f53a6522e03ebcd";
logging-data="1247687"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/JT+Fe3BotnGQUfV7CTftqXFh6xLuRjRw="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:KrzRpPgta6xIWVufDG84nnp4TsI=
Content-Language: en-US
In-Reply-To: <XbJPM.157838$Hih7.40116@fx11.iad>
 by: Chris M. Thomasson - Sun, 24 Sep 2023 02:03 UTC

On 9/23/2023 2:58 PM, Pavel wrote:
> Bonita Montero wrote:
>> Am 23.09.2023 um 04:50 schrieb Pavel:
>>
>>> for some mutex it may fail, for some not. C++ standard never says
>>> that std::mutex can fail. ...
>>
>> std::mutex::lock() can throw a system_error.
> Wrong. The standard says that lock can throw while discussing general
> requirements for all types of mutices (mutex, recursive_mutex,
> timed_mutex, recursive_timed_mutex, shared_mutex, and
> shared_timed_mutex). Then, when discussing specific mutices, the
> standard says that recursive_mutex, recursive_timed_mutex and any of the
> shared mutices can throw system_error). About std::mutex the standard
> says no such thing.
>
> The only mentioning of an error for std::mutex is in a non-normative
> note that explains: "If the
> implementation can detect the deadlock, a resource_deadlock_would_occur
> error condition might be observed". We knew that already (using
> deadlock-detecting mutex is asking for error) -- as well as that no C++
> implementer would use a more complex error-throwing deadlock-detecting
> mutex given a simpler alternative for static initialization.
>
> In particular, on UNIX, an implementer will use
> PTHREAD_MUTEX_INITIALIZER that initializes a non-ERRORCHECK mutex.
>
> On Windows, critical section is essentially a recursive
> deadlock-detecting (timed) mutex so it is relatively expensive and can
> fail.
>
> All it means that, on Windows, to implement static initialization, a C++
> implementor has to use other primitives, most conveniently --
> InitOnceExecuteOnce -- that cannot fail unless the callback it executes
> fails.
>
>>
>>> "delayed" or "deferred" initialization of synchronization primitives
>>> that can be used to initialize static C++ object is entirely your
>>> fantasy. It cannot be used for this purpose, period.
>>
>> The mutex' constructor doesn't throw, so there must be delayed
>> initialization.
> Wrong. a mutex can be created during static initialization entirely in
> user space (like POSIX mutex on linux is created)
>
>> But as we've seen recently pthread Mutexes also
>> allow delayed initialization
> "Allow" does not mean C++ static initialization implementer has to use
> it. On Linux, mutex_lock tries light-weight user-space initialization
> internally so no need to reinvent that wheel for implementers. But of
> course on Linux any reasonable implementer will use pthread_once (which
> also does not have any delayed iniitialization), rather than a mutex.
>
>>  of the slow path and Win32 always
>> does the same.
> Yes, on Win32 C++ static initialization does not have to use mutex
> either. INIT_ONCE can be initialized statically to INIT_ONCE_STATIC_INIT
> -- which is all one needs for static initialization (coincidentally, on
> Win32 specifically InitOnceInitialize does not fail either but this is
> irrelevant to the issue of static C++ initialization).

Iirc, I think a windows critical section might raise an error if its
been waiting for 30 days or something, deadlocked.

Re: Thread-safe initialization of static objects

<ueo5ps$163k0$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sun, 24 Sep 2023 04:10:07 +0200
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <ueo5ps$163k0$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udotj5$1e8do$2@dont-email.me>
<udpbun$1gf8c$1@dont-email.me> <udqatg$1mar3$1@dont-email.me>
<udqbg3$1mdv6$1@dont-email.me> <udqq8u$1p3q6$1@dont-email.me>
<uds0qa$22qfj$1@dont-email.me> <udtg9s$2aqb5$1@dont-email.me>
<uduimg$2imqi$1@dont-email.me> <FUEMM.7469$ZkX3.6739@fx09.iad>
<ue3bhh$3l4s1$1@dont-email.me> <ue3nga$3ms5s$1@dont-email.me>
<eohNM.4832$3lL1.1360@fx47.iad> <ue4cqe$3rjfr$1@dont-email.me>
<LwlNM.50924$QShe.20423@fx11.iad> <ue4p5v$3truj$1@dont-email.me>
<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> <uegsia$3dj0r$2@dont-email.me>
<fp7PM.104556$GHI6.80009@fx17.iad> <uej631$3uji0$4@dont-email.me>
<VnsPM.122464$Hih7.52058@fx11.iad> <uelqeq$lo9j$4@dont-email.me>
<XbJPM.157838$Hih7.40116@fx11.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 02:10:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8eb27cf8260b077e260d32247752bc17";
logging-data="1248896"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ETSxp/A5w0RrfKfDpfR87Oe7YHj+G9Eg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:51KCVts6LC5reSxrZtk/AG39e8w=
In-Reply-To: <XbJPM.157838$Hih7.40116@fx11.iad>
Content-Language: de-DE
 by: Bonita Montero - Sun, 24 Sep 2023 02:10 UTC

Am 23.09.2023 um 23:58 schrieb Pavel:

>> std::mutex::lock() can throw a system_error.

> Wrong. The standard says that lock can throw while discussing general
> requirements for all types of mutices (mutex, recursive_mutex,
> timed_mutex, recursive_timed_mutex, shared_mutex, and
> shared_timed_mutex). Then, when discussing specific mutices, the
> standard says that recursive_mutex, recursive_timed_mutex and any of the
> shared mutices can throw system_error). About std::mutex the standard
> says no such thing.

constexpr mutex() noexcept;

https://en.cppreference.com/w/cpp/thread/mutex/mutex

> Wrong. a mutex can be created during static initialization entirely
> in user space (like POSIX mutex on linux is created)

If the mutex' constructor is noexcept the kernel-part for the slow
path has to be initialized delayed while locking.

Rest unread.

Re: Thread-safe initialization of static objects

<ueo6av$166mm$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 19:19:09 -0700
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <ueo6av$166mm$1@dont-email.me>
References: <udafjf$2joeq$1@dont-email.me> <udpbun$1gf8c$1@dont-email.me>
<udqatg$1mar3$1@dont-email.me> <udqbg3$1mdv6$1@dont-email.me>
<udqq8u$1p3q6$1@dont-email.me> <uds0qa$22qfj$1@dont-email.me>
<udtg9s$2aqb5$1@dont-email.me> <uduimg$2imqi$1@dont-email.me>
<FUEMM.7469$ZkX3.6739@fx09.iad> <ue3bhh$3l4s1$1@dont-email.me>
<ue3nga$3ms5s$1@dont-email.me> <eohNM.4832$3lL1.1360@fx47.iad>
<ue4cqe$3rjfr$1@dont-email.me> <LwlNM.50924$QShe.20423@fx11.iad>
<ue4p5v$3truj$1@dont-email.me> <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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <VnsPM.122464$Hih7.52058@fx11.iad>
<uelqeq$lo9j$4@dont-email.me> <XbJPM.157838$Hih7.40116@fx11.iad>
<ueo5ps$163k0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 02:19:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4cc3e7dd6bc9b0456f53a6522e03ebcd";
logging-data="1252054"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/A7rUZ9LTPAN9iAj+BOyH+k/EwrlOmYMc="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:0EqnKWAA107tpTKXxXbeMh6a8/8=
Content-Language: en-US
In-Reply-To: <ueo5ps$163k0$1@dont-email.me>
 by: Chris M. Thomasson - Sun, 24 Sep 2023 02:19 UTC

On 9/23/2023 7:10 PM, Bonita Montero wrote:
> Am 23.09.2023 um 23:58 schrieb Pavel:
>
>>> std::mutex::lock() can throw a system_error.
>
>> Wrong. The standard says that lock can throw while discussing general
>> requirements for all types of mutices (mutex, recursive_mutex,
>> timed_mutex, recursive_timed_mutex, shared_mutex, and
>> shared_timed_mutex). Then, when discussing specific mutices, the
>> standard says that recursive_mutex, recursive_timed_mutex and any of
>> the shared mutices can throw system_error). About std::mutex the
>> standard says no such thing.
>
> constexpr mutex() noexcept;
>
> https://en.cppreference.com/w/cpp/thread/mutex/mutex
>
>> Wrong. a mutex can be created during static initialization entirely
>> in user space (like POSIX mutex on linux is created)
>
> If the mutex' constructor is noexcept the kernel-part for the slow
> path has to be initialized delayed while locking.

Huh?

>
> Rest unread.
>

Figured.

Re: Thread-safe initialization of static objects

<20230923185708.811@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sun, 24 Sep 2023 02:22:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <20230923185708.811@kylheku.com>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
<uemlbj$pl2d$1@dont-email.me>
Injection-Date: Sun, 24 Sep 2023 02:22:38 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de7138e0de32fc51e80c05ed2d00fab7";
logging-data="1253480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/ffl4ykdbMLOR22wA/HQpN4myK/1d7Zk="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:04tYQd95Y8p/WrfcrKMW6j2jdtE=
 by: Kaz Kylheku - Sun, 24 Sep 2023 02:22 UTC

On 2023-09-23, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 23.09.2023 um 13:20 schrieb Kaz Kylheku:
>
>> Not in any way that can report a resource error, while remaining
>> POSIX compliant.
>
> If there's no kernel call while initializing the mutex the slow path
> kernel part has to be initialized on contention. So if the initiali-
> zation is noexcept, the synchronization needs to be capable to throw
> a system_error at least for the delayed inittialization of this mutex
> part.
> And waiting on the slow path may also fail if there's no space for
> en entry in the wait queue of the semaphore. This may not apply for
> all operating systems, but this is a theoretical possibility the
> standard shout honor.

The thing is, mutexes don't require a kernel object per.
A possible design is that each *thread* has a wait semaphore. If a
thread is created, it has a semaphore;the resource check can be done at
thread creation time. If we cannot create that important semaphore, the
thread doesn't get created; thread creation fails.

When a thread waits on mutex, it puts itself into the mutex's wait
queue, and then waits on its own semaphore. It uses that semaphore
for all mutexes.

When the mutex is unlocked, one (or maybe more) threads waiting on it
are dequeued and woken by signaling their individual semaphore.

These semaphores can be hidden under operations suspend() and
resume(thread). suspend() finds the thread's self structure, and
blocks on self->suspend_sem. resume(thread) signals the
thread->suspend_sem.

Roughly speaking.

You need a lower level locking mechanism to do the queuing.

Inside a kernel, you'd never bother with a semaphore per lock,
because you just put the thread into a wait queue, change it to
a blocked state, and call the scheduler.

Those mechanisms could be used to *build* a semaphore. Or barrier,

(The semaphore-per-thread in user space simulates that: using suspend()
and resume(thread) is like scheduling. suspend() deschedules this
thread; resume(thread) chooses a thread to be runnable.)
condition variable, etc.

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

<ueo8m4$16kh3$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 19:59:12 -0700
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <ueo8m4$16kh3$1@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
<uemlbj$pl2d$1@dont-email.me> <20230923185708.811@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 02:59:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4cc3e7dd6bc9b0456f53a6522e03ebcd";
logging-data="1266211"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+sLe2v3SwCKB84e6BGyS80N8FsZdY4cI="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:eIwgaMPK6iLDTE2eTsehLczSLqA=
Content-Language: en-US
In-Reply-To: <20230923185708.811@kylheku.com>
 by: Chris M. Thomasson - Sun, 24 Sep 2023 02:59 UTC

On 9/23/2023 7:22 PM, Kaz Kylheku wrote:
> On 2023-09-23, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 23.09.2023 um 13:20 schrieb Kaz Kylheku:
>>
>>> Not in any way that can report a resource error, while remaining
>>> POSIX compliant.
>>
>> If there's no kernel call while initializing the mutex the slow path
>> kernel part has to be initialized on contention. So if the initiali-
>> zation is noexcept, the synchronization needs to be capable to throw
>> a system_error at least for the delayed inittialization of this mutex
>> part.
>> And waiting on the slow path may also fail if there's no space for
>> en entry in the wait queue of the semaphore. This may not apply for
>> all operating systems, but this is a theoretical possibility the
>> standard shout honor.
>
> The thing is, mutexes don't require a kernel object per.
>
> A possible design is that each *thread* has a wait semaphore. If a
> thread is created, it has a semaphore;the resource check can be done at
> thread creation time. If we cannot create that important semaphore, the
> thread doesn't get created; thread creation fails.
>
> When a thread waits on mutex, it puts itself into the mutex's wait
> queue, and then waits on its own semaphore. It uses that semaphore
> for all mutexes.
>
> When the mutex is unlocked, one (or maybe more) threads waiting on it
> are dequeued and woken by signaling their individual semaphore.
>
> These semaphores can be hidden under operations suspend() and
> resume(thread). suspend() finds the thread's self structure, and
> blocks on self->suspend_sem. resume(thread) signals the
> thread->suspend_sem.
>
> Roughly speaking.
>
> You need a lower level locking mechanism to do the queuing.
>
> Inside a kernel, you'd never bother with a semaphore per lock,
> because you just put the thread into a wait queue, change it to
> a blocked state, and call the scheduler.
>
> Those mechanisms could be used to *build* a semaphore. Or barrier,
>
> (The semaphore-per-thread in user space simulates that: using suspend()
> and resume(thread) is like scheduling. suspend() deschedules this
> thread; resume(thread) chooses a thread to be runnable.)
> condition variable, etc.
>

That's about it. The only problem wrt emulating it in user space is that
it basically takes the scheduling ability away from the OS.

struct per_thread
{ per_thread* m_next;
os_semaphore m_sema;
};

So, to wake a thread on the unlock of a mutex or something, wrt the wait
queue it would just pop a node and post to its semaphore. It did not
"necessarily" have to search itself for a specific thread... We can use
some indirection:

struct per_thread_node
{ per_thread_wait_node* m_next;
per_thread* m_thread;
};

And use per_thread_node instead of using per_thread directly to link
them into a wait queue.

Re: Thread-safe initialization of static objects

<ueo915$16kh3$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 20:05:08 -0700
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <ueo915$16kh3$2@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <IsoPM.20139$wO91.15127@fx39.iad>
<uelqb1$lo9j$3@dont-email.me> <24DPM.126389$Hih7.28245@fx11.iad>
<uemvt2$rcgo$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 03:05:09 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4cc3e7dd6bc9b0456f53a6522e03ebcd";
logging-data="1266211"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NIFO6DXoyW1Q6z29iQYHmmu6XkEd/Na8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:tIrfukdos5PfirYkAnhW5CjGjUg=
In-Reply-To: <uemvt2$rcgo$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sun, 24 Sep 2023 03:05 UTC

On 9/23/2023 8:23 AM, Bonita Montero wrote:
> Am 23.09.2023 um 17:00 schrieb Scott Lurndal:
>
>> We're talking specifically about the implementation mechanism
>> used to serialize static initialization of a function-scope
>> static object.
>
> I've noticed that a Pthread mutex can be statically initialized and
> Win32's CRITICAL_SECTION never fails on initialization either. But
> this mean that the semaphore for the slow path on contention has to
> be initialized delayed. So if there's contention the sychronization
> can still fail.
> And even if the semaphore creation doesn't fail the synchronization
> on it may fail, depending on the implementation inside the kernel,
> f.e. if there's no memory for an additional entry in the wait-queue
> on the semaphore.
> I think it's ridiculous to say "look at operating system X, it would
> never fail" since that's not the way the standard should be designed.
> As I've shown this may be true for the synchronization part only but
> for sure not for the creation of the binary semaphore.
> If std::mutex may fail on synchronization and implementations have
> to create the kernel part of the "mutexes" for static initialization
> on demand, static initialization should be definedto throw a
> system_error.
> The probability of this happening is very low, simply because the
> probability of contention at this point is usually low and creating
> a semaphore may fail under rare conditions, but what happens then
> needs to be clearly defined for me. std::mutex has defined means
> to handle that, static initialization not.
>
>

Why do you think that static initialization must use a std::mutex
anyway? Why do you think that std::mutex must use some delayed kernel
resource initialization? Strange.

Re: Thread-safe initialization of static objects

<ueo98b$16kh3$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sat, 23 Sep 2023 20:08:57 -0700
Organization: A noiseless patient Spider
Lines: 85
Message-ID: <ueo98b$16kh3$3@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
<uemlbj$pl2d$1@dont-email.me> <20230923185708.811@kylheku.com>
<ueo8m4$16kh3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 24 Sep 2023 03:08:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="4cc3e7dd6bc9b0456f53a6522e03ebcd";
logging-data="1266211"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CLR6PBQP74irDBHkfIi1xXrPe5CwJzO8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:IjzUhcq7wIrSDEMrr+lw5n37nH4=
Content-Language: en-US
In-Reply-To: <ueo8m4$16kh3$1@dont-email.me>
 by: Chris M. Thomasson - Sun, 24 Sep 2023 03:08 UTC

On 9/23/2023 7:59 PM, Chris M. Thomasson wrote:
> On 9/23/2023 7:22 PM, Kaz Kylheku wrote:
>> On 2023-09-23, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>> Am 23.09.2023 um 13:20 schrieb Kaz Kylheku:
>>>
>>>> Not in any way that can report a resource error, while remaining
>>>> POSIX compliant.
>>>
>>> If there's no kernel call while initializing the mutex the slow path
>>> kernel part has to be initialized on contention. So if the initiali-
>>> zation is noexcept, the synchronization needs to be capable to throw
>>> a system_error at least for the delayed inittialization of this mutex
>>> part.
>>> And waiting on the slow path may also fail if there's no space for
>>> en entry in the wait queue of the semaphore. This may not apply for
>>> all operating systems, but this is a theoretical possibility the
>>> standard shout honor.
>>
>> The thing is, mutexes don't require a kernel object per.
>> A possible design is that each *thread* has a wait semaphore. If a
>> thread is created, it has a semaphore;the resource check can be done at
>> thread creation time. If we cannot create that important semaphore, the
>> thread doesn't get created; thread creation fails.
>>
>> When a thread waits on mutex, it puts itself into the mutex's wait
>> queue, and then waits on its own semaphore. It uses that semaphore
>> for all mutexes.
>>
>> When the mutex is unlocked, one (or maybe more) threads waiting on it
>> are dequeued and woken by signaling their individual semaphore.
>>
>> These semaphores can be hidden under operations suspend() and
>> resume(thread). suspend() finds the thread's self structure, and
>> blocks on self->suspend_sem.  resume(thread) signals the
>> thread->suspend_sem.
>>
>> Roughly speaking.
>>
>> You need a lower level locking mechanism to do the queuing.
>>
>> Inside a kernel, you'd never bother with a semaphore per lock,
>> because you just put the thread into a wait queue, change it to
>> a blocked state, and call the scheduler.
>>
>> Those mechanisms could be used to *build* a semaphore. Or barrier,
>>
>> (The semaphore-per-thread in user space simulates that: using suspend()
>> and resume(thread) is like scheduling. suspend() deschedules this
>> thread; resume(thread) chooses a thread to be runnable.)
>> condition variable, etc.
>>
>
> That's about it. The only problem wrt emulating it in user space is that
> it basically takes the scheduling ability away from the OS.
>
> struct per_thread
> {
>     per_thread* m_next;
>     os_semaphore m_sema;
> };
>
> So, to wake a thread on the unlock of a mutex or something, wrt the wait
> queue it would just pop a node and post to its semaphore. It did not
> "necessarily" have to search itself for a specific thread... We can use
> some indirection:
>
> struct per_thread_node
> {
>     per_thread_wait_node* m_next;
^^^^^^^^^^^^^^^^^^^^

That should be:

per_thread_node* m_next;

>     per_thread* m_thread;
> };

Damn typos!

>
>
> And use per_thread_node instead of using per_thread directly to link
> them into a wait queue.

Re: Thread-safe initialization of static objects

<ueoqpa$19436$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sun, 24 Sep 2023 10:08:13 +0200
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <ueoqpa$19436$1@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
<uemlbj$pl2d$1@dont-email.me> <20230923185708.811@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 08:08:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8eb27cf8260b077e260d32247752bc17";
logging-data="1347686"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bNJsVuLiotMxQLZLMC8tmwXxP5oRqWkw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:7SHqy2giVaieSF+m5gve9dlupZU=
In-Reply-To: <20230923185708.811@kylheku.com>
Content-Language: de-DE
 by: Bonita Montero - Sun, 24 Sep 2023 08:08 UTC

Am 24.09.2023 um 04:22 schrieb Kaz Kylheku:

> A possible design is that each *thread* has a wait semaphore.

You'd have to register the binary semaphore and register as a waiter in
one atomic step. You could do that with a DCAS'd ring, but you coudn't
atomically update the link pointers of the other nodes at the same
time you update the roots link pointers. And you would have the deallo-
cation poblem, i.e. the memory of your object may never be deallocated.
Show me your implementation !
It's ridiculous because mutex are a cheap resource. And if such imple-
mentation would be possible the standard shouldn't mandate it since
not every platform has a DCAS.

Re: Thread-safe initialization of static objects

<ueoqra$19436$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sun, 24 Sep 2023 10:09:17 +0200
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <ueoqra$19436$2@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <IsoPM.20139$wO91.15127@fx39.iad>
<uelqb1$lo9j$3@dont-email.me> <24DPM.126389$Hih7.28245@fx11.iad>
<uemvt2$rcgo$1@dont-email.me> <ueo915$16kh3$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 08:09:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8eb27cf8260b077e260d32247752bc17";
logging-data="1347686"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/enD0v6k8Vq1t7RBYlAKvSirzxwtBmjws="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qgjr5Y9Ht4qxsRQVSYx5lZEuDjo=
In-Reply-To: <ueo915$16kh3$2@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sun, 24 Sep 2023 08:09 UTC

Am 24.09.2023 um 05:05 schrieb Chris M. Thomasson:

> Why do you think that static initialization must use a std::mutex
> anyway? ...

Call it std::mutex or whatever. It is mandated that contenders
are sleeping - that's not possible without sth. else than a mutex.

Re: Thread-safe initialization of static objects

<37dbe3b6-ead0-487b-884c-197124888eben@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ac8:5796:0:b0:403:b6b0:bdb5 with SMTP id v22-20020ac85796000000b00403b6b0bdb5mr31121qta.7.1695549019907;
Sun, 24 Sep 2023 02:50:19 -0700 (PDT)
X-Received: by 2002:a05:6820:221b:b0:571:9ccb:f90a with SMTP id
cj27-20020a056820221b00b005719ccbf90amr3120394oob.1.1695549019593; Sun, 24
Sep 2023 02:50:19 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Sun, 24 Sep 2023 02:50:19 -0700 (PDT)
In-Reply-To: <ueoqra$19436$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:859c:9294:d188:1b63;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:859c:9294:d188:1b63
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <IsoPM.20139$wO91.15127@fx39.iad>
<uelqb1$lo9j$3@dont-email.me> <24DPM.126389$Hih7.28245@fx11.iad>
<uemvt2$rcgo$1@dont-email.me> <ueo915$16kh3$2@dont-email.me> <ueoqra$19436$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <37dbe3b6-ead0-487b-884c-197124888eben@googlegroups.com>
Subject: Re: Thread-safe initialization of static objects
From: already5...@yahoo.com (Michael S)
Injection-Date: Sun, 24 Sep 2023 09:50:19 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Michael S - Sun, 24 Sep 2023 09:50 UTC

On Sunday, September 24, 2023 at 11:09:31 AM UTC+3, Bonita Montero wrote:
> Am 24.09.2023 um 05:05 schrieb Chris M. Thomasson:
>
> > Why do you think that static initialization must use a std::mutex
> > anyway? ...
>
> Call it std::mutex or whatever. It is mandated that contenders
> are sleeping - that's not possible without sth. else than a mutex.

Few dozens posts above I demonstrated an implementation
for pre-Vista Windows where contenders are sleeping just fine and
waking just fine and initialization of different objects proceeds
simultaneously just fine. And it does not use mutex for that.
It uses SleepEx() for wait and QueueUserApc() for wakeup.
It uses one global mutex (in form of critical section) for a short
while for metadata updates, but that's done for simplicity and
practicality rather than out of necessity. I am pretty sure that
with enough effort I can device a variant that achieves the same
with CaS.

Re: Thread-safe initialization of static objects

<uep1kp$1a921$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: Thread-safe initialization of static objects
Date: Sun, 24 Sep 2023 12:05:16 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uep1kp$1a921$1@dont-email.me>
References: <udafjf$2joeq$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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <IsoPM.20139$wO91.15127@fx39.iad>
<uelqb1$lo9j$3@dont-email.me> <24DPM.126389$Hih7.28245@fx11.iad>
<uemvt2$rcgo$1@dont-email.me> <ueo915$16kh3$2@dont-email.me>
<ueoqra$19436$2@dont-email.me>
<37dbe3b6-ead0-487b-884c-197124888eben@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 10:05:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8eb27cf8260b077e260d32247752bc17";
logging-data="1385537"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GSsXFyITvaO/P7h3HPK7geHaDOA2rtIo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:TQzTBinZ9tR01YkxkCJG+66h4Lk=
In-Reply-To: <37dbe3b6-ead0-487b-884c-197124888eben@googlegroups.com>
Content-Language: de-DE
 by: Bonita Montero - Sun, 24 Sep 2023 10:05 UTC

Am 24.09.2023 um 11:50 schrieb Michael S:

> ...And it does not use mutex for that.
> It uses SleepEx() for wait and QueueUserApc() for wakeup.

That's also a kind of semaphore.

Re: Thread-safe initialization of static objects

<MTUPM.158223$Hih7.125749@fx11.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!newsfeed.hasname.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx11.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> <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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <20230922111441.730@kylheku.com>
<uelq9h$lo9j$2@dont-email.me> <20230923041951.728@kylheku.com>
<uemlbj$pl2d$1@dont-email.me> <20230923185708.811@kylheku.com>
<ueoqpa$19436$1@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
Content-Language: en-US
In-Reply-To: <ueoqpa$19436$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 28
Message-ID: <MTUPM.158223$Hih7.125749@fx11.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: Sun, 24 Sep 2023 07:15:56 -0400
X-Received-Bytes: 2728
 by: Richard Damon - Sun, 24 Sep 2023 11:15 UTC

On 9/24/23 4:08 AM, Bonita Montero wrote:
> Am 24.09.2023 um 04:22 schrieb Kaz Kylheku:
>
>> A possible design is that each *thread* has a wait semaphore.
>
> You'd have to register the binary semaphore and register as a waiter in
> one atomic step. You could do that with a DCAS'd ring, but you coudn't
> atomically update the link pointers of the other nodes at the same
> time you update the roots link pointers. And you would have the deallo-
> cation poblem, i.e. the memory of your object may never be deallocated.
> Show me your implementation !
> It's ridiculous because mutex are a cheap resource. And if such imple-
> mentation would be possible the standard shouldn't mandate it since
> not every platform has a DCAS.
>

So, you still think that the Standard should allow a very common
operation to fail in ways that the program can not handle it when
another method exists to do the operation exists that has no opportunity
for failure.

You just don't understand programming.

Something that is marginally faster, but introduces chance of failure,
is NOT an improvement or valid optimization.

The fact that YOU don't understand how to do a standard operation,
doesn't mean it isn't a good way to do it.

Re: Thread-safe initialization of static objects

<YVUPM.158224$Hih7.22856@fx11.iad>

  copy mid

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

  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!fx11.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Thread-safe initialization of static objects
Content-Language: en-US
Newsgroups: comp.lang.c++
References: <udafjf$2joeq$1@dont-email.me> <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>
<uegsia$3dj0r$2@dont-email.me> <fp7PM.104556$GHI6.80009@fx17.iad>
<uej631$3uji0$4@dont-email.me> <mzkPM.16155$ugs.3977@fx36.iad>
<uekler$bqj8$1@dont-email.me> <IsoPM.20139$wO91.15127@fx39.iad>
<uelqb1$lo9j$3@dont-email.me> <24DPM.126389$Hih7.28245@fx11.iad>
<uemvt2$rcgo$1@dont-email.me> <ueo915$16kh3$2@dont-email.me>
<ueoqra$19436$2@dont-email.me>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <ueoqra$19436$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 17
Message-ID: <YVUPM.158224$Hih7.22856@fx11.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: Sun, 24 Sep 2023 07:18:16 -0400
X-Received-Bytes: 1928
 by: Richard Damon - Sun, 24 Sep 2023 11:18 UTC

On 9/24/23 4:09 AM, Bonita Montero wrote:
> Am 24.09.2023 um 05:05 schrieb Chris M. Thomasson:
>
>> Why do you think that static initialization must use a std::mutex
>> anyway? ...
>
> Call it std::mutex or whatever. It is mandated that contenders
> are sleeping - that's not possible without sth. else than a mutex.
>
>

Where is it mandated that they "Sleep"?

What is mandated is that initialization occur just once and no code can
access the object (if properly accessed) before its initialization.

I think you don't understand the meaning of the Standard.


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

Pages:123456789101112131415161718192021222324252627
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor