Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If A = B and B = C, then A = C, except where void or prohibited by law. -- Roy Santoro


devel / comp.lang.c++ / C++20 futex with heavy contention slower than mutex

SubjectAuthor
* C++20 futex with heavy contention slower than mutexBonita Montero
+* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
|+* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||`* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
|| +- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
|| `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||  `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   +- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   +* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |`* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   | `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |  `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |   `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |    `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |     `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |      `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |       `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |        `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |         `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |          `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |           `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |            `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |             +* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |             |`- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |             +- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |             `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |              `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |               `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                 `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  +* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |`* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  | `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |  `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   +* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |+- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |`* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   | `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |  `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |   `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |    `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |     `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |      `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |       `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |        +* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |        |`* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |        | `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |        |  `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |        |   `- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |        `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  |   |         `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |          `* Re: C++20 futex with heavy contention slower than mutexRichard Damon
||   |                  |   |           `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |            +* Re: C++20 futex with heavy contention slower than mutexScott Lurndal
||   |                  |   |            |`* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |            | `* Re: C++20 futex with heavy contention slower than mutexScott Lurndal
||   |                  |   |            |  `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |            |   `* Re: C++20 futex with heavy contention slower than mutexScott Lurndal
||   |                  |   |            |    `* Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |            |     `* Re: C++20 futex with heavy contention slower than mutexScott Lurndal
||   |                  |   |            |      `- Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   |            `* Re: C++20 futex with heavy contention slower than mutexRichard Damon
||   |                  |   |             `- Re: C++20 futex with heavy contention slower than mutexBonita Montero
||   |                  |   `- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   |                  `- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||   `* Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
||    `- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
|`- Re: C++20 futex with heavy contention slower than mutexBonita Montero
+- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson
`* Re: C++20 futex with heavy contention slower than mutexMichael S
 `- Re: C++20 futex with heavy contention slower than mutexChris M. Thomasson

Pages:123
C++20 futex with heavy contention slower than mutex

<uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: C++20 futex with heavy contention slower than mutex
Date: Fri, 29 Mar 2024 14:14:11 +0100
Organization: A noiseless patient Spider
Lines: 170
Message-ID: <uu6eqs$ab8u$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: Fri, 29 Mar 2024 13:14:04 +0100 (CET)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="8336d7d8531d53c859d1ac5c10960755";
logging-data="339230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LT49Nl3C7mZvo4HE+eQShATdEQz0qnYk="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:5fu4FmMj5RuQf4eFSeRKV7uqqBU=
Content-Language: de-DE
 by: Bonita Montero - Fri, 29 Mar 2024 13:14 UTC

The following program simulates constant locking und unlocking of one
to jthread::hardware_concurrency() threadas with a std::mutex and a
futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
5 threads constantly contending, but beyond the CPU time of the futex
explodes and the conventional mutex is faster with Windows as and with
Linux.

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <functional>
#include <chrono>
#include <vector>

using namespace std;
using namespace chrono;

int main()
{ constexpr int64_t ROUNDS = 10000;
auto bench = [&]( char const *head, auto fn )
{
cout << head << endl;
atomic_int64_t tSum( 0 );
vector<jthread> threads;
int hc = jthread::hardware_concurrency();
for( int nThreads = 1; nThreads <= hc; ++nThreads )
{
for( unsigned t = nThreads; t; --t )
threads.emplace_back( [&]()
{
auto start = high_resolution_clock::now();
int64_t cmp = 0;
for( int64_t r = ROUNDS; r; --r )
cmp = fn( cmp );
tSum += duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count();
} );
threads.resize( 0 );
cout << "\t" << nThreads << ": " << tSum /
((double)nThreads * ROUNDS) << endl;
}
};
{
mutex mtx;
bench( "mutex: ",
[&]( int64_t )
{
mtx.lock();
mtx.unlock();
return 0;
} );
}
{
atomic_int64_t futex( 0 );
constexpr int64_t HIBIT = numeric_limits<int64_t>::min();
bench( "futex:",
[&]( int64_t cmp )
{
int64_t niu;
for( ; ; )
if( cmp >= 0 )
if( futex.compare_exchange_weak( cmp, niu = cmp
| HIBIT, memory_order_acquire, memory_order_relaxed ) )
{
cmp = niu;
break;
}
else;
else
if( futex.compare_exchange_weak( cmp, niu = cmp
+ 1, memory_order_relaxed, memory_order_relaxed ) )
{
futex.wait( niu, memory_order_acquire );
cmp = futex.load( memory_order_relaxed );
}
for( ; ; )
if( cmp & ~HIBIT )
if( futex.compare_exchange_weak( cmp, niu =
(cmp - 1) & ~HIBIT, memory_order_release, memory_order_relaxed ) )
{
cmp = niu;
futex.notify_one();
break;
}
else;
else
if( futex.compare_exchange_weak( cmp, 0,
memory_order_release, memory_order_relaxed ) )
{
cmp = 0;
break;
}
return cmp;
} );
}
}

This are the results under Ubuntu 20.04 LTS:

mutex:
1: 3.9134
2: 5.8706
3: 33.1753
4: 48.63
5: 71.6777
6: 109.556
7: 151.271
8: 208.871
9: 288.031
10: 353.536
11: 446.28
12: 563.841
13: 701.134
14: 833.08
15: 983.734
16: 1138.48
17: 1297.53
18: 1481.15
19: 1664.51
20: 1857.88
21: 2053.83
22: 2285.07
23: 2546.67
24: 2782.53
25: 3065.25
26: 3349.4
27: 3652.06
28: 3971.25
29: 4339.13
30: 4727.31
31: 5129.95
32: 5499.05
futex:
1: 3.3654
2: 5.02155
3: 6.69107
4: 15.2522
5: 16.8824
6: 185.235
7: 162.176
8: 360.421
9: 2272.83
10: 4805.11
11: 8987.24
12: 14225.5
13: 20076.4
14: 27199.4
15: 36711.8
16: 49480.8
17: 57641.8
18: 75633.2
19: 97196.6
20: 122223
21: 147617
22: 180377
23: 216084
24: 256032
25: 299681
26: 345518
27: 398785
28: 445499
29: 506351
30: 572440
31: 643455
32: 721461

With all threads locking and unlocking the conventional mutex is 131
timea faster.

