Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

In space, no one can hear you fart.


devel / comp.lang.c++ / Re: Why volatile may make sense for parallel code today.

SubjectAuthor
* Why volatile may make sense for parallel code today.Bonita Montero
+* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
|+* Re: Why volatile may make sense for parallel code today.red floyd
||`* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
|| +* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
|| |`- Re: Why volatile may make sense for parallel code today.Marcel Mueller
|| +* Re: Why volatile may make sense for parallel code today.David Brown
|| |+- Re: Why volatile may make sense for parallel code today.Bonita Montero
|| |`* Re: Why volatile may make sense for parallel code today.Scott Lurndal
|| | `- Re: Why volatile may make sense for parallel code today.David Brown
|| `* Re: Why volatile may make sense for parallel code today.Richard Damon
||  `* Re: Why volatile may make sense for parallel code today.Bonita Montero
||   +* Re: Why volatile may make sense for parallel code today.Richard Damon
||   |+* Re: Why volatile may make sense for parallel code today.Bonita Montero
||   ||`* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
||   || `* Re: Why volatile may make sense for parallel code today.Bonita Montero
||   ||  `* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
||   ||   `- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
||   |`- Re: Why volatile may make sense for parallel code today.David Brown
||   +* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
||   |+- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
||   |`* Re: Why volatile may make sense for parallel code today.Bonita Montero
||   | `* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
||   |  `- Re: Why volatile may make sense for parallel code today.Bonita Montero
||   `* Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
||    `- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
|`* Re: Why volatile may make sense for parallel code today.Bonita Montero
| +* Re: Why volatile may make sense for parallel code today.Kaz Kylheku
| |`* Re: Why volatile may make sense for parallel code today.Bonita Montero
| | `- Re: Why volatile may make sense for parallel code today.Bonita Montero
| `* Re: Why volatile may make sense for parallel code today.Scott Lurndal
|  +* Re: Why volatile may make sense for parallel code today.Bonita Montero
|  |+- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
|  |`- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
|  `- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
`* Re: Why volatile may make sense for parallel code today.Kaz Kylheku
 `* Re: Why volatile may make sense for parallel code today.Bonita Montero
  +- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
  +- Re: Why volatile may make sense for parallel code today.David Brown
  `* Re: Why volatile may make sense for parallel code today.Kaz Kylheku
   +- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson
   `* Re: Why volatile may make sense for parallel code today.Bonita Montero
    `- Re: Why volatile may make sense for parallel code today.Chris M. Thomasson

Pages:12
Re: Why volatile may make sense for parallel code today.

<ujpkq5$28aoq$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 08:53:49 +0100
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <ujpkq5$28aoq$1@raubtier-asyl.eternal-september.org>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujpefk$27eto$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 07:53:41 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="9ade70e49ca68ba2beb5a7fae8a1ed12";
logging-data="2370330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jxnaOS9VjdmFFVBfGjU5rdq9L9Qrqo8U="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kbD6GXej7IqxMVVo8zZ6E2YmcEo=
Content-Language: de-DE
In-Reply-To: <ujpefk$27eto$2@dont-email.me>
 by: Bonita Montero - Fri, 24 Nov 2023 07:53 UTC

Am 24.11.2023 um 07:05 schrieb Chris M. Thomasson:

> humm.. Say, the read is from a word in memory. Define your trivial
> object, POD, l2 cache line sized, and aligned on a l2 cache line
> boundary? Are you refering to how certain arch works?

Read that:
https://stackoverflow.com/questions/61329240/what-is-the-difference-between-trivial-and-non-trivial-objects

Re: Why volatile may make sense for parallel code today.

<ujpl1a$28aoq$3@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!usenet.network!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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 08:57:38 +0100
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <ujpl1a$28aoq$3@raubtier-asyl.eternal-september.org>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujo3dr$1p192$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 07:57:30 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="9ade70e49ca68ba2beb5a7fae8a1ed12";
logging-data="2370330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FUlefUu8D98HXVCH7COkPmwdEJXKOIPg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:8jkMa136IcBhlBUVBo2L5/qIqQU=
In-Reply-To: <ujo3dr$1p192$1@i2pn2.org>
Content-Language: de-DE
 by: Bonita Montero - Fri, 24 Nov 2023 07:57 UTC

Am 23.11.2023 um 18:50 schrieb Richard Damon:

> Yes, the atomic itself doesn't "cache" the data, but as far as I read,
> there is no requirement to refetch the data if the code still has the
> old value around, and it hasn't been invalidated by possible memory
> ordering.

I don't believe that, think about an atomic flag that is periodically
polled. The compiler shouldn't cache that value.

Re: Why volatile may make sense for parallel code today.

<ujpm48$28gcu$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 00:16:07 -0800
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <ujpm48$28gcu$1@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujo3dr$1p192$1@i2pn2.org>
<ujpl1a$28aoq$3@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, 24 Nov 2023 08:16:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7664f6b07543b36eac45e26755f90876";
logging-data="2376094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SfHmr9uIdmesRRneXfiB10QIRjuV/Jp4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:pjta4VmakhRQKgFnCiwArq+J6VY=
Content-Language: en-US
In-Reply-To: <ujpl1a$28aoq$3@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Fri, 24 Nov 2023 08:16 UTC

On 11/23/2023 11:57 PM, Bonita Montero wrote:
> Am 23.11.2023 um 18:50 schrieb Richard Damon:
>
>> Yes, the atomic itself doesn't "cache" the data, but as far as I read,
>> there is no requirement to refetch the data if the code still has the
>> old value around, and it hasn't been invalidated by possible memory
>> ordering.
>
> I don't believe that, think about an atomic flag that is periodically
> polled. The compiler shouldn't cache that value.
>
>

std::atomic is going to work for such a flag. Depending on your setup,
it should be using std::memory_order_relaxed for the polling.

Re: Why volatile may make sense for parallel code today.

<ujpmue$28j0j$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 09:30:15 +0100
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <ujpmue$28j0j$1@raubtier-asyl.eternal-september.org>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujo3dr$1p192$1@i2pn2.org>
<ujpl1a$28aoq$3@raubtier-asyl.eternal-september.org>
<ujpm48$28gcu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 08:30:06 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="9ade70e49ca68ba2beb5a7fae8a1ed12";
logging-data="2378771"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Dq6gGlyfumwz7rpFqZw1zQSVXuF+UVWQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:WtUtpUlVWYT0yEysvO91s5s7f8s=
Content-Language: de-DE
In-Reply-To: <ujpm48$28gcu$1@dont-email.me>
 by: Bonita Montero - Fri, 24 Nov 2023 08:30 UTC

Am 24.11.2023 um 09:16 schrieb Chris M. Thomasson:

> std::atomic is going to work for such a flag. Depending on your
> setup, it should be using std::memory_order_relaxed for the polling.

There's also atomic_flag, but it has some limitations over atomic_bool
that I've never used it. You can set it only in conjunction with an
atomic read and I never had a use for that. And this relies on a atomic
exchange, which costs a lot more than just a byte write.

Re: Why volatile may make sense for parallel code today.

<ujpovj$28srl$1@dont-email.me>

  copy mid

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

  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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 01:04:50 -0800
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <ujpovj$28srl$1@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujo3dr$1p192$1@i2pn2.org>
<ujpl1a$28aoq$3@raubtier-asyl.eternal-september.org>
<ujpm48$28gcu$1@dont-email.me>
<ujpmue$28j0j$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, 24 Nov 2023 09:04:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7664f6b07543b36eac45e26755f90876";
logging-data="2388853"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+raDp+1KrIH1Uox46khfW/M/CIYvuPVDs="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kn+Ag/dsrfyI68Lw91BP8xg5igs=
In-Reply-To: <ujpmue$28j0j$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 24 Nov 2023 09:04 UTC

On 11/24/2023 12:30 AM, Bonita Montero wrote:
> Am 24.11.2023 um 09:16 schrieb Chris M. Thomasson:
>
>> std::atomic is going to work for such a flag. Depending on your
>> setup, it should be using std::memory_order_relaxed for the polling.
>
> There's also atomic_flag, but it has some limitations over atomic_bool
> that I've never used it. You can set it only in conjunction with an
> atomic read and I never had a use for that. And this relies on a atomic
> exchange, which costs a lot more than just a byte write.

Fwiw, this flag should be aligned on a l2 cache line boundary, and
padded up to a l2 cache line size.

Re: Why volatile may make sense for parallel code today.

<ujpp19$28srl$2@dont-email.me>

  copy mid

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

  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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 01:05:44 -0800
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <ujpp19$28srl$2@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujo3dr$1p192$1@i2pn2.org>
<ujpl1a$28aoq$3@raubtier-asyl.eternal-september.org>
<ujpm48$28gcu$1@dont-email.me>
<ujpmue$28j0j$1@raubtier-asyl.eternal-september.org>
<ujpovj$28srl$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 09:05:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7664f6b07543b36eac45e26755f90876";
logging-data="2388853"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yupOoR6JRs0s9PSQXgz3KHPf795NNhZU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:OvbBqg4g9+jwwAOfTe8NXpPxj2k=
In-Reply-To: <ujpovj$28srl$1@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 24 Nov 2023 09:05 UTC

On 11/24/2023 1:04 AM, Chris M. Thomasson wrote:
> On 11/24/2023 12:30 AM, Bonita Montero wrote:
>> Am 24.11.2023 um 09:16 schrieb Chris M. Thomasson:
>>
>>> std::atomic is going to work for such a flag. Depending on your
>>> setup, it should be using std::memory_order_relaxed for the polling.
>>
>> There's also atomic_flag, but it has some limitations over atomic_bool
>> that I've never used it. You can set it only in conjunction with an
>> atomic read and I never had a use for that. And this relies on a atomic
>> exchange, which costs a lot more than just a byte write.
>
> Fwiw, this flag should be aligned on a l2 cache line boundary, and
> padded up to a l2 cache line size.

You can stuff a cache line with words, as long as you do not straddle a
cache line boundary... YIKES!

Re: Why volatile may make sense for parallel code today.

<ujpp38$28srl$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 01:06:47 -0800
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <ujpp38$28srl$3@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujpefk$27eto$2@dont-email.me>
<ujpkq5$28aoq$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, 24 Nov 2023 09:06:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7664f6b07543b36eac45e26755f90876";
logging-data="2388853"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/o5pMXFmFb2r0nP7ic9gVLu7ID28yvjRA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:N2DVzJHMHtrecCpupmlrBNog9CY=
In-Reply-To: <ujpkq5$28aoq$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 24 Nov 2023 09:06 UTC

On 11/23/2023 11:53 PM, Bonita Montero wrote:
> Am 24.11.2023 um 07:05 schrieb Chris M. Thomasson:
>
>> humm.. Say, the read is from a word in memory. Define your trivial
>> object, POD, l2 cache line sized, and aligned on a l2 cache line
>> boundary? Are you refering to how certain arch works?
>
> Read that:
> https://stackoverflow.com/questions/61329240/what-is-the-difference-between-trivial-and-non-trivial-objects

I know. Btw, what the hell happened to std::is_pod? ;^)

Re: Why volatile may make sense for parallel code today.

<ujpp6k$28uia$1@dont-email.me>

  copy mid

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

  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: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 10:08:35 +0100
Organization: A noiseless patient Spider
Lines: 74
Message-ID: <ujpp6k$28uia$1@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujn4g3$1pevs$1@dont-email.me>
<P6O7N.94356$BbXa.42252@fx16.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 24 Nov 2023 09:08:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="472d6a28bc35cabbeb190303a479b5eb";
logging-data="2390602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19lqouIptEmPtVYBgbyWMzDEPfA2oFd004="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:oD14AnhycMyHt2VnLfZZS0whB7c=
In-Reply-To: <P6O7N.94356$BbXa.42252@fx16.iad>
Content-Language: en-GB
 by: David Brown - Fri, 24 Nov 2023 09:08 UTC

On 23/11/2023 20:55, Scott Lurndal wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 23/11/2023 06:05, Chris M. Thomasson wrote:
>>> On 11/22/2023 4:17 PM, red floyd wrote:
>>>> On 11/22/2023 1:22 PM, Chris M. Thomasson wrote:
>>>>> On 11/22/2023 8:35 AM, Bonita Montero wrote:
>>>>>> #include <Windows.h>
>>>>>> #include <thread>
>>>>>>
>>>>>> using namespace std;
>>>>>>
>>>>>> int main()
>>>>>> {
>>>>>>      constexpr size_t ROUNDS = 1'000'000;
>>>>>>      size_t volatile r = 1'000'000;
>>>>>>      jthread thr( [&]()
>>>>>>          {
>>>>>>              while( r )
>>>>>>                  SleepEx( INFINITE, TRUE );
>>>>>>          } );
>>>>>>      for( size_t r = ROUNDS; r--; )
>>>>>>          QueueUserAPC( (PAPCFUNC)[]( auto p ) { --*(size_t*)p; },
>>>>>> thr.native_handle(), (ULONG_PTR)&r );
>>>>>> }
>>>>>
>>>>> std::atomic<size_t> r
>>>>>
>>>>
>>>> I'm confused.  Does std:atomic imply "do not optimize access to this
>>>> variable"?  Because if it doesn't, then I can see how the "while (r)"
>>>> loop can just spin.
>>>>
>>>>
>>>
>>> std::atomic should honor a read, when you read it even from
>>> std::memory_order_relaxed. If not, imvvvvhhooo, its broken?
>>
>>
>> You will probably find that compilers in practice will re-read "r" each
>> round of the loop, regardless of the memory order. I am not convinced
>> this would be required for "relaxed", but compilers generally do not
>> optimise atomics as much as they are allowed to. They are, as far as I
>> have seen in my far from comprehensive testing, treated as though
>> "atomic" implied "volatile".
>
> Linux tends to apply the volatile qualifier on the access, rather
> than the definition.
>
> #define ACCESS_ONCE(x) (*(volatile __typeof__(x) *)&(x))
>
> while (ACCESS_ONCE(r)) {
> }
>
> Makes it rather obvious when reading the code what the intent
> is, and won't be affected of someone accidentially removes the
> volatile qualifier from the declaration of r.
>
> Works just fine in c++, too.

That is often my preference too, since it is the access that is
"volatile" - a "volatile object" is simply one for which all accesses
are "volatile".

For the pedants, it might be worth noting that the "cast to pointer to
volatile" technique of ACCESS_ONCE is not actually guaranteed to be
treated as a volatile access in C until C17/C18 when the wording was
changed to talk about accesses via "volatile lvalues" rather than
accesses to objects declared as volatile. (When the topic was discussed
by the committee, everyone agreed that all known compiler vendors
treated "cast to pointer to volatile" accesses as volatile, so the
change was a formality rather than any practical difference.) I don't
know if and when this change was added to C++.

Re: Why volatile may make sense for parallel code today.

<ujpp9d$28uis$1@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 10:10:13 +0100
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <ujpp9d$28uis$1@raubtier-asyl.eternal-september.org>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujpefk$27eto$2@dont-email.me>
<ujpkq5$28aoq$1@raubtier-asyl.eternal-september.org>
<ujpp38$28srl$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 09:10:05 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="9ade70e49ca68ba2beb5a7fae8a1ed12";
logging-data="2390620"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CfhzUruq7NuQ+FIyeTQdykJm42jVNKsE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:vESmKitw5UnpGaxwby+x9DInDO0=
Content-Language: de-DE
In-Reply-To: <ujpp38$28srl$3@dont-email.me>
 by: Bonita Montero - Fri, 24 Nov 2023 09:10 UTC

Am 24.11.2023 um 10:06 schrieb Chris M. Thomasson:
> On 11/23/2023 11:53 PM, Bonita Montero wrote:
>> Am 24.11.2023 um 07:05 schrieb Chris M. Thomasson:
>>
>>> humm.. Say, the read is from a word in memory. Define your trivial
>>> object, POD, l2 cache line sized, and aligned on a l2 cache line
>>> boundary? Are you refering to how certain arch works?
>>
>> Read that:
>> https://stackoverflow.com/questions/61329240/what-is-the-difference-between-trivial-and-non-trivial-objects
>
> I know. Btw, what the hell happened to std::is_pod? ;^)

PODs are also trivial but go beyond since you can copy them
with a memcpy():

Re: Why volatile may make sense for parallel code today.

<ujppgt$28srl$4@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 01:14:04 -0800
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <ujppgt$28srl$4@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$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, 24 Nov 2023 09:14:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7664f6b07543b36eac45e26755f90876";
logging-data="2388853"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187ecQnt2FX6uAEPU5XJdIbb0EzqA8iP/4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:oz/M2nENCXW2Cmb95O3cKvW97SU=
Content-Language: en-US
In-Reply-To: <ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Fri, 24 Nov 2023 09:14 UTC

On 11/23/2023 8:20 AM, Bonita Montero wrote:
> Am 23.11.2023 um 17:07 schrieb Richard Damon:
>
>> My understanding is that std:atomic needs to honor a read in the sense
>> that it wlll get the most recent value that has happened "before" the
>> read (as determined by memory order).
>
> ... and the read is atomic - even if the trivial object is 1kB in size.

How is that read atomic with 1kb of data? On what arch?

>
>> So, if nothing in the loop can establish a time order with respect to
>> other threads, then it should be allowed for the compiler to optimize
>> out the read. ...
>
> An atomic doesn't cache repeatable reads. The order memory-consistency
> parameter is just for the ordering of other reads and writes.
>
>
>

Re: Why volatile may make sense for parallel code today.

<ujpq1k$28uia$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 10:23:00 +0100
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <ujpq1k$28uia$2@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujo3dr$1p192$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 09:23:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="472d6a28bc35cabbeb190303a479b5eb";
logging-data="2390602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19o41gK/nsMotmEy72uYkNDaXvdYBindv0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:qS0g/mC6vuyNAwaVM1TBuUPwGHw=
In-Reply-To: <ujo3dr$1p192$1@i2pn2.org>
Content-Language: en-GB
 by: David Brown - Fri, 24 Nov 2023 09:23 UTC

On 23/11/2023 18:50, Richard Damon wrote:
> On 11/23/23 11:20 AM, Bonita Montero wrote:
>> Am 23.11.2023 um 17:07 schrieb Richard Damon:
>>
>>> My understanding is that std:atomic needs to honor a read in the
>>> sense that it wlll get the most recent value that has happened
>>> "before" the read (as determined by memory order).
>>
>> ... and the read is atomic - even if the trivial object is 1kB in size.
>
> Yes, which has nothing to do with the question.
>
>>
>>> So, if nothing in the loop can establish a time order with respect to
>>> other threads, then it should be allowed for the compiler to optimize
>>> out the read. ...
>>
>> An atomic doesn't cache repeatable reads. The order memory-consistency
>> parameter is just for the ordering of other reads and writes.
>>
>
> Yes, the atomic itself doesn't "cache" the data, but as far as I read,
> there is no requirement to refetch the data if the code still has the
> old value around, and it hasn't been invalidated by possible memory
> ordering.
>
> If there can't be a write "before" the second read, that wasn't also
> also "after" the first read, then there is no requirement to refetch the
> data. In relaxed memory orders, just being physically before isn't
> enough to be "before", but you need some explicit "barrier" to establish
> it.
>
> I will admit this isn't an area I consider myself an expert in, but I
> find no words that prohibit the optimization. The implementation does
> need to consider possible action by other "threads" but only as far a
> constrained by memory order, so two reads in the same ordering "slot"
> are not forced.

That is exactly how I see it (I also do not consider myself an expert in
this area). I cannot see any requirement in the description of the
execution, covering sequencing, ordering, "happens before", and all the
rest, that suggests that the number of atomic accesses, or their order
amongst each other, or their order with respect to volatile accesses or
non-volatile accesses, is forced to follow the source code except where
the atomics have specific sequencing. Atomic accesses are not
"volatile" - they are not, in themselves, "observable behaviour".

Because the the sequencing requirements for atomics depends partly on
things happening in other threads, compilers are much more limited in
how they can re-order or otherwise optimise atomic accesses than they
are for normal accesses (unless the compiler knows all about the other
threads too!). Compilers must be pessimistic about optimisation. But
for certain simple cases, such as multiple neighbouring atomic reads of
the same address or multiple neighbouring writes to the same address, I
can't see any reason why they cannot be combined.

(Again, I am not an expert here - and I will be happy to be corrected.
They say the best way to learn something on the internet is not by
asking questions, but by writing something that is wrong!)

Re: Why volatile may make sense for parallel code today.

<ujpqpl$28uia$3@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.samoylyk.net!nntp.comgw.net!paganini.bofh.team!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c++
Subject: Re: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 10:35:49 +0100
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <ujpqpl$28uia$3@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<20231123120550.812@kylheku.com>
<ujp8n3$26qm6$2@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, 24 Nov 2023 09:35:49 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="472d6a28bc35cabbeb190303a479b5eb";
logging-data="2390602"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7I6Cj9MGWxdtD+EOYZSsBza7WFUlleN8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:NeCOzMQDkQIz80ZTX6Fhz+zNUv8=
In-Reply-To: <ujp8n3$26qm6$2@raubtier-asyl.eternal-september.org>
Content-Language: en-GB
 by: David Brown - Fri, 24 Nov 2023 09:35 UTC

On 24/11/2023 05:27, Bonita Montero wrote:

> I already corrected that with my code and I guessed no one will notice
> that here; usually I'm right with that.
>

You really believe that?

I think one of the (many) reasons people don't take you seriously is
that you never check your work. You invariably post code that is badly
wrong, followed by multiple replies to yourself making corrections and
improvements. Every time you claim your code is bug-free, we know you
will follow up shortly with a bug fix. Every time you claim it is
"perfect", we know that you will follow it with an "improved" version
("perfect" and "improved" being in your opinion only).

Yes, people have noticed. Yes, people will continue to notice.

It's nice that you post code, however, as it can start some interesting
discussions - before descending into a pantomime farce. But it might
make things a little better if you bothered to re-read your code before
posting, or even try testing it.

Re: Why volatile may make sense for parallel code today.

<20231124085003.476@kylheku.com>

  copy mid

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

  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: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 18:27:37 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <20231124085003.476@kylheku.com>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<20231123120550.812@kylheku.com>
<ujp8n3$26qm6$2@raubtier-asyl.eternal-september.org>
Injection-Date: Fri, 24 Nov 2023 18:27:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c1ea8c6eccf6dad4e5ac74f80adfa4fc";
logging-data="2559265"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18qcc/XbFOyoTJrJ6+Na/9iKZpjO67fkHk="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:0/e5MiAcD3UISbC3IPz5a+twLdI=
 by: Kaz Kylheku - Fri, 24 Nov 2023 18:27 UTC

On 2023-11-24, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 23.11.2023 um 21:32 schrieb Kaz Kylheku:
>> On 2023-11-22, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>> #include <Windows.h>
>>> #include <thread>
>>>
>>> using namespace std;
>>>
>>> int main()
>>> {
>>> constexpr size_t ROUNDS = 1'000'000;
>>> size_t volatile r = 1'000'000;
>>> jthread thr( [&]()
>>> {
>>> while( r )
>>> SleepEx( INFINITE, TRUE );
>>> } );
>>> for( size_t r = ROUNDS; r--; )
>>
>> This shadows the r variable. Did you mean "for (size_t i = ROUNDS; i--)"?
>>
>>> QueueUserAPC( (PAPCFUNC)[]( auto p ) { --*(size_t*)p; },
>>> thr.native_handle(), (ULONG_PTR)&r );
>>
>> Thus, this takes the address of the for loop's r variable, not the volatile one
>> that the thread is accessing. Is that what you wanted?
>>
>> BTW, is the C++ lambda too broken to access the r via lexical scoping?
>> Why can't the APC just do "--r".
>>
>> I believe local functions in Pascal from 1971 can do this.
>>
>
> I already corrected that with my code and I guessed no one will notice
> that here; usually I'm right with that.

Anyway, this APC mechanism is quite similar to signal handling.
Particularly asynchronous signal handling. Just like POSIX signals, it
makes the execution abruptly call an unrelated function and then resume
at the interrupted point.

The main difference is that the signal has a number, which selects a
registered handler, rather than specifying a function directly.

Why I bring this up is that ISO C (since 1990, I think), has specified a
use of a "volatile sig_atomic_t" type in regard to asynchronous signal
handlers. (Look it up.)

The use of volatile with interrupt-like mechanisms is nothing new.

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

Re: Why volatile may make sense for parallel code today.

<ujrbr8$2gjhg$1@dont-email.me>

  copy mid

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

  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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 15:32:54 -0800
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <ujrbr8$2gjhg$1@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujntc3$1p191$1@i2pn2.org>
<ujnu49$1t7eu$1@raubtier-asyl.eternal-september.org>
<ujppgt$28srl$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 23:32:56 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a478786200890072e344b77cbd100045";
logging-data="2641456"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/O8ZrVr6dla2uJIz/bEYWrPIMgyOlsCxc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:SKKAeT4VEopjIEhdwocLKmUEPJU=
In-Reply-To: <ujppgt$28srl$4@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 24 Nov 2023 23:32 UTC

On 11/24/2023 1:14 AM, Chris M. Thomasson wrote:
> On 11/23/2023 8:20 AM, Bonita Montero wrote:
>> Am 23.11.2023 um 17:07 schrieb Richard Damon:
>>
>>> My understanding is that std:atomic needs to honor a read in the
>>> sense that it wlll get the most recent value that has happened
>>> "before" the read (as determined by memory order).
>>
>> ... and the read is atomic - even if the trivial object is 1kB in size.
>
> How is that read atomic with 1kb of data? On what arch?

Unless you atomically read a pointer that points to 1kB of memory.

>
>>
>>> So, if nothing in the loop can establish a time order with respect to
>>> other threads, then it should be allowed for the compiler to optimize
>>> out the read. ...
>>
>> An atomic doesn't cache repeatable reads. The order memory-consistency
>> parameter is just for the ordering of other reads and writes.
>>
>>
>>
>

Re: Why volatile may make sense for parallel code today.

<ujrc3n$2gjhg$2@dont-email.me>

  copy mid

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

  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: Why volatile may make sense for parallel code today.
Date: Fri, 24 Nov 2023 15:37:26 -0800
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <ujrc3n$2gjhg$2@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<20231123120550.812@kylheku.com>
<ujp8n3$26qm6$2@raubtier-asyl.eternal-september.org>
<20231124085003.476@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 24 Nov 2023 23:37:27 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a478786200890072e344b77cbd100045";
logging-data="2641456"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kTrLlETAlsyZJZwYizJObWUdjeqBR2J4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lqfNdn/CybPcxyyFsj0bCppAyXU=
In-Reply-To: <20231124085003.476@kylheku.com>
Content-Language: en-US
 by: Chris M. Thomasson - Fri, 24 Nov 2023 23:37 UTC

On 11/24/2023 10:27 AM, Kaz Kylheku wrote:
> On 2023-11-24, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> Am 23.11.2023 um 21:32 schrieb Kaz Kylheku:
>>> On 2023-11-22, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>>> #include <Windows.h>
>>>> #include <thread>
>>>>
>>>> using namespace std;
>>>>
>>>> int main()
>>>> {
>>>> constexpr size_t ROUNDS = 1'000'000;
>>>> size_t volatile r = 1'000'000;
>>>> jthread thr( [&]()
>>>> {
>>>> while( r )
>>>> SleepEx( INFINITE, TRUE );
>>>> } );
>>>> for( size_t r = ROUNDS; r--; )
>>>
>>> This shadows the r variable. Did you mean "for (size_t i = ROUNDS; i--)"?
>>>
>>>> QueueUserAPC( (PAPCFUNC)[]( auto p ) { --*(size_t*)p; },
>>>> thr.native_handle(), (ULONG_PTR)&r );
>>>
>>> Thus, this takes the address of the for loop's r variable, not the volatile one
>>> that the thread is accessing. Is that what you wanted?
>>>
>>> BTW, is the C++ lambda too broken to access the r via lexical scoping?
>>> Why can't the APC just do "--r".
>>>
>>> I believe local functions in Pascal from 1971 can do this.
>>>
>>
>> I already corrected that with my code and I guessed no one will notice
>> that here; usually I'm right with that.
>
> Anyway, this APC mechanism is quite similar to signal handling.
> Particularly asynchronous signal handling. Just like POSIX signals, it
> makes the execution abruptly call an unrelated function and then resume
> at the interrupted point.
>
> The main difference is that the signal has a number, which selects a
> registered handler, rather than specifying a function directly.
>
> Why I bring this up is that ISO C (since 1990, I think), has specified a
> use of a "volatile sig_atomic_t" type in regard to asynchronous signal
> handlers. (Look it up.)
>
> The use of volatile with interrupt-like mechanisms is nothing new.
>

After reading this, for some reason I am now thinking about signal safe
sync primitives in POSIX. Fwiw, certain pure lock/wait free algorithms
in signal handlers are okay.

Re: Why volatile may make sense for parallel code today.

<ujso91$2prnc$2@raubtier-asyl.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.samoylyk.net!news.gegeweb.eu!gegeweb.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: Why volatile may make sense for parallel code today.
Date: Sat, 25 Nov 2023 13:11:22 +0100
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <ujso91$2prnc$2@raubtier-asyl.eternal-september.org>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<20231123120550.812@kylheku.com>
<ujp8n3$26qm6$2@raubtier-asyl.eternal-september.org>
<20231124085003.476@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 25 Nov 2023 12:11:13 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="c7d7c344a75e43bef168361525523d8d";
logging-data="2944748"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18XsjvxYzF5WuYRSyU/5Gx8+S0KACCzt9A="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:AyTYFfJx0gYU/TgEHuTK3RCzOoQ=
In-Reply-To: <20231124085003.476@kylheku.com>
Content-Language: de-DE
 by: Bonita Montero - Sat, 25 Nov 2023 12:11 UTC

Am 24.11.2023 um 19:27 schrieb Kaz Kylheku:

> Anyway, this APC mechanism is quite similar to signal handling.
> Particularly asynchronous signal handling. Just like POSIX signals,
> it makes the execution abruptly call an unrelated function and then
> resume at the interrupted point.

I don't think so because APCs only can interrupt threads in an alertable
mode. Signals can interrupt nearly any code and they have implications
on the compiler's ABI through defining the size of the red zone. So com-
pared to signals APCs are rather clean, nevertheless you can do a lot of
interesting things with signals, as reported lately when I was informed
that mutexes with the glibc rely on signals; I gues it's the same when
a thread waits for a condition variable and a mutex at once.

> The main difference is that the signal has a number, which selects
> a registered handler, rather than specifying a function directly.

The ugly thing with synchronous signals is that the signal handler is
global for all threads. You can concatenate them but the next signal
handler in the chain may be in a shared object already unloaded. I
think this should be corrected my making synchronous signals' handlers
thread-specific.

> The use of volatile with interrupt-like mechanisms is nothing new.

I think this pattern doesn't happen very often since it's rare that
a signal shares state with the interrupted code.

Re: Why volatile may make sense for parallel code today.

<ujttfq$2v76e$2@dont-email.me>

  copy mid

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

  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: Why volatile may make sense for parallel code today.
Date: Sat, 25 Nov 2023 14:46:18 -0800
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <ujttfq$2v76e$2@dont-email.me>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<20231123120550.812@kylheku.com>
<ujp8n3$26qm6$2@raubtier-asyl.eternal-september.org>
<20231124085003.476@kylheku.com>
<ujso91$2prnc$2@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, 25 Nov 2023 22:46:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a478786200890072e344b77cbd100045";
logging-data="3120334"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SC1TU8LmuzVuOH+jio6ecEBb+YJRoKAI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:cdPFfPMfOZEfRpa1xHmtbhOCGFU=
Content-Language: en-US
In-Reply-To: <ujso91$2prnc$2@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Sat, 25 Nov 2023 22:46 UTC

On 11/25/2023 4:11 AM, Bonita Montero wrote:
> Am 24.11.2023 um 19:27 schrieb Kaz Kylheku:
>
>> Anyway, this APC mechanism is quite similar to signal handling.
> > Particularly asynchronous signal handling. Just like POSIX signals,
> > it makes the execution abruptly call an unrelated function and then
> > resume at the interrupted point.
>
> I don't think so because APCs only can interrupt threads in an alertable
> mode. Signals can interrupt nearly any code and they have implications
> on the compiler's ABI through defining the size of the red zone. So com-
> pared to signals APCs are rather clean, nevertheless you can do a lot of
> interesting things with signals, as reported lately when I was informed
> that mutexes with the glibc rely on signals; I gues it's the same when
> a thread waits for a condition variable and a mutex at once.
>
>> The main difference is that the signal has a number, which selects
>> a registered handler, rather than specifying a function directly.
>
> The ugly thing with synchronous signals is that the signal handler is
> global for all threads. You can concatenate them but the next signal
> handler in the chain may be in a shared object already unloaded. I
> think this should be corrected my making synchronous signals' handlers
> thread-specific.
>
>> The use of volatile with interrupt-like mechanisms is nothing new.
>
> I think this pattern doesn't happen very often since it's rare that
> a signal shares state with the interrupted code.
>

You might be interested in how pthreads-win32 handles async thread
cancellation. Iirc, it uses a kernel module. It's hackish, but interesting.

https://sourceware.org/pthreads-win32/

Re: Why volatile may make sense for parallel code today.

<ukc37c$1to4s$3@gwaiyur.mb-net.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!eternal-september.org!news.swapon.de!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail
From: news.5.m...@spamgourmet.org (Marcel Mueller)
Newsgroups: comp.lang.c++
Subject: Re: Why volatile may make sense for parallel code today.
Date: Fri, 1 Dec 2023 08:50:04 +0100
Organization: MB-NET.NET for Open-News-Network e.V.
Message-ID: <ukc37c$1to4s$3@gwaiyur.mb-net.net>
References: <ujlajk$1dalh$1@raubtier-asyl.eternal-september.org>
<ujlrdv$1g6ej$1@dont-email.me> <ujm5n6$1hok4$1@redfloyd.dont-email.me>
<ujmmia$1nmms$1@dont-email.me> <ujmmki$1nmms$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 1 Dec 2023 07:50:04 -0000 (UTC)
Injection-Info: gwaiyur.mb-net.net;
logging-data="2023580"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hciVEd6aTuDCbnBQOec0SFW/uGA= sha256:QQEcTj/BPj60O+XDXNHGP9sALbTgXe7iNT9oIpwVlbY=
sha1:Kk0npIe7ozMTzp/FuEi/Wj+RCHA= sha256:4NVkoR7GhLmswjX8ZnbMaQnC/FAW+87rwXe74fw7ONA=
Content-Language: de-DE, en-US
In-Reply-To: <ujmmki$1nmms$2@dont-email.me>
 by: Marcel Mueller - Fri, 1 Dec 2023 07:50 UTC

Am 23.11.23 um 06:06 schrieb Chris M. Thomasson:
> Afaict, std::atomic should imply volatile? Right? If not, please correct
> me!

In practice yes. But is this required by the standard? I could not find
any hint. Strictly speaking it is still required.

In fact memory ordering does not guarantee any particular time when the
change appears at another thread. So there is always some delay. But
could it be infinite? So the compiler could cache anything if no other
memory access according to the memory barrier is generated by the code?
This applies to read and write.
But I think it is almost impossible to write any reasonable code, that
causes no other memory access that forces the atomic value to be read or
written.

Marcel


devel / comp.lang.c++ / Re: Why volatile may make sense for parallel code today.

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor