From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 576D43875A25; Mon, 25 Jul 2022 13:51:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 576D43875A25 From: "anthony.ajw at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/106183] std::atomic::wait might fail to be unblocked by notify_one/all on platforms without platform_wait() Date: Mon, 25 Jul 2022 13:51:39 +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: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: anthony.ajw at gmail dot com X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: rodgertq at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jul 2022 13:51:39 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106183 Anthony Williams changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |anthony.ajw at gmail dot c= om --- Comment #3 from Anthony Williams --- This is one of the common mistakes I mention when teaching people about condition variables. Just because the data being waited for is atomic, does= n't guarantee that the condition variable state is updated: you need the mutex = to synchronize that. In current libstdc++ trunk libstdc++-v3/include/bits/atomic_wait.h insert a line in _M_notify at line 235: https://gcc.gnu.org/git/?p=3Dgcc.git;a=3Dblob;f=3Dlibstdc%2B%2B-v3/include/= bits/atomic_wait.h;h=3D125b1cad88682384737c048ac236af9c4deab957;hb=3Drefs/h= eads/trunk void _M_notify(const __platform_wait_t* __addr, bool __all, bool __bare) noexcept { if (!(__bare || _M_waiting())) return; #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT __platform_notify(__addr, __all); #else /// INSERT HERE { std::lock_guard __lock(_M_mtx); } /// END INSERT if (__all) _M_cv.notify_all(); else _M_cv.notify_one(); #endif } The lock/unlock here ensures that the notify is correctly synchronized with= the wait.=