Re: C++20 futex with heavy contention slower than mutex

<uu7fa1$i04u$2@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Fri, 29 Mar 2024 15:28:16 -0700
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <uu7fa1$i04u$2@dont-email.me>
References: <uu6eqs$ab8u$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: Fri, 29 Mar 2024 22:28:17 +0100 (CET)
Injection-Info: dont-email.me; posting-host="90aa96892193c0823071ec0d2869dcb8";
logging-data="589982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1949nIKIlRAzZXh6wqquR409g8OQO/lKBU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lEIYgMjM2O6BVpSmJdOniCyod04=
In-Reply-To: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 29 Mar 2024 22:28 UTC

On 3/29/2024 6:14 AM, Bonita Montero wrote:
> The following program simulates constant locking und unlocking of one
> to jthread::hardware_concurrency() threadas with a std::mutex and a
> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
> 5 threads constantly contending, but beyond the CPU time of the futex
> explodes and the conventional mutex is faster with Windows as and with
> Linux.
[...]

A futex is not a mutex!

Re: C++20 futex with heavy contention slower than mutex

<uu7feo$i04u$3@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Fri, 29 Mar 2024 15:30:48 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <uu7feo$i04u$3@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 29 Mar 2024 22:30:48 +0100 (CET)
Injection-Info: dont-email.me; posting-host="90aa96892193c0823071ec0d2869dcb8";
logging-data="589982"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KlkD1m40Xzsyw2BMtYhX5X7zwpkaXpzE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:zSZFNJIno4sy5m556P+Qi1ICN1k=
Content-Language: en-US
In-Reply-To: <uu7fa1$i04u$2@dont-email.me>
 by: Chris M. Thomasson - Fri, 29 Mar 2024 22:30 UTC

On 3/29/2024 3:28 PM, Chris M. Thomasson wrote:
> On 3/29/2024 6:14 AM, Bonita Montero wrote:
>> The following program simulates constant locking und unlocking of one
>> to jthread::hardware_concurrency() threadas with a std::mutex and a
>> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
>> 5 threads constantly contending, but beyond the CPU time of the futex
>> explodes and the conventional mutex is faster with Windows as and with
>> Linux.
> [...]
>
> A futex is not a mutex!
>

I have to take a look at the logic you used for your "mutex" based on a
futex, not enough time right now. Perhaps, the std::mutex is just way
more efficient that your use of a futex? Keep in mind futexs are tricky:

https://cis.temple.edu/~giorgio/cis307/readings/futex.pdf

;^)

It depends on the logic you use.

Re: C++20 futex with heavy contention slower than mutex

<uu7it7$iqqi$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Fri, 29 Mar 2024 16:29:43 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <uu7it7$iqqi$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 29 Mar 2024 23:29:43 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e3ffe136872d6b59fa10d485f79c0803";
logging-data="617298"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18uWq4LU/3RN1cbBjbbV6Z/tM6INLcorVI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:cD5dCgWUNJu5uU5GpGeeQ/wbzT4=
Content-Language: en-US
In-Reply-To: <uu7feo$i04u$3@dont-email.me>
 by: Chris M. Thomasson - Fri, 29 Mar 2024 23:29 UTC

On 3/29/2024 3:30 PM, Chris M. Thomasson wrote:
> On 3/29/2024 3:28 PM, Chris M. Thomasson wrote:
>> On 3/29/2024 6:14 AM, Bonita Montero wrote:
>>> The following program simulates constant locking und unlocking of one
>>> to jthread::hardware_concurrency() threadas with a std::mutex and a
>>> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
>>> 5 threads constantly contending, but beyond the CPU time of the futex
>>> explodes and the conventional mutex is faster with Windows as and with
>>> Linux.
>> [...]
>>
>> A futex is not a mutex!
>>
>
> I have to take a look at the logic you used for your "mutex" based on a
> futex, not enough time right now. Perhaps, the std::mutex is just way
> more efficient that your use of a futex? Keep in mind futexs are tricky:
>
> https://cis.temple.edu/~giorgio/cis307/readings/futex.pdf
>
> ;^)
>
> It depends on the logic you use.

You generally want a "waitbit" type of logic... I posted a mutex based
on a futex here a while back. I just need to find it. Iirc, it used a
Windows "futex"... If I can find it, you should test against it. It used
NO compare and swap. Ugghhhh.... Try to avoid that.

Re: C++20 futex with heavy contention slower than mutex

<uu7ue6$onhl$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Fri, 29 Mar 2024 19:46:29 -0700
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <uu7ue6$onhl$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Mar 2024 02:46:30 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e3ffe136872d6b59fa10d485f79c0803";
logging-data="810549"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+VJ/nh0XUdgalbphBvAUUFp18QXncnPOg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:uzlw1HwAKZJmOQPZM57Kj+xGTE8=
In-Reply-To: <uu7it7$iqqi$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 30 Mar 2024 02:46 UTC

On 3/29/2024 4:29 PM, Chris M. Thomasson wrote:
> On 3/29/2024 3:30 PM, Chris M. Thomasson wrote:
>> On 3/29/2024 3:28 PM, Chris M. Thomasson wrote:
>>> On 3/29/2024 6:14 AM, Bonita Montero wrote:
>>>> The following program simulates constant locking und unlocking of one
>>>> to jthread::hardware_concurrency() threadas with a std::mutex and a
>>>> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
>>>> 5 threads constantly contending, but beyond the CPU time of the futex
>>>> explodes and the conventional mutex is faster with Windows as and with
>>>> Linux.
>>> [...]
>>>
>>> A futex is not a mutex!
>>>
>>
>> I have to take a look at the logic you used for your "mutex" based on
>> a futex, not enough time right now. Perhaps, the std::mutex is just
>> way more efficient that your use of a futex? Keep in mind futexs are
>> tricky:
>>
>> https://cis.temple.edu/~giorgio/cis307/readings/futex.pdf
>>
>> ;^)
>>
>> It depends on the logic you use.
>
> You generally want a "waitbit" type of logic... I posted a mutex based
> on a futex here a while back. I just need to find it. Iirc, it used a
> Windows "futex"... If I can find it, you should test against it. It used
> NO compare and swap. Ugghhhh.... Try to avoid that.

Another key, try to avoid waiting on a futex, strive for that. Maximize
the fast paths, and minimize the slow paths.

Re: C++20 futex with heavy contention slower than mutex

<uu83qp$pv7s$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 30 Mar 2024 05:18:35 +0100
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uu83qp$pv7s$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Mar 2024 04:18:34 +0100 (CET)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="f1d1aa16ce03f0b55eea9d3066b593e3";
logging-data="851196"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pX/9+7vLNZo/IAl/7htbZYWO4zXAx/jQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:6/gaGlhuuHoQetWJD5HaMpfVvaQ=
Content-Language: de-DE
In-Reply-To: <uu7fa1$i04u$2@dont-email.me>
 by: Bonita Montero - Sat, 30 Mar 2024 04:18 UTC

Am 29.03.2024 um 23:28 schrieb Chris M. Thomasson:
> On 3/29/2024 6:14 AM, Bonita Montero wrote:
>> The following program simulates constant locking und unlocking of one
>> to jthread::hardware_concurrency() threadas with a std::mutex and a
>> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
>> 5 threads constantly contending, but beyond the CPU time of the futex
>> explodes and the conventional mutex is faster with Windows as and with
>> Linux.
> [...]
>
> A futex is not a mutex!
>

You can build a mutex on top of a futex and that's the typical usage.

Re: C++20 futex with heavy contention slower than mutex

<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 30 Mar 2024 09:25:26 +0100
Organization: A noiseless patient Spider
Lines: 178
Message-ID: <uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Mar 2024 08:25:25 +0100 (CET)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="f1d1aa16ce03f0b55eea9d3066b593e3";
logging-data="945049"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+QGFvx21w01imeGkfw8TEdMn9Z4OzqVmo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:BBLAomcLAEM1LDGihZp6PC0GhOU=
In-Reply-To: <uu7it7$iqqi$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Sat, 30 Mar 2024 08:25 UTC

Am 30.03.2024 um 00:29 schrieb Chris M. Thomasson:
> On 3/29/2024 3:30 PM, Chris M. Thomasson wrote:
>> On 3/29/2024 3:28 PM, Chris M. Thomasson wrote:
>>> On 3/29/2024 6:14 AM, Bonita Montero wrote:
>>>> The following program simulates constant locking und unlocking of one
>>>> to jthread::hardware_concurrency() threadas with a std::mutex and a
>>>> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
>>>> 5 threads constantly contending, but beyond the CPU time of the futex
>>>> explodes and the conventional mutex is faster with Windows as and with
>>>> Linux.
>>> [...]
>>>
>>> A futex is not a mutex!
>>>
>>
>> I have to take a look at the logic you used for your "mutex" based on
>> a futex, not enough time right now. Perhaps, the std::mutex is just
>> way more efficient that your use of a futex? Keep in mind futexs are
>> tricky:
>>
>> https://cis.temple.edu/~giorgio/cis307/readings/futex.pdf
>>
>> ;^)
>>
>> It depends on the logic you use.
>
> You generally want a "waitbit" type of logic...

The sign-bit of my uint64 shows that signs that the lock is occupied
and the 63 lower bits sign the number of threads waiting to get owner-
ship. But i simplified the code a lot by using just an atomic_bool.
Now under Windows the futex is superior from 12 threads on. But under
Linux the futex is slower except for one or two threads.

Windows:

1: 248%
2: 54%
3: 27%
4: 28%
5: 33%
6: 31%
7: 37%
8: 37%
9: 53%
10: 42%
11: 82%
12: 101%
13: 116%
14: 142%
15: 153%
16: 160%
17: 175%
18: 191%
19: 192%
20: 194%
21: 206%
22: 211%
23: 221%
24: 220%
25: 223%
26: 220%
27: 220%
28: 223%
29: 222%
30: 220%
31: 219%
32: 216%

Linux:

1: 121%
2: 106%
3: 67%
4: 99%
5: 87%
6: 18%
7: 13%
8: 12%
9: 10%
10: 10%
11: 9%
12: 9%
13: 8%
14: 8%
15: 8%
16: 8%
17: 8%
18: 8%
19: 8%
20: 8%
21: 7%
22: 7%
23: 8%
24: 7%
25: 7%
26: 7%
27: 7%
28: 7%
29: 7%
30: 7%
31: 7%
32: 6%

#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
#include <vector>
#include <mutex>

using namespace std;
using namespace chrono;

int main()
{ unsigned hc = jthread::hardware_concurrency();
auto test = [&]( char const *head, auto fn, vector<double> &times )
{
cout << head << endl;
constexpr size_t ROUNDS = 10'000;
atomic_int64_t tSum, roundsSum;
vector<jthread> threads;
for( unsigned nThreads = 1; nThreads <= hc; ++nThreads )
{
auto st = []<typename T, integral X>( atomic<T> &a, X v ) { return
a.store( (T)v, memory_order_relaxed ); };
st( tSum, 0 );
st( roundsSum, 0 );
atomic_bool stop( false );
auto ld = []<typename T>( atomic<T> &a ) { return a.load(
memory_order_relaxed ); };
for( unsigned t = nThreads; t; --t )
threads.emplace_back( [&]
{
auto start = high_resolution_clock::now();
int64_t rounds = 0;
for( ; !ld( stop ); fn(), ++rounds );
auto add = []( atomic_int64_t &a, int64_t add ) { a.fetch_add(
add, memory_order_relaxed ); };
add( tSum, duration_cast<nanoseconds>(
high_resolution_clock::now() - start ).count() );
add( roundsSum, rounds );
} );
this_thread::sleep_for( 100ms );
st( stop, true );
threads.resize( 0 );
times.emplace_back( ld( tSum ) / (double)ld( roundsSum ) );
cout << "\t" << nThreads << ": " << times.back() << endl;
}
};
vector<double> mutexTimes, futexTimes;
{
mutex mtx;
test( "mutex:",
[&]
{
mtx.lock();
mtx.unlock();
}, mutexTimes );
}
{
atomic_bool futex( false );
test( "futex:",
[&]
{
for( bool cmp; !futex.compare_exchange_weak( cmp = false, true,
memory_order_acquire, memory_order_relaxed ); )
futex.wait( true, memory_order_relaxed );
futex.store( false, memory_order_release );
futex.notify_one();
}, futexTimes );
}
cout << "relation: " << endl;
for( size_t i = 0; i != hc; ++i )
cout << "\t" << i + 1 << ": " << (int)(100.0 * mutexTimes[i] /
futexTimes[i] + 0.5) << "%" << endl;
}

Re: C++20 futex with heavy contention slower than mutex

<uu9n0g$15fv8$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 30 Mar 2024 11:51:59 -0700
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <uu9n0g$15fv8$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$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, 30 Mar 2024 18:52:00 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e3ffe136872d6b59fa10d485f79c0803";
logging-data="1228776"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ToEfM6Y9MvE1nfXSXcbARgr812jj2IwE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:O0uSsT0YyfK+crYIMXHKBuy1ugM=
Content-Language: en-US
In-Reply-To: <uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Sat, 30 Mar 2024 18:51 UTC

On 3/30/2024 1:25 AM, Bonita Montero wrote:
> {
>                 for( bool cmp; !futex.compare_exchange_weak( cmp =
> false, true, memory_order_acquire, memory_order_relaxed ); )
>                     futex.wait( true, memory_order_relaxed );
>                 futex.store( false, memory_order_release );
>                 futex.notify_one();
>             }, futexTimes );

You need to introduce some sort of waitbit logic to minimize calls to
futex.notify_one() and futex.wait(). I posted an example but am having a
hard time finding the damn thing in comp.lang.c++. I will try to find it.

Re: C++20 futex with heavy contention slower than mutex

<uu9ngf$15kid$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 30 Mar 2024 12:00:31 -0700
Organization: A noiseless patient Spider
Lines: 144
Message-ID: <uu9ngf$15kid$1@dont-email.me>
References: <uu6eqs$ab8u$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, 30 Mar 2024 19:00:32 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e3ffe136872d6b59fa10d485f79c0803";
logging-data="1233485"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19oIoYuxGbIbDnEUMZe4TWJaF5HQmY+6vU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:c6W0kMWRr0K7G/+ZQPeFFQHi5tE=
Content-Language: en-US
In-Reply-To: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Sat, 30 Mar 2024 19:00 UTC

On 3/29/2024 6:14 AM, Bonita Montero wrote:
> The following program simulates constant locking und unlocking of one
> to jthread::hardware_concurrency() threadas with a std::mutex and a
> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
> 5 threads constantly contending, but beyond the CPU time of the futex
> explodes and the conventional mutex is faster with Windows as and with
> Linux.
[...]

I think I found my highly experimental windows futex based mutex:

https://groups.google.com/g/comp.lang.c++/c/1MZvhswJ6DQ/m/qyaYH-i0CgAJ

(read all to refresh the context... ;^)

Notice:
___________________
void unlock()
{ if (InterlockedExchange(&m_state, 0) == 2)
{
WakeByAddressSingle(&m_state);
}
} ___________________

We only signal when we have to!

copy and paste from the link. Sorry about the copy and paste destroying
all of my indentation, argh!

Can you run this Bonita? Fwiw, it would be my first time using a Windows
Futex! lol:
______________________________________
#include <iostream>
#include <thread>
#include <functional>

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#define CT_L2_ALIGNMENT 128
#define CT_THREADS 32
#define CT_ITERS 6666666

struct ct_futex_mutex
{ ULONG alignas(CT_L2_ALIGNMENT) m_state;

ct_futex_mutex() : m_state(0)
{

}

void lock()
{ if (InterlockedExchange(&m_state, 1))
{ while (InterlockedExchange(&m_state, 2))
{ ULONG cmp = 2;
WaitOnAddress(&m_state, &cmp, sizeof(ULONG), INFINITE);
} }
}

void unlock()
{ if (InterlockedExchange(&m_state, 0) == 2)
{ WakeByAddressSingle(&m_state);
} }
};

struct ct_shared
{ ct_futex_mutex m_mtx;
unsigned long m_count;

ct_shared() : m_count(0) {}

~ct_shared()
{ if (m_count != 0)
{ std::cout << "counter is totally fubar!\n";
} }
};

void ct_thread(ct_shared& shared)
{ for (unsigned long i = 0; i < CT_ITERS; ++i)
{ shared.m_mtx.lock();
++shared.m_count;
shared.m_mtx.unlock();

shared.m_mtx.lock();
--shared.m_count;
shared.m_mtx.unlock();
} }

int main()
{ std::thread threads[CT_THREADS];

std::cout << "Starting up...\n";

{
ct_shared shared;

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

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

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

std::cout << "Completed!\n";

return 0;
} ______________________________________

Re: C++20 futex with heavy contention slower than mutex

<uu9nk5$15fv8$2@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 30 Mar 2024 12:02:28 -0700
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <uu9nk5$15fv8$2@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 30 Mar 2024 19:02:29 +0100 (CET)
Injection-Info: dont-email.me; posting-host="e3ffe136872d6b59fa10d485f79c0803";
logging-data="1228776"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iXha+2+3Ys9rB+1M5lI8C2isdTNqCnWk="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:nDXdusUwXrXXqTB28cp4QyX9B2Q=
Content-Language: en-US
In-Reply-To: <uu9n0g$15fv8$1@dont-email.me>
 by: Chris M. Thomasson - Sat, 30 Mar 2024 19:02 UTC

On 3/30/2024 11:51 AM, Chris M. Thomasson wrote:
> On 3/30/2024 1:25 AM, Bonita Montero wrote:
>> {
>>                  for( bool cmp; !futex.compare_exchange_weak( cmp =
>> false, true, memory_order_acquire, memory_order_relaxed ); )
>>                      futex.wait( true, memory_order_relaxed );
>>                  futex.store( false, memory_order_release );
>>                  futex.notify_one();
>>              }, futexTimes );
>
>
> You need to introduce some sort of waitbit logic to minimize calls to
> futex.notify_one() and futex.wait(). I posted an example but am having a
> hard time finding the damn thing in comp.lang.c++. I will try to find it.

I found my older experimental code, take a deep look at ct_futex_mutex:

https://groups.google.com/g/comp.lang.c++/c/1MZvhswJ6DQ/m/qyaYH-i0CgAJ

Re: C++20 futex with heavy contention slower than mutex

