Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

There's got to be more to life than compile-and-go.


devel / comp.lang.c++ / Re: 10 ^ N

SubjectAuthor
* 10 ^ NBonita Montero
+* Re: 10 ^ NPaavo Helde
|+- Re: 10 ^ NBonita Montero
|`- Re: 10 ^ NBonita Montero
+* Re: 10 ^ NChris M. Thomasson
|+- Re: 10 ^ NChris M. Thomasson
|`* Re: 10 ^ NBonita Montero
| +* Re: 10 ^ NChris M. Thomasson
| |`* Re: 10 ^ NChris M. Thomasson
| | `- Re: 10 ^ NChris M. Thomasson
| `* Re: 10 ^ NChris M. Thomasson
|  `* Re: 10 ^ NChris M. Thomasson
|   `- Re: 10 ^ NChris M. Thomasson
+* Re: 10 ^ NChris M. Thomasson
|`* Re: 10 ^ NChris M. Thomasson
| `* Re: 10 ^ NChris M. Thomasson
|  `- Re: 10 ^ NChris M. Thomasson
`- Re: 10 ^ NChris M. Thomasson

1
10 ^ N

<uv8955$1jss1$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: 10 ^ N
Date: Thu, 11 Apr 2024 11:05:44 +0200
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 11 Apr 2024 11:05:41 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="60f5ab8b13164cf87238791110f9e482";
logging-data="1700737"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190UqUFH/vVV3qH/cUoVIgwqTB6uHRQf7o="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:fuJRAKRYI5CSaleIML5OC7y+NDo=
Content-Language: de-DE
 by: Bonita Montero - Thu, 11 Apr 2024 09:05 UTC

I tried to use futexes for my pow10-function.
Is the usage correct ?

#include <bit>
#include <atomic>
#include <array>
#include <mutex>
#include "xmath.h"

using namespace std;

double pow10( int64_t exp )
{ constexpr uint64_t EXP_MASK = 0x7FFull << 52;
// table for binary exponentation with 10 ^ (2 ^ N)
static array<double, 64> tenPows;
// table initialized ?
if( static atomic_bool once( false ); !once.load( memory_order_acquire ) )
{
once.wait( false, memory_order_acquire );
if( !once.load( memory_order_relaxed ) )
{
// no: calculate table
for( double p10x2xN = 10.0; double &pow : tenPows )
pow = p10x2xN,
p10x2xN *= p10x2xN;
// set initialized flag with release semantics
once.store( true, memory_order_release );
once.notify_all();
}
}
// begin with 1.0 since x ^ 0 = 1
double result = 1.0;
// unsigned exponent
uint64_t uExp = exp >= 0 ? exp : -exp;
// highest set bit of exponent
size_t bit = 63 - countl_zero( uExp );
// bit mask to highest set bit
uint64_t mask = 1ull << bit;
// loop as long as there are bits in unsigned exponent
for( ; uExp; uExp &= ~mask, mask >>= 1, --bit )
// bit set ?
if( uExp & mask )
{
// yes: multiply result by 10 ^ (bit + 1)
result *= tenPows[bit];
// overlow ?
if( (bit_cast<uint64_t>( result ) & EXP_MASK) == EXP_MASK )
// yes: result wouldn't change furhter; stop
break;
}
// return 1 / result if exponent is negative
return exp >= 0 ? result : 1.0 / result;
};

Re: 10 ^ N

<uv8m86$1mkmt$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: eesnimi@osa.pri.ee (Paavo Helde)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Thu, 11 Apr 2024 15:49:10 +0300
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uv8m86$1mkmt$1@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 11 Apr 2024 14:49:10 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="3af0dab980a8d85e0a3db0bdf2631539";
logging-data="1790685"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/OXl7gmFmm8vWAw2myTBgLC3zLQR+qyiM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:r4rluw8ewYc5rvdtVzC4sBGi+4s=
Content-Language: en-US
In-Reply-To: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
 by: Paavo Helde - Thu, 11 Apr 2024 12:49 UTC

11.04.2024 12:05 Bonita Montero kirjutas:
> I tried to use futexes for my pow10-function.
> Is the usage correct ?

Maybe, but it's not needed and makes the code more complex, redundant
and potentially a bit slower.

C++ statics' initialization is guaranteed to be thread-safe since C++11.
Just put your initialization logic into a separate function and use this
for initializing your static variable. C++ will perform any needed
multi-thread locking automatically, adding ones own explicit locks will
not make it faster.

Re: 10 ^ N

<uv8nae$1mrrk$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Thu, 11 Apr 2024 15:07:30 +0200
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <uv8nae$1mrrk$1@raubtier-asyl.eternal-september.org>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uv8m86$1mkmt$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 11 Apr 2024 15:07:27 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="60f5ab8b13164cf87238791110f9e482";
logging-data="1798004"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19f1yUN6uutjLrWAKbqMP9hfDaSuNFIP5k="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:UGVRFA5u+y+KVLzTvV/IDngM8C8=
In-Reply-To: <uv8m86$1mkmt$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 11 Apr 2024 13:07 UTC

Am 11.04.2024 um 14:49 schrieb Paavo Helde:

> Maybe, but it's not needed and makes the code more complex,
> redundant and potentially a bit slower.

I asked if the code uses double-checked locking correct since
I've implemented it with a mutex so far and not with a futex
and not if the code is hard to read for some people.

Re: 10 ^ N

<uvc3r6$2hlo3$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Fri, 12 Apr 2024 21:59:39 +0200
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <uvc3r6$2hlo3$1@raubtier-asyl.eternal-september.org>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uv8m86$1mkmt$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 12 Apr 2024 21:59:35 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="23207d468f914230250afc62bac35bc8";
logging-data="2676483"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Zm82PC8OsayFtcWcACLY/2bqvT17D9yM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Oi6XY2evWMrmhy55RVhUFGjz0rE=
In-Reply-To: <uv8m86$1mkmt$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Fri, 12 Apr 2024 19:59 UTC

Am 11.04.2024 um 14:49 schrieb Paavo Helde:
> 11.04.2024 12:05 Bonita Montero kirjutas:
>> I tried to use futexes for my pow10-function.
>> Is the usage correct ?
>
> Maybe, but it's not needed and makes the code more complex, redundant
> and potentially a bit slower.
>
> C++ statics' initialization is guaranteed to be thread-safe since C++11.
> Just put your initialization logic into a separate function and use this
> for initializing your static variable. C++ will perform any needed
> multi-thread locking automatically, adding ones own explicit locks will
> not make it faster.
>
>

I just simplified it:

double pow10( int64_t exp )
{ constexpr uint64_t EXP_MASK = 0x7FFull << 52;
constexpr double INF = numeric_limits<double>::infinity();
static double const rawTenPows[64] alignas(CL_SIZE)
{
0x1.4000000000000p+3,
0x1.9000000000000p+6,
0x1.3880000000000p+13,
0x1.7d78400000000p+26,
0x1.1c37937e08000p+53,
0x1.3b8b5b5056e17p+106,
0x1.84f03e93ff9f6p+212,
0x1.27748f9301d33p+425,
0x1.54fdd7f73bf3fp+850,
INF, INF, INF, INF, INF, INF, INF, INF, INF, INF,
INF, INF, INF, INF, INF, INF, INF, INF, INF, INF,
INF, INF, INF, INF, INF, INF, INF, INF, INF, INF,
INF, INF, INF, INF, INF, INF, INF, INF, INF, INF,
INF, INF, INF, INF, INF, INF, INF, INF, INF, INF,
INF, INF, INF, INF, INF
};
span tenPows( rawTenPows );
// begin with 1.0 since x ^ 0 = 1
double result = 1.0;
// unsigned exponent
uint64_t uExp = exp >= 0 ? exp : -exp;
// iterator to highest exponent
auto itExp = tenPows.rbegin();
// as long as there are bits set in uExp
for( size_t gap ; uExp; uExp <<= gap, uExp <<= 1 )
{
// exponent bits gap
gap = countl_zero( uExp );
// get next exponent
itExp += gap;
// multiply result by next pow10 exponent
result *= *itExp++;
// overlow / underflow ?
if( (bit_cast<uint64_t>( result ) & EXP_MASK) == EXP_MASK ) [[unlikely]]
// yes: result wouldn't change furhter; stop
return exp >= 0 ? result : 0.0;
}
return exp >= 0 ? result : 1.0 / result;
}

Re: 10 ^ N

<uvvg68$3fhgl$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Fri, 19 Apr 2024 21:26:49 -0700
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <uvvg68$3fhgl$1@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Apr 2024 06:26:49 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5eaea31a3ed2a899286d00bdf92a80a0";
logging-data="3655189"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lUkBXFawg11BIlwu8rkVNRQeOX4/gDk4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:DCgMDh2uA7ZqBQmL1kYKHLZJmiU=
Content-Language: en-US
In-Reply-To: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Sat, 20 Apr 2024 04:26 UTC

On 4/11/2024 2:05 AM, Bonita Montero wrote:
> I tried to use futexes for my pow10-function.
> Is the usage correct ?
>
> #include <bit>
> #include <atomic>
> #include <array>
> #include <mutex>
> #include "xmath.h"
>
> using namespace std;
>
> double pow10( int64_t exp )
> {
>     constexpr uint64_t EXP_MASK = 0x7FFull << 52;
>     // table for binary exponentation with 10 ^ (2 ^ N)
>     static array<double, 64> tenPows;
>     // table initialized ?
>     if( static atomic_bool once( false ); !once.load(
> memory_order_acquire ) )
>     {
>         once.wait( false, memory_order_acquire );

Any problems with spurious wakes? You need to recheck the predicate when
a futex wait returns. Kind of akin to a condvar.

>         if( !once.load( memory_order_relaxed ) )
>         {
>             // no: calculate table
>             for( double p10x2xN = 10.0; double &pow : tenPows )
>                 pow = p10x2xN,
>                 p10x2xN *= p10x2xN;
>             // set initialized flag with release semantics
>             once.store( true, memory_order_release );
>             once.notify_all();
>         }
>     }
>     // begin with 1.0 since x ^ 0 = 1
>     double result = 1.0;
>     // unsigned exponent
>     uint64_t uExp = exp >= 0 ? exp : -exp;
>     // highest set bit of exponent
>     size_t bit = 63 - countl_zero( uExp );
>     // bit mask to highest set bit
>     uint64_t mask = 1ull << bit;
>     // loop as long as there are bits in unsigned exponent
>     for( ; uExp; uExp &= ~mask, mask >>= 1, --bit )
>         // bit set ?
>         if( uExp & mask )
>         {
>             // yes: multiply result by 10 ^ (bit + 1)
>             result *= tenPows[bit];
>             // overlow ?
>             if( (bit_cast<uint64_t>( result ) & EXP_MASK) == EXP_MASK )
>                 // yes: result wouldn't change furhter; stop
>                 break;
>         }
>     // return 1 / result if exponent is negative
>     return exp >= 0 ? result : 1.0 / result;
> };

Re: 10 ^ N

<uvvgb7$3fhgl$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Fri, 19 Apr 2024 21:29:27 -0700
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <uvvgb7$3fhgl$2@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Apr 2024 06:29:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5eaea31a3ed2a899286d00bdf92a80a0";
logging-data="3655189"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QNl+1FBITN6mzsxbF+BY2OwuQd0+oyqE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Gm9bSYo/C+/XrhnDx7zlpO4PuaI=
In-Reply-To: <uvvg68$3fhgl$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 20 Apr 2024 04:29 UTC

On 4/19/2024 9:26 PM, Chris M. Thomasson wrote:
> On 4/11/2024 2:05 AM, Bonita Montero wrote:
>> I tried to use futexes for my pow10-function.
>> Is the usage correct ?
>>
>> #include <bit>
>> #include <atomic>
>> #include <array>
>> #include <mutex>
>> #include "xmath.h"
>>
>> using namespace std;
>>
>> double pow10( int64_t exp )
>> {
>>      constexpr uint64_t EXP_MASK = 0x7FFull << 52;
>>      // table for binary exponentation with 10 ^ (2 ^ N)
>>      static array<double, 64> tenPows;
>>      // table initialized ?
>>      if( static atomic_bool once( false ); !once.load(
>> memory_order_acquire ) )
>>      {
>>          once.wait( false, memory_order_acquire );
>
> Any problems with spurious wakes? You need to recheck the predicate when
> a futex wait returns. Kind of akin to a condvar.
[...]

I need to focus on the code, but I think I can see some issues just at a
glance. Just because a futex wait returns does not mean that condition
it true.

Your code is rather odd for sure. Are you sure you know what you are doing?

Re: 10 ^ N

<uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 07:17:59 +0200
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 20 Apr 2024 07:17:55 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="14d6ddb846e8593f63920489669f92c8";
logging-data="3683720"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197T6OMvMOSGbrmoTN7ZQ45GNURJPQxUqY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:KOBTWC689z1mm++W3+rAoQTpD44=
In-Reply-To: <uvvg68$3fhgl$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 20 Apr 2024 05:17 UTC

Am 20.04.2024 um 06:26 schrieb Chris M. Thomasson:

>> once.wait( false, memory_order_acquire );

> Any problems with spurious wakes? You need to recheck the predicate when
> a futex wait returns. ....

>> if( !once.load( memory_order_relaxed ) )

What's that ?

Re: 10 ^ N

<v002v3$3jeca$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 02:47:16 -0700
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <v002v3$3jeca$1@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
<uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 20 Apr 2024 11:47:16 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5eaea31a3ed2a899286d00bdf92a80a0";
logging-data="3783050"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/j6+c32WnC9omKx+wgy2dJVPHLHi7ieMc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:z+o7SwtsZpoDJ94aIx15qRgKXNc=
In-Reply-To: <uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 20 Apr 2024 09:47 UTC

On 4/19/2024 10:17 PM, Bonita Montero wrote:
> Am 20.04.2024 um 06:26 schrieb Chris M. Thomasson:
>
>>> once.wait( false, memory_order_acquire );
>
>> Any problems with spurious wakes? You need to recheck the predicate
>> when a futex wait returns. ....
>
>>> if( !once.load( memory_order_relaxed ) )
>
> What's that ?
>
>

Humm... Think if three threads hit once.wait, and the all get woken up
spuriously, once is still false. Now all three threads do your

if( !once.load( memory_order_relaxed ) )

once is false, and all three threads go into your init code at the same
time. What am I missing here?

if( static atomic_bool once( false ); !once.load( memory_order_acquire ) )
{
// three threads A, B and C wait.
once.wait( false, memory_order_acquire );

// A, B, get here on a spurious wake.

// oh shit!
if( !once.load( memory_order_relaxed ) )
{

// all three threads are going to see once as being false
// they are all in the init code!
// you are now fucked!

What am I missing?

Re: 10 ^ N

<v003bk$3jeca$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 02:53:57 -0700
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <v003bk$3jeca$2@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
<uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
<v002v3$3jeca$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Apr 2024 11:53:57 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5eaea31a3ed2a899286d00bdf92a80a0";
logging-data="3783050"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19osR37Jx+XBxPOtghmrGRSCuK/mpSmZpQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:IgQH3/LiTjDJ7REHucnJwLuj5ps=
Content-Language: en-US
In-Reply-To: <v002v3$3jeca$1@dont-email.me>
 by: Chris M. Thomasson - Sat, 20 Apr 2024 09:53 UTC

On 4/20/2024 2:47 AM, Chris M. Thomasson wrote:
> On 4/19/2024 10:17 PM, Bonita Montero wrote:
>> Am 20.04.2024 um 06:26 schrieb Chris M. Thomasson:
>>
>>>> once.wait( false, memory_order_acquire );
>>
>>> Any problems with spurious wakes? You need to recheck the predicate
>>> when a futex wait returns. ....
>>
>>>> if( !once.load( memory_order_relaxed ) )
>>
>> What's that ?
>>
>>
>
> Humm... Think if three threads hit once.wait, and the all get woken up
> spuriously, once is still false. Now all three threads do your
>
> if( !once.load( memory_order_relaxed ) )
>
> once is false, and all three threads go into your init code at the same
> time. What am I missing here?
>
>
>  if( static atomic_bool once( false ); !once.load( memory_order_acquire
> ) )
>     {
>         // three threads A, B and C wait.
>         once.wait( false, memory_order_acquire );
>
>
>         // A, B, get here on a spurious wake.
>
>
>         // oh shit!
>         if( !once.load( memory_order_relaxed ) )
>         {
>
>               // all three threads are going to see once as being false
>               // they are all in the init code!
>               // you are now fucked!
>
> What am I missing?

You probably want something like this pseudo-code, untested typed in the
newsreader:
________________________
atomic<int> g_state = 0;

if (! g_state.exchange(1, relaxed))
{ // critical section

// do your thing...

g_state.store(2, release);
g_state.notify_all(relaxed);
}

else
{ while (g_state.load(acquire) != 2)
{
g_state.wait(1, relaxed);
}
} ________________________

?

Re: 10 ^ N

<v003jb$3jeca$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 02:58:04 -0700
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <v003jb$3jeca$3@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
<uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
<v002v3$3jeca$1@dont-email.me> <v003bk$3jeca$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Apr 2024 11:58:04 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5eaea31a3ed2a899286d00bdf92a80a0";
logging-data="3783050"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197xo50Z8g4VqOqFL3GuwlVTXaN2PCfaoE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:zKK5MnYMJ9FHzuIfTiSei0re2dc=
Content-Language: en-US
In-Reply-To: <v003bk$3jeca$2@dont-email.me>
 by: Chris M. Thomasson - Sat, 20 Apr 2024 09:58 UTC

On 4/20/2024 2:53 AM, Chris M. Thomasson wrote:
> On 4/20/2024 2:47 AM, Chris M. Thomasson wrote:
>> On 4/19/2024 10:17 PM, Bonita Montero wrote:
>>> Am 20.04.2024 um 06:26 schrieb Chris M. Thomasson:
>>>
>>>>> once.wait( false, memory_order_acquire );
>>>
>>>> Any problems with spurious wakes? You need to recheck the predicate
>>>> when a futex wait returns. ....
>>>
>>>>> if( !once.load( memory_order_relaxed ) )
>>>
>>> What's that ?
>>>
>>>
>>
>> Humm... Think if three threads hit once.wait, and the all get woken up
>> spuriously, once is still false. Now all three threads do your
>>
>> if( !once.load( memory_order_relaxed ) )
>>
>> once is false, and all three threads go into your init code at the
>> same time. What am I missing here?
>>
>>
>>   if( static atomic_bool once( false ); !once.load(
>> memory_order_acquire ) )
>>      {
>>          // three threads A, B and C wait.
>>          once.wait( false, memory_order_acquire );
>>
>>
>>          // A, B, get here on a spurious wake.
>>
>>
>>          // oh shit!
>>          if( !once.load( memory_order_relaxed ) )
>>          {
>>
>>                // all three threads are going to see once as being false
>>                // they are all in the init code!
>>                // you are now fucked!
>>
>> What am I missing?
>
>
> You probably want something like this pseudo-code, untested typed in the
> newsreader:
> ________________________
> atomic<int> g_state = 0;
>
> if (! g_state.exchange(1, relaxed))

Yikes! That should be a CAS

if (g_state.cas(0, 1))

We want to set it to 1 if and only if it was 0 before, or do nothing.

Sorry about that! God damn it! lol.

> {
>     // critical section
>
>     // do your thing...
>
>     g_state.store(2, release);
>     g_state.notify_all(relaxed);
> }
>
> else
> {
>     while (g_state.load(acquire) != 2)
>     {
>         g_state.wait(1, relaxed);
>     }
> }
> ________________________
>
> ?

Re: 10 ^ N

<v004f4$3jsjm$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 03:12:51 -0700
Organization: A noiseless patient Spider
Lines: 121
Message-ID: <v004f4$3jsjm$1@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 20 Apr 2024 12:12:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5eaea31a3ed2a899286d00bdf92a80a0";
logging-data="3797622"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+easngARNdHNxKEFOHjmFCKbL7prikK4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:KSEAMeCIZEJKLQu94fB442uqeRg=
In-Reply-To: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 20 Apr 2024 10:12 UTC

On 4/11/2024 2:05 AM, Bonita Montero wrote:
> I tried to use futexes for my pow10-function.
> Is the usage correct ?
[...]

I coded up a quick and dirty little test for a futex based double
checked lock.

Can you run it?
______________________________
#include <iostream>
#include <functional>
#include <atomic>
#include <thread>
#include <chrono>

#include <cstdlib>

// Ughh, some quick test params...
#define CT_L2_CACHE 128UL
#define CT_THREAD_N 8UL
#define CT_ITER_N 10000000UL

struct alignas(CT_L2_CACHE) ct_double_checked_lock
{ std::atomic<unsigned int> m_state = 0;
int m_var = 0;

void init()
{
unsigned int state = 0;

if (m_state.compare_exchange_strong(
state,
1,
std::memory_order_acquire))
{
// critical section
std::this_thread::yield();
m_var += 123;
std::this_thread::yield();

m_state.store(2, std::memory_order_release);
m_state.notify_all();
}

else
{
while (m_state.load(std::memory_order_acquire) != 2)
{
m_state.wait(1, std::memory_order_relaxed);
}
}
}
};

struct ct_shared
{ ct_double_checked_lock m_dcl;

bool check_state()
{
if (m_dcl.m_var != 123)
{
std::cout << "GOD DAMN IT!!!\n" << std::endl;
return false;
}

return true;
}
};

void ct_thread(ct_shared& shared)
{ for (unsigned long i = 0; i < CT_ITER_N; ++i)
{
shared.m_dcl.init();
}
}

int main()
{ std::cout << "ct_c++20_futex_test\n\n";
std::cout << "__________________________________\n\n" << std::endl;

{
ct_shared shared;

std::cout << "Creating Threads..." << std::endl;

std::thread threads[CT_THREAD_N];

for (unsigned long i = 0; i < CT_THREAD_N; ++i)
{
threads[i] = std::thread(ct_thread, std::ref(shared));
}

std::cout << "Running...\n" << std::endl;

for (unsigned long i = 0; i < CT_THREAD_N; ++i)
{
threads[i].join();
}

if (! shared.check_state())
{
std::cout << "OH SHIT!!!!! The crap sure hit the fan!!!\n";
}
}

std::cout << "Complete!" << std::endl;

return EXIT_SUCCESS;
} ______________________________

Re: 10 ^ N

<v01h48$3td37$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 15:55:04 -0700
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <v01h48$3td37$1@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
<uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Apr 2024 00:55:04 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7de4a237f5dd7ab0ec891d2dca5bce18";
logging-data="4109415"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/L1USqcy2nOuYUgRB1Ludz8kF6lIp2DUI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ghNynizAR/xGu1b5PLcGNQ9VFJk=
In-Reply-To: <uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 20 Apr 2024 22:55 UTC

On 4/19/2024 10:17 PM, Bonita Montero wrote:
> Am 20.04.2024 um 06:26 schrieb Chris M. Thomasson:
>
>>> once.wait( false, memory_order_acquire );
>
>> Any problems with spurious wakes? You need to recheck the predicate
>> when a futex wait returns. ....
>
>>> if( !once.load( memory_order_relaxed ) )
>
> What's that ?
>
>

Still not 100% sure if atomic::wait is allowed to return spuriously. The
docs seem to say that it cannot? That means that it must do a double
check in a loop in its logic. This is a lot different than normal futex
behavior.

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

Re: 10 ^ N

<v01h8d$3td37$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.network!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 15:57:18 -0700
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <v01h8d$3td37$2@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
<uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
<v01h48$3td37$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Apr 2024 00:57:17 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7de4a237f5dd7ab0ec891d2dca5bce18";
logging-data="4109415"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Upk3YHE/6100oK958fop+nFbaTPbGunE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:domBD0RJruQUka0oXFz5cbxIvZc=
In-Reply-To: <v01h48$3td37$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 20 Apr 2024 22:57 UTC

On 4/20/2024 3:55 PM, Chris M. Thomasson wrote:
> On 4/19/2024 10:17 PM, Bonita Montero wrote:
>> Am 20.04.2024 um 06:26 schrieb Chris M. Thomasson:
>>
>>>> once.wait( false, memory_order_acquire );
>>
>>> Any problems with spurious wakes? You need to recheck the predicate
>>> when a futex wait returns. ....
>>
>>>> if( !once.load( memory_order_relaxed ) )
>>
>> What's that ?
>>
>>
>
> Still not 100% sure if atomic::wait is allowed to return spuriously. The
> docs seem to say that it cannot? That means that it must do a double
> check in a loop in its logic. This is a lot different than normal futex
> behavior.
>
> https://en.cppreference.com/w/cpp/atomic/atomic/wait

Still, your logic is still going to have problems.

Re: 10 ^ N

<v01hp9$3td37$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 16:06:18 -0700
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <v01hp9$3td37$3@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Apr 2024 01:06:18 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7de4a237f5dd7ab0ec891d2dca5bce18";
logging-data="4109415"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SN+TjhZ+coPuhhKllfV0hSUqymgRL/kY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:s7EDkAIFrpmMxysjcT5KpXttVsg=
In-Reply-To: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 20 Apr 2024 23:06 UTC

On 4/11/2024 2:05 AM, Bonita Montero wrote:
> I tried to use futexes for my pow10-function.
> Is the usage correct ?
>
> #include <bit>
> #include <atomic>
> #include <array>
> #include <mutex>
> #include "xmath.h"
>
> using namespace std;
>
> double pow10( int64_t exp )
> {
>     constexpr uint64_t EXP_MASK = 0x7FFull << 52;
>     // table for binary exponentation with 10 ^ (2 ^ N)
>     static array<double, 64> tenPows;
>     // table initialized ?
>     if( static atomic_bool once( false ); !once.load(
> memory_order_acquire ) )
>     {

Say three threads got here. Now, they only block if once is false, which
it is, okay:

>         once.wait( false, memory_order_acquire );

Why would they not wait forever right here? Spurious aside for a moment...

You have some interesting issues.

>         if( !once.load( memory_order_relaxed ) )
>         {
>             // no: calculate table
>             for( double p10x2xN = 10.0; double &pow : tenPows )
>                 pow = p10x2xN,
>                 p10x2xN *= p10x2xN;
>             // set initialized flag with release semantics
>             once.store( true, memory_order_release );
>             once.notify_all();
>         }
>     }
>     // begin with 1.0 since x ^ 0 = 1
>     double result = 1.0;
>     // unsigned exponent
>     uint64_t uExp = exp >= 0 ? exp : -exp;
>     // highest set bit of exponent
>     size_t bit = 63 - countl_zero( uExp );
>     // bit mask to highest set bit
>     uint64_t mask = 1ull << bit;
>     // loop as long as there are bits in unsigned exponent
>     for( ; uExp; uExp &= ~mask, mask >>= 1, --bit )
>         // bit set ?
>         if( uExp & mask )
>         {
>             // yes: multiply result by 10 ^ (bit + 1)
>             result *= tenPows[bit];
>             // overlow ?
>             if( (bit_cast<uint64_t>( result ) & EXP_MASK) == EXP_MASK )
>                 // yes: result wouldn't change furhter; stop
>                 break;
>         }
>     // return 1 / result if exponent is negative
>     return exp >= 0 ? result : 1.0 / result;
> };

Re: 10 ^ N

<v01ln6$3u6te$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 17:13:26 -0700
Organization: A noiseless patient Spider
Lines: 110
Message-ID: <v01ln6$3u6te$1@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<v004f4$3jsjm$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Apr 2024 02:13:26 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7de4a237f5dd7ab0ec891d2dca5bce18";
logging-data="4135854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dYmj+vGxriALgc+SpV1Y8OLlzzUQx+DU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:cenmxF+qXuv3URd/3tVRiJ4j5SU=
Content-Language: en-US
In-Reply-To: <v004f4$3jsjm$1@dont-email.me>
 by: Chris M. Thomasson - Sun, 21 Apr 2024 00:13 UTC

On 4/20/2024 3:12 AM, Chris M. Thomasson wrote:
> On 4/11/2024 2:05 AM, Bonita Montero wrote:
>> I tried to use futexes for my pow10-function.
>> Is the usage correct ?
> [...]
>
> I coded up a quick and dirty little test for a futex based double
> checked lock.
>
> Can you run it?
> ______________________________
> #include <iostream>
> #include <functional>
> #include <atomic>
> #include <thread>
> #include <chrono>
>
> #include <cstdlib>
>
>
> // Ughh, some quick test params...
> #define CT_L2_CACHE 128UL
> #define CT_THREAD_N 8UL
> #define CT_ITER_N 10000000UL
>
>
> struct alignas(CT_L2_CACHE) ct_double_checked_lock
> {
>     std::atomic<unsigned int> m_state = 0;
>     int m_var = 0;
>
>     void init()
>     {
>         unsigned int state = 0;
>
>         if (m_state.compare_exchange_strong(
>                 state,
>                 1,
>                 std::memory_order_acquire))
>         {
>             // critical section
>             std::this_thread::yield();
>             m_var += 123;
>             std::this_thread::yield();
>
>             m_state.store(2, std::memory_order_release);
>             m_state.notify_all();
>         }
>

>         else
>         {
>             while (m_state.load(std::memory_order_acquire) != 2)
>             {
>                 m_state.wait(1, std::memory_order_relaxed);
>             }
^^^^^^^^^^^^^^^^^^^^

Now according to these docs:

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

It says that an underlying impl may encounter spurious wakes. However,
it also says this, which changes my previous view of futexes. C++ is a
bit different wrt:
_________________
These functions are guaranteed to return only if value has changed, even
if underlying implementation unblocks spuriously.
_________________

Oh, wow. So, this would mean I can get around that while loop in a slow
path of my DCL code. This is going against my previous knowledge of
futexes. C++ might as well have a std::wait_strong/weak akin to
compare_exchange_strong/weak... Anyway, so I should be able to get rid
of that loop:
_____________
while (m_state.load(std::memory_order_acquire) != 2)
{ m_state.wait(1, std::memory_order_relaxed);
} _____________

Into just:
_____________
if (m_state.load(std::memory_order_acquire) != 2)
{ // slow path
m_state.wait(1, std::memory_order_relaxed);

// no need to recheck because m_state == 2 here for sure.
} _____________

Humm... This goes against my futex ways! If C++ really does do its own
internal check and automatically handles any spurious wakes from the
underlying impl, then, well. Okay. Just not what I expected.

>         }
>     }
> };
>
[...]

Re: 10 ^ N

<v01m47$3u6te$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 17:20:23 -0700
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <v01m47$3u6te$2@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<v004f4$3jsjm$1@dont-email.me> <v01ln6$3u6te$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Apr 2024 02:20:23 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7de4a237f5dd7ab0ec891d2dca5bce18";
logging-data="4135854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19TSyToR5SRiogZLZsREJSbJqcvPZ+xPPw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:o1nAx7dpc10jYqOE9fEGfEQr/as=
Content-Language: en-US
In-Reply-To: <v01ln6$3u6te$1@dont-email.me>
 by: Chris M. Thomasson - Sun, 21 Apr 2024 00:20 UTC

On 4/20/2024 5:13 PM, Chris M. Thomasson wrote:
[...]
> Oh, wow. So, this would mean I can get around that while loop in a slow
> path of my DCL code. This is going against my previous knowledge of
> futexes. C++ might as well have a std::wait_strong/weak akin to
> compare_exchange_strong/weak... Anyway, so I should be able to get rid
> of that loop:
> _____________
> while (m_state.load(std::memory_order_acquire) != 2)
> {
>     m_state.wait(1, std::memory_order_relaxed);
> }
> _____________
>
> Into just:
> _____________
> if (m_state.load(std::memory_order_acquire) != 2)
> {
>    // slow path
>    m_state.wait(1, std::memory_order_relaxed);
^^^^^^^^^^^^^^

This wait would need memory_order_acquire, not relaxed. I am not used to
actually embedding a membar into a futex wait call. Afaict, the C++
futex sepcs are at a slightly higher level than traditional futex impls.

Good thing its just code in the newsreader. I need to implement this in
real code. If c++ does what those specs says they do, then it should
work, and that while loop can be omitted.

>
>    // no need to recheck because m_state == 2 here for sure.
> }
> _____________
>
> Humm... This goes against my futex ways! If C++ really does do its own
> internal check and automatically handles any spurious wakes from the
> underlying impl, then, well. Okay. Just not what I expected.
>
>
>
>
>
>>          }
>>      }
>> };
>>
> [...]
>
>

Re: 10 ^ N

<v01mpa$3u6te$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sat, 20 Apr 2024 17:31:39 -0700
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <v01mpa$3u6te$3@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<v004f4$3jsjm$1@dont-email.me> <v01ln6$3u6te$1@dont-email.me>
<v01m47$3u6te$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Apr 2024 02:31:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7de4a237f5dd7ab0ec891d2dca5bce18";
logging-data="4135854"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jijYPjS1xDkJUTQmxrMcs95Bwr44NPHg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:BM4UZm5d4nH9HUl8VupYST8gd2c=
Content-Language: en-US
In-Reply-To: <v01m47$3u6te$2@dont-email.me>
 by: Chris M. Thomasson - Sun, 21 Apr 2024 00:31 UTC

On 4/20/2024 5:20 PM, Chris M. Thomasson wrote:
[...]
>> Humm... This goes against my futex ways! If C++ really does do its own
>> internal check and automatically handles any spurious wakes from the
>> underlying impl, then, well. Okay. Just not what I expected.

Actually, I can refine this further since std::atomic::wait is specified
to not return spuriously. The updated value from the failed
compare_exchange_strong can be used. Perhaps something like:
__________________
struct alignas(CT_L2_CACHE) ct_double_checked_lock
{ std::atomic<unsigned int> m_state = 0;
int m_var = 0;

void init()
{
unsigned int state = 0;

if (m_state.compare_exchange_strong(
state,
1,
std::memory_order_acquire))
{
// critical section
std::this_thread::yield();
m_var += 123;
std::this_thread::yield();

m_state.store(2, std::memory_order_release);
m_state.notify_all();
}

else if (state != 2)
{
m_state.wait(1, std::memory_order_acquire);
}
}
};
__________________

That should work? This goes against my old school futex thinking. ;^)

Re: 10 ^ N

<v04ht2$njs3$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: 10 ^ N
Date: Sun, 21 Apr 2024 19:26:42 -0700
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <v04ht2$njs3$1@dont-email.me>
References: <uv8955$1jss1$1@raubtier-asyl.eternal-september.org>
<uvvg68$3fhgl$1@dont-email.me>
<uvvj63$3gdc8$1@raubtier-asyl.eternal-september.org>
<v01h48$3td37$1@dont-email.me> <v01h8d$3td37$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Apr 2024 04:26:42 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="66d6990e316a47f3e0444523b8616b68";
logging-data="774019"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PjemcEZqz6vKjyyHPl5a/N4tffWNfFHE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:7t50xK93eb1cSOmByz1NL/2vdP8=
Content-Language: en-US
In-Reply-To: <v01h8d$3td37$2@dont-email.me>
 by: Chris M. Thomasson - Mon, 22 Apr 2024 02:26 UTC

On 4/20/2024 3:57 PM, Chris M. Thomasson wrote:
> On 4/20/2024 3:55 PM, Chris M. Thomasson wrote:
>> On 4/19/2024 10:17 PM, Bonita Montero wrote:
>>> Am 20.04.2024 um 06:26 schrieb Chris M. Thomasson:
>>>
>>>>> once.wait( false, memory_order_acquire );
>>>
>>>> Any problems with spurious wakes? You need to recheck the predicate
>>>> when a futex wait returns. ....
>>>
>>>>> if( !once.load( memory_order_relaxed ) )
>>>
>>> What's that ?
>>>
>>>
>>
>> Still not 100% sure if atomic::wait is allowed to return spuriously.
>> The docs seem to say that it cannot? That means that it must do a
>> double check in a loop in its logic. This is a lot different than
>> normal futex behavior.
>>
>> https://en.cppreference.com/w/cpp/atomic/atomic/wait
>
> Still, your logic is still going to have problems.

Correct your program Bonita?

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor