From: Liu Hao <lh_mouse@126.com>
To: Jonathan Wakely <jwakely.gcc@gmail.com>,
Florian Weimer <fweimer@redhat.com>
Cc: "☂Josh Chia (謝任中) via Gcc-help" <gcc-help@gcc.gnu.org>
Subject: Re: Failure to optimize?
Date: Wed, 13 Jan 2021 00:53:54 +0800 [thread overview]
Message-ID: <1129702b-f187-d8f4-3070-f0122834ffc8@126.com> (raw)
In-Reply-To: <CAH6eHdTrO-2R-oncSYVP=5qBTkcU386qmyoepnwc956GbB9ajw@mail.gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 1612 bytes --]
在 2021-01-12 22:20, Jonathan Wakely via Gcc-help 写道:
>
> I'm not sure about the rules for C, but in C++ the compiler can assume
> there is no race, because the increment is not atomic. If there were
> another access to the variable then a non-atomic store would be a race
> even in the bar1 version.
>
What about this code:
// -- beginning of copy-n-pasted code
char const* foo();
int cursor = 0;
char const* bar1() {
char const* result = foo();
if (result)
++cursor;
return result;
}
char const* bar2() {
char const* result = foo();
cursor += !!result;
return result;
}
// -- end of copy-n-pasted code
#include <atomic>
#include <thread>
#include <cstdio>
char const* foo() {
static ::std::atomic<char const*> str("meow");
return str.exchange(nullptr);
}
int main() {
::std::thread thrs[10];
for(auto& r : thrs)
r = ::std::thread(bar1);
for(auto& r : thrs)
r.join();
::std::printf("cursor = %d\n", cursor);
}
`foo()` will return non-null for exactly one thread. Increment of `cursor` by that thread is
sequenced before its termination, which synchronizes with exactly one `join()`, which is sequenced
before the final read of `cursor`. There is no race in this program, but there would be if `bar2`
was called in place of `bar1`, where all threads could modify `cursor` concurrently.
--
Best regards,
LH_Mouse
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]
next prev parent reply other threads:[~2021-01-12 16:54 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-12 13:32 ☂Josh Chia (謝任中)
2021-01-12 13:36 ` Florian Weimer
2021-01-12 14:05 ` ☂Josh Chia (謝任中)
2021-01-12 14:15 ` Alexander Monakov
2021-01-12 14:20 ` Florian Weimer
2021-01-12 14:20 ` Jonathan Wakely
2021-01-12 14:22 ` Florian Weimer
2021-01-12 16:53 ` Liu Hao [this message]
2021-01-12 18:16 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1129702b-f187-d8f4-3070-f0122834ffc8@126.com \
--to=lh_mouse@126.com \
--cc=fweimer@redhat.com \
--cc=gcc-help@gcc.gnu.org \
--cc=jwakely.gcc@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).