<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 30 Mar 2024 21:18:06 +0100
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Mar 2024 20:18:06 +0100 (CET)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="f1d1aa16ce03f0b55eea9d3066b593e3";
logging-data="1272002"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18QL9cV+UirWKLR63jwMl9eLonA3fVDRDI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:qxKnV75222BVWMsmcCq5w5k2k7k=
Content-Language: de-DE
In-Reply-To: <uu9n0g$15fv8$1@dont-email.me>
 by: Bonita Montero - Sat, 30 Mar 2024 20:18 UTC

Am 30.03.2024 um 19:51 schrieb Chris M. Thomasson:

> You need to introduce some sort of waitbit logic to minimize calls to
> futex.notify_one() and futex.wait(). I posted an example but am having a
> hard time finding the damn thing in comp.lang.c++. I will try to find it.

Futex notify and wait are at least very cheap with Windows.

Re: C++20 futex with heavy contention slower than mutex

<uucg28$1vucq$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sun, 31 Mar 2024 13:11:52 -0700
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uucg28$1vucq$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$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, 31 Mar 2024 20:11:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="08ef37f04c264ba6cec9989a4cda6b29";
logging-data="2095514"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tKDLVDd7dvves3Y39wfiGGXbVs7BaoWg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:exFkPw5+5/zQV0mi72WIkoXsirQ=
Content-Language: en-US
In-Reply-To: <uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Sun, 31 Mar 2024 20:11 UTC

On 3/30/2024 1:18 PM, Bonita Montero wrote:
> Am 30.03.2024 um 19:51 schrieb Chris M. Thomasson:
>
>> You need to introduce some sort of waitbit logic to minimize calls to
>> futex.notify_one() and futex.wait(). I posted an example but am having
>> a hard time finding the damn thing in comp.lang.c++. I will try to
>> find it.
>
> Futex notify and wait are at least very cheap with Windows.
>

Well, I have not taken a look at Microsoft's Futex implementation.
Afaict, your code showcases that aspect wrt calling into notify on every
mutex unlock, and wait on every point of contention wrt lock. Interesting.

Re: C++20 futex with heavy contention slower than mutex

<uudf43$2aa29$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sun, 31 Mar 2024 22:01:55 -0700
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <uudf43$2aa29$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 01 Apr 2024 05:01:55 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="780895c2b46351ba3de900b0ef1e45f3";
logging-data="2435145"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PK4fz4doi/LVGV5Wyhc9OiDaw201BCcY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:83iDWaVrf16zXp1KPhZKROLccUU=
In-Reply-To: <uu9n0g$15fv8$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 1 Apr 2024 05:01 UTC

On 3/30/2024 11:51 AM, Chris M. Thomasson wrote:
> On 3/30/2024 1:25 AM, Bonita Montero wrote:
>> {
>>                  for( bool cmp; !futex.compare_exchange_weak( cmp =
>> false, true, memory_order_acquire, memory_order_relaxed ); )
>>                      futex.wait( true, memory_order_relaxed );
>>                  futex.store( false, memory_order_release );
>>                  futex.notify_one();
>>              }, futexTimes );
>
>
> You need to introduce some sort of waitbit logic to minimize calls to
> futex.notify_one() and futex.wait(). I posted an example but am having a
> hard time finding the damn thing in comp.lang.c++. I will try to find it.

Also, does notify_one automatically imply release semantics?

Re: C++20 futex with heavy contention slower than mutex

<uudf50$2aa29$2@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sun, 31 Mar 2024 22:02:23 -0700
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <uudf50$2aa29$2@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me> <uudf43$2aa29$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 01 Apr 2024 05:02:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="780895c2b46351ba3de900b0ef1e45f3";
logging-data="2435145"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dxmsolGjb2RSq8JHEGgE/1cR3JjzgCY4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:8ljc+CCr4NSRmGbnPmhPuJleCKA=
In-Reply-To: <uudf43$2aa29$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 1 Apr 2024 05:02 UTC

On 3/31/2024 10:01 PM, Chris M. Thomasson wrote:
> On 3/30/2024 11:51 AM, Chris M. Thomasson wrote:
>> On 3/30/2024 1:25 AM, Bonita Montero wrote:
>>> {
>>>                  for( bool cmp; !futex.compare_exchange_weak( cmp =
>>> false, true, memory_order_acquire, memory_order_relaxed ); )
>>>                      futex.wait( true, memory_order_relaxed );
>>>                  futex.store( false, memory_order_release );
>>>                  futex.notify_one();
>>>              }, futexTimes );
>>
>>
>> You need to introduce some sort of waitbit logic to minimize calls to
>> futex.notify_one() and futex.wait(). I posted an example but am having
>> a hard time finding the damn thing in comp.lang.c++. I will try to
>> find it.
>
> Also, does notify_one automatically imply release semantics?

I don't think so, humm. Cannot remember.

Re: C++20 futex with heavy contention slower than mutex

<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Mon, 1 Apr 2024 08:32:08 +0200
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 01 Apr 2024 06:32:08 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="2270fc823eb0358f5701f1b843284b82";
logging-data="2468888"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18YSznYA4mfqUR1xEkEUDj3ox243g5sijc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:4c7ALj1LTcQK/yqIeKYXw8x9LoI=
Content-Language: de-DE
In-Reply-To: <uucg28$1vucq$1@dont-email.me>
 by: Bonita Montero - Mon, 1 Apr 2024 06:32 UTC

Am 31.03.2024 um 22:11 schrieb Chris M. Thomasson:

> Well, I have not taken a look at Microsoft's Futex implementation.
> Afaict, your code showcases that aspect wrt calling into notify on every
> mutex unlock, and wait on every point of contention wrt lock. Interesting.

I'm using notify and wait only when there's contention.

Re: C++20 futex with heavy contention slower than mutex

<uudkss$2bcg5$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sun, 31 Mar 2024 23:40:28 -0700
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <uudkss$2bcg5$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$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: Mon, 01 Apr 2024 06:40:29 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="780895c2b46351ba3de900b0ef1e45f3";
logging-data="2470405"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2j+UPzcMi0PpKLXgsAieAzkgSOfzN7Dk="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:FwzRmBXEKnpyF8sBOQr4WJUHAlc=
Content-Language: en-US
In-Reply-To: <uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Mon, 1 Apr 2024 06:40 UTC

