public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Liu Hao <lh_mouse@126.com>
Cc: "Florian Weimer" <fweimer@redhat.com>,
	"☂Josh Chia (謝任中) via Gcc-help" <gcc-help@gcc.gnu.org>
Subject: Re: Failure to optimize?
Date: Tue, 12 Jan 2021 18:16:16 +0000	[thread overview]
Message-ID: <CAH6eHdSeXLAbrkeeyjM31rk90CT22ShYb7AaHvTYYDWjpmK=oQ@mail.gmail.com> (raw)
In-Reply-To: <1129702b-f187-d8f4-3070-f0122834ffc8@126.com>

On Tue, 12 Jan 2021, 16:55 Liu Hao, <lh_mouse@126.com> wrote:

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


Yes, that's Florian's point. Introducing a write in the other threads would
introduce a data race, which has undefined behaviour.

      reply	other threads:[~2021-01-12 18:16 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
2021-01-12 18:16       ` Jonathan Wakely [this message]

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='CAH6eHdSeXLAbrkeeyjM31rk90CT22ShYb7AaHvTYYDWjpmK=oQ@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=lh_mouse@126.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).