public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/110016] Possible miscodegen when inlining std::condition_variable::wait predicate causes deadlock
Date: Mon, 29 May 2023 20:38:54 +0000	[thread overview]
Message-ID: <bug-110016-4-8DvLobUCMh@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110016-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110016

--- Comment #16 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The condition variable uses that mutex to synchronize. If you just modify the
atomic variable _without locking and unlocking the mutex_ then you are not
synchronizing with the condition variable.

If you don't use the mutex for the producer thread, then (as Andrew described)
the consumer can check the atomic, see that the condition is not yet true, and
decide it needs to wait. Then the producer makes the condition true and signals
using notify_all but that is missed because the consumer thread isn't waiting
yet, then the consumer starts to wait. But because it already missed the
notify_all call, it waits forever.

Using the mutex in the producer does not just hide the bug, it fixes it
properly. If the producer updates the atomic while the mutex is held then it's
impossible for the condition to become true between the consumer checking it
and starting to wait. Those two things (checking the condition and starting to
wait) become atomic w.r.t the producer. It's not possible to get this
interleaving if you use the mutex correctly:

consumer checks condition
producer makes condition true
producer notifies
consumer waits

Instead you either get:

consumer checks condition and waits
producer makes condition true and notifies, waking the consumer

or:

producer makes condition true (and notifies, but consumer isn't waiting)
consumer sees condition is true and never waits

      parent reply	other threads:[~2023-05-29 20:38 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-28 21:11 [Bug c++/110016] New: [12/13/14] " amy at amyspark dot me
2023-05-28 21:44 ` [Bug libstdc++/110016] " pinskia at gcc dot gnu.org
2023-05-29  0:47 ` [Bug libstdc++/110016] " amy at amyspark dot me
2023-05-29  1:03 ` pinskia at gcc dot gnu.org
2023-05-29  1:05 ` pinskia at gcc dot gnu.org
2023-05-29  1:05 ` pinskia at gcc dot gnu.org
2023-05-29  1:20 ` pinskia at gcc dot gnu.org
2023-05-29  1:27 ` pinskia at gcc dot gnu.org
2023-05-29  1:34 ` pinskia at gcc dot gnu.org
2023-05-29  1:47 ` pinskia at gcc dot gnu.org
2023-05-29  6:43 ` pinskia at gcc dot gnu.org
2023-05-29 13:30 ` rachel at rachelmant dot com
2023-05-29 14:28 ` pinskia at gcc dot gnu.org
2023-05-29 15:15 ` pinskia at gcc dot gnu.org
2023-05-29 15:18 ` rachel at rachelmant dot com
2023-05-29 15:37 ` pinskia at gcc dot gnu.org
2023-05-29 20:38 ` redi at gcc dot gnu.org [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=bug-110016-4-8DvLobUCMh@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).