On 3/31/2024 11:32 PM, Bonita Montero wrote:
> Am 31.03.2024 um 22:11 schrieb Chris M. Thomasson:
>
>> Well, I have not taken a look at Microsoft's Futex implementation.
>> Afaict, your code showcases that aspect wrt calling into notify on
>> every mutex unlock, and wait on every point of contention wrt lock.
>> Interesting.
>
> I'm using notify and wait only when there's contention.

Even here?
_____________
for( bool cmp; !futex.compare_exchange_weak( cmp =
false, true, memory_order_acquire, memory_order_relaxed ); )
futex.wait( true, memory_order_relaxed );
futex.store( false, memory_order_release );
futex.notify_one();
}, futexTimes );
____________

What am I missing?

futex.store( false, memory_order_release );
futex.notify_one();

?

Re: C++20 futex with heavy contention slower than mutex

<uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Mon, 1 Apr 2024 10:57:32 +0200
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
<uudkss$2bcg5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 01 Apr 2024 08:57:31 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="2270fc823eb0358f5701f1b843284b82";
logging-data="2528921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+foe2wieL+P7QbeCZNyhWLdzVFgB2diqs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:t1pFK3mSw8DLzRWdxm+EOrFR2Ok=
Content-Language: de-DE
In-Reply-To: <uudkss$2bcg5$1@dont-email.me>
 by: Bonita Montero - Mon, 1 Apr 2024 08:57 UTC

Am 01.04.2024 um 08:40 schrieb Chris M. Thomasson:
> On 3/31/2024 11:32 PM, Bonita Montero wrote:
>> Am 31.03.2024 um 22:11 schrieb Chris M. Thomasson:
>>
>>> Well, I have not taken a look at Microsoft's Futex implementation.
>>> Afaict, your code showcases that aspect wrt calling into notify on
>>> every mutex unlock, and wait on every point of contention wrt lock.
>>> Interesting.
>>
>> I'm using notify and wait only when there's contention.
>
> Even here?
> _____________
>                  for( bool cmp; !futex.compare_exchange_weak( cmp =
> false, true, memory_order_acquire, memory_order_relaxed ); )
>                      futex.wait( true, memory_order_relaxed );
>                  futex.store( false, memory_order_release );
>                  futex.notify_one();
>              }, futexTimes );
> ____________
>
> What am I missing?
>
>                  futex.store( false, memory_order_release );
>                  futex.notify_one();
>
> ?
>

Ok, I was talking about my older version.
But notify is fast.

Re: C++20 futex with heavy contention slower than mutex

<uudt4f$2d7ic$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Mon, 1 Apr 2024 11:01:05 +0200
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <uudt4f$2d7ic$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
<uudkss$2bcg5$1@dont-email.me>
<uudstq$2d5kp$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: Mon, 01 Apr 2024 09:01:04 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="2270fc823eb0358f5701f1b843284b82";
logging-data="2530892"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cWn09irrngP6UsZ2X9WLcWoAA3N6Fwoo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:PnTNwoD1OfoPU6tTJVExyLnlSEs=
Content-Language: de-DE
In-Reply-To: <uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>
 by: Bonita Montero - Mon, 1 Apr 2024 09:01 UTC

Am 01.04.2024 um 10:57 schrieb Bonita Montero:
> Am 01.04.2024 um 08:40 schrieb Chris M. Thomasson:
>> On 3/31/2024 11:32 PM, Bonita Montero wrote:
>>> Am 31.03.2024 um 22:11 schrieb Chris M. Thomasson:
>>>
>>>> Well, I have not taken a look at Microsoft's Futex implementation.
>>>> Afaict, your code showcases that aspect wrt calling into notify on
>>>> every mutex unlock, and wait on every point of contention wrt lock.
>>>> Interesting.
>>>
>>> I'm using notify and wait only when there's contention.
>>
>> Even here?
>> _____________
>>                   for( bool cmp; !futex.compare_exchange_weak( cmp =
>> false, true, memory_order_acquire, memory_order_relaxed ); )
>>                       futex.wait( true, memory_order_relaxed );
>>                   futex.store( false, memory_order_release );
>>                   futex.notify_one();
>>               }, futexTimes );
>> ____________
>>
>> What am I missing?
>>
>>                   futex.store( false, memory_order_release );
>>                   futex.notify_one();
>>
>> ?
>>
>
> Ok, I was talking about my older version.
> But notify is fast.
>

An uncontended notify is 2.5ns on my Zen4 computer.

Re: C++20 futex with heavy contention slower than mutex

<20240401121800.00000d25@yahoo.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Mon, 1 Apr 2024 12:18:00 +0300
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <20240401121800.00000d25@yahoo.com>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 01 Apr 2024 09:18:09 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="62e7d7e668fcd9339a571b60585e0c58";
logging-data="1675778"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19y7KizSD8MCovqjonRA6kW7W1HKEFuCB8="
Cancel-Lock: sha1:UuWqBV1oWVnD1GrV2EHyqu/53bI=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
 by: Michael S - Mon, 1 Apr 2024 09:18 UTC

On Fri, 29 Mar 2024 14:14:11 +0100
Bonita Montero <Bonita.Montero@gmail.com> wrote:

> The following program simulates constant locking und unlocking of one
> to jthread::hardware_concurrency() threadas with a std::mutex and a
> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
> 5 threads constantly contending, but beyond the CPU time of the futex
> explodes and the conventional mutex is faster with Windows as and with
> Linux.
>

In case of heavy contention what to consider 'faster' is not at all
obvious.

On lightly loaded system with more cores than work to do (a typical
client) 'faster' means faster forward progress of group of contending
threads. Achieved by very long polling before switching to wait,
probably up to several tens of usec and by hyperactive tickless OS
scheduler.

On heavily loaded system with much more work to do than available
cores, 'faster' means more work done by unrelated threads and processes.
Achieved by very short polling before switching to wait, probably less
than 500 nsec and by 'passive' OS scheduler that rarely intervenes
outside of clock tick.

And of course there are cases in the middle.

And then traditional HPC with MPI that is completely different kettle of
fish.

Re: C++20 futex with heavy contention slower than mutex

<uuf1r3$2lqho$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Mon, 1 Apr 2024 12:27:31 -0700
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <uuf1r3$2lqho$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
<uudkss$2bcg5$1@dont-email.me>
<uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>
<uudt4f$2d7ic$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: Mon, 01 Apr 2024 19:27:31 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="780895c2b46351ba3de900b0ef1e45f3";
logging-data="2812472"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bcXvdKpDc+XmfCldjmwlYGN57Xwh30pw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:R4JgYTXGNMXQYKYfITcDAUhF7+U=
In-Reply-To: <uudt4f$2d7ic$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Mon, 1 Apr 2024 19:27 UTC

