From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6DAAF3858412; Mon, 29 May 2023 20:38:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6DAAF3858412 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685392734; bh=TY53fp9oiobrmmARl51wzNiHExZ5swBKEVMkgpiACkg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=PvwWejshvhMo0SZxqS6tZrH2rjZcLGQ4U6Ri1f9poSg9465b1YfT8cSsrhZDQ26z4 b7D6MadLXQywgCbbqQWI8kROobnT1OK7Q3go5AM1lcFoWJhCDi0r1yrLW/zbKYp9HR 3B0SJHZR0ED0AnNASdpE8d/2dqqXEfj3NChQasBY= From: "redi at gcc dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 12.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110016 --- Comment #16 from Jonathan Wakely --- The condition variable uses that mutex to synchronize. If you just modify t= he 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 describ= ed) 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 sig= nals using notify_all but that is missed because the consumer thread isn't waiti= ng 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 i= t'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=