On 4/1/2024 2:01 AM, Bonita Montero wrote:
> Am 01.04.2024 um 10:57 schrieb Bonita Montero:
>> Am 01.04.2024 um 08:40 schrieb Chris M. Thomasson:
>>> On 3/31/2024 11:32 PM, Bonita Montero wrote:
>>>> Am 31.03.2024 um 22:11 schrieb Chris M. Thomasson:
>>>>
>>>>> Well, I have not taken a look at Microsoft's Futex implementation.
>>>>> Afaict, your code showcases that aspect wrt calling into notify on
>>>>> every mutex unlock, and wait on every point of contention wrt lock.
>>>>> Interesting.
>>>>
>>>> I'm using notify and wait only when there's contention.
>>>
>>> Even here?
>>> _____________
>>>                   for( bool cmp; !futex.compare_exchange_weak( cmp =
>>> false, true, memory_order_acquire, memory_order_relaxed ); )
>>>                       futex.wait( true, memory_order_relaxed );
>>>                   futex.store( false, memory_order_release );
>>>                   futex.notify_one();
>>>               }, futexTimes );
>>> ____________
>>>
>>> What am I missing?
>>>
>>>                   futex.store( false, memory_order_release );
>>>                   futex.notify_one();
>>>
>>> ?
>>>
>>
>> Ok, I was talking about my older version.
>> But notify is fast.
>>
>
> An uncontended notify is 2.5ns on my Zen4 computer.
>

For the Microsoft version, right? I don't have Linux installed at the
moment.

Re: C++20 futex with heavy contention slower than mutex

<uufr1r$2v8qo$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  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.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Tue, 2 Apr 2024 04:37:47 +0200
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <uufr1r$2v8qo$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
<uudkss$2bcg5$1@dont-email.me>
<uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>
<uudt4f$2d7ic$1@raubtier-asyl.eternal-september.org>
<uuf1r3$2lqho$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 02 Apr 2024 02:37:47 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="016070494e2eb6ffb8d820e0bbd4267b";
logging-data="3122008"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KM/+cto/CoNHfcivGI6ZsS2+FnUr3Dr0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:N6VhCtyVDoyi6m5oUKNdJPCzUOI=
In-Reply-To: <uuf1r3$2lqho$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Tue, 2 Apr 2024 02:37 UTC

Am 01.04.2024 um 21:27 schrieb Chris M. Thomasson:
> On 4/1/2024 2:01 AM, Bonita Montero wrote:
>> Am 01.04.2024 um 10:57 schrieb Bonita Montero:
>>> Am 01.04.2024 um 08:40 schrieb Chris M. Thomasson:
>>>> On 3/31/2024 11:32 PM, Bonita Montero wrote:
>>>>> Am 31.03.2024 um 22:11 schrieb Chris M. Thomasson:
>>>>>
>>>>>> Well, I have not taken a look at Microsoft's Futex implementation.
>>>>>> Afaict, your code showcases that aspect wrt calling into notify on
>>>>>> every mutex unlock, and wait on every point of contention wrt
>>>>>> lock. Interesting.
>>>>>
>>>>> I'm using notify and wait only when there's contention.
>>>>
>>>> Even here?
>>>> _____________
>>>>                   for( bool cmp; !futex.compare_exchange_weak( cmp =
>>>> false, true, memory_order_acquire, memory_order_relaxed ); )
>>>>                       futex.wait( true, memory_order_relaxed );
>>>>                   futex.store( false, memory_order_release );
>>>>                   futex.notify_one();
>>>>               }, futexTimes );
>>>> ____________
>>>>
>>>> What am I missing?
>>>>
>>>>                   futex.store( false, memory_order_release );
>>>>                   futex.notify_one();
>>>>
>>>> ?
>>>>
>>>
>>> Ok, I was talking about my older version.
>>> But notify is fast.
>>>
>>
>> An uncontended notify is 2.5ns on my Zen4 computer.
>>
>
> For the Microsoft version, right? I don't have Linux installed at the
> moment.

Linux with libstdc++ is 1.7ns.

Re: C++20 futex with heavy contention slower than mutex

<uunp8e$14n67$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Thu, 4 Apr 2024 19:56:13 -0700
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <uunp8e$14n67$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<20240401121800.00000d25@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 05 Apr 2024 02:56:14 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="1a74e58d95f7f0faf9e73d2935d6e0e5";
logging-data="1203399"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/r+889k4tDv2KJkeYeD/DdWH2zVMtIg8g="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:8Y0R9tKyKzYl0YehvRFU5Y3HnqY=
In-Reply-To: <20240401121800.00000d25@yahoo.com>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 5 Apr 2024 02:56 UTC

On 4/1/2024 2:18 AM, Michael S wrote:
> On Fri, 29 Mar 2024 14:14:11 +0100
> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>
>> The following program simulates constant locking und unlocking of one
>> to jthread::hardware_concurrency() threadas with a std::mutex and a
>> futex. On my 16 core / 32 thread Zen4 system a futex is faster up to
>> 5 threads constantly contending, but beyond the CPU time of the futex
>> explodes and the conventional mutex is faster with Windows as and with
>> Linux.
>>
>
> In case of heavy contention what to consider 'faster' is not at all
> obvious.
>
> On lightly loaded system with more cores than work to do (a typical
> client) 'faster' means faster forward progress of group of contending
> threads. Achieved by very long polling before switching to wait,
> probably up to several tens of usec and by hyperactive tickless OS
> scheduler.
>
> On heavily loaded system with much more work to do than available
> cores, 'faster' means more work done by unrelated threads and processes.
> Achieved by very short polling before switching to wait, probably less
> than 500 nsec and by 'passive' OS scheduler that rarely intervenes
> outside of clock tick.
>
> And of course there are cases in the middle.
>
> And then traditional HPC with MPI that is completely different kettle of
> fish.
>

Ditto. However, I did find it interesting (20+ years ago) to test worst
case scenarios. What happens when I blast this mutex algorithm A with
artificial load vs algo B forcing it into horrible periods of radical
contention. How do they handle it...

Re: C++20 futex with heavy contention slower than mutex

<uvc8c4$2ik6i$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Fri, 12 Apr 2024 14:16:52 -0700
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <uvc8c4$2ik6i$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
<uudkss$2bcg5$1@dont-email.me>
<uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>
<uudt4f$2d7ic$1@raubtier-asyl.eternal-september.org>
<uuf1r3$2lqho$1@dont-email.me>
<uufr1r$2v8qo$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: Fri, 12 Apr 2024 23:16:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="580845cba5e816a4d21fffa29c63ed62";
logging-data="2707666"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/K8ZXzmm1MJI114xEW3mmG1D7IBWLhBCY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:p4IfbBaXdvwirR/6lEFTANZseUk=
In-Reply-To: <uufr1r$2v8qo$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 12 Apr 2024 21:16 UTC

On 4/1/2024 7:37 PM, Bonita Montero wrote:
[...]
>>>> Ok, I was talking about my older version.
>>>> But notify is fast.
>>>>
>>>
>>> An uncontended notify is 2.5ns on my Zen4 computer.

Keep in mind that notify and wait are slow paths. One needs to strive to
minimize calls to notify and wait. Calling notify and/or wait when they
do _have_ to be called is a rather inefficient design.

>>>
>>
>> For the Microsoft version, right? I don't have Linux installed at the
>> moment.
>
> Linux with libstdc++ is 1.7ns.
>

Re: C++20 futex with heavy contention slower than mutex

<uvclf0$2l4bk$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!newsfeed.bofh.team!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.M...@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 13 Apr 2024 03:00:21 +0200
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <uvclf0$2l4bk$1@raubtier-asyl.eternal-september.org>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
<uudkss$2bcg5$1@dont-email.me>
<uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>
<uudt4f$2d7ic$1@raubtier-asyl.eternal-september.org>
<uuf1r3$2lqho$1@dont-email.me>
<uufr1r$2v8qo$1@raubtier-asyl.eternal-september.org>
<uvc8c4$2ik6i$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 13 Apr 2024 03:00:17 +0200 (CEST)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="5b8b4e6e680f8ea6f68dc8c065d99a78";
logging-data="2789748"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19iaKruGjd7sDBX1hPr/TXNUTAALv7DWz8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:MDwFpG3QkijKcoWFRj2mD6EKKKU=
Content-Language: de-DE
In-Reply-To: <uvc8c4$2ik6i$1@dont-email.me>
 by: Bonita Montero - Sat, 13 Apr 2024 01:00 UTC

Am 12.04.2024 um 23:16 schrieb Chris M. Thomasson:
> On 4/1/2024 7:37 PM, Bonita Montero wrote:
> [...]
>>>>> Ok, I was talking about my older version.
>>>>> But notify is fast.
>>>>>
>>>>
>>>> An uncontended notify is 2.5ns on my Zen4 computer.
>
> Keep in mind that notify and wait are slow paths.

There's no slow path with futexes since you've to notify _always_.
And 1.7ns isn't slow.

> One needs to strive to minimize calls to notify and wait. Calling notify and/or wait when they
> do _have_ to be called is a rather inefficient design.
>
>
>>>>
>>>
>>> For the Microsoft version, right? I don't have Linux installed at the
>>> moment.
>>
>> Linux with libstdc++ is 1.7ns.
>>
>

Re: C++20 futex with heavy contention slower than mutex

<uvem9v$35hth$1@dont-email.me>

  copy mid

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

  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....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: C++20 futex with heavy contention slower than mutex
Date: Sat, 13 Apr 2024 12:26:55 -0700
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <uvem9v$35hth$1@dont-email.me>
References: <uu6eqs$ab8u$1@raubtier-asyl.eternal-september.org>
<uu7fa1$i04u$2@dont-email.me> <uu7feo$i04u$3@dont-email.me>
<uu7it7$iqqi$1@dont-email.me>
<uu8i9k$sqsp$1@raubtier-asyl.eternal-september.org>
<uu9n0g$15fv8$1@dont-email.me>
<uu9s1u$16q62$1@raubtier-asyl.eternal-september.org>
<uucg28$1vucq$1@dont-email.me>
<uudkd7$2bb0o$1@raubtier-asyl.eternal-september.org>
<uudkss$2bcg5$1@dont-email.me>
<uudstq$2d5kp$1@raubtier-asyl.eternal-september.org>
<uudt4f$2d7ic$1@raubtier-asyl.eternal-september.org>
<uuf1r3$2lqho$1@dont-email.me>
<uufr1r$2v8qo$1@raubtier-asyl.eternal-september.org>
<uvc8c4$2ik6i$1@dont-email.me>
<uvclf0$2l4bk$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, 13 Apr 2024 21:26:56 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a578e7d9e82dc2c787ca41f74c01f148";
logging-data="3327921"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/m8Jc/1JvMChLnGiXfYvckndcWpYaeSUc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:pvduMHMpeRk2d7fRojuFIHf44Ho=
In-Reply-To: <uvclf0$2l4bk$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 13 Apr 2024 19:26 UTC

On 4/12/2024 6:00 PM, Bonita Montero wrote:
> Am 12.04.2024 um 23:16 schrieb Chris M. Thomasson:
>> On 4/1/2024 7:37 PM, Bonita Montero wrote:
>> [...]
>>>>>> Ok, I was talking about my older version.
>>>>>> But notify is fast.
>>>>>>
>>>>>
>>>>> An uncontended notify is 2.5ns on my Zen4 computer.
>>
>> Keep in mind that notify and wait are slow paths.
>
> There's no slow path with futexes since you've to notify _always_.

Huh? Nope! Where did you get that idea from? calling notify is a slow
path, and calling wait is a slow path.

> And 1.7ns isn't slow.

You do not want to _always_ call notify and/or wait when you do not have to.

>
>> One needs to strive to  minimize calls to notify and wait. Calling
>> notify and/or wait when they do _have_ to be called is a rather
>> inefficient design.

>>
>>
>>>>>
>>>>
>>>> For the Microsoft version, right? I don't have Linux installed at
>>>> the moment.
>>>
>>> Linux with libstdc++ is 1.7ns.
>>>
>>
>


devel / comp.lang.c++ / C++20 futex with heavy contention slower than mutex

Pages:123
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor