From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 16F3C385841A; Thu, 11 Nov 2021 10:54:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 16F3C385841A From: "s.zvyagin83 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/103187] New: std::counting_semaphore::try_acquire_for does not unblock during wait duration Date: Thu, 11 Nov 2021 10:54:08 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: s.zvyagin83 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: 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: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: 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: Thu, 11 Nov 2021 10:54:09 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103187 Bug ID: 103187 Summary: std::counting_semaphore::try_acquire_for does not unblock during wait duration Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: s.zvyagin83 at gmail dot com Target Milestone: --- According to C++ standart https://eel.is/c++draft/thread.sema.cnt#18.2 try_acquire_for/until should block until counter is greater than zero or un= til timeout expires.=20 But semaphores implemented with atomic_wait blocks for whole timeout even if semaphore released during wait.=20 minimal repoduce code: g++ -std=3Dc++20 -lpthread #include #include #include #include using namespace std::chrono_literals; int main() { std::binary_semaphore sem{0}; auto begin =3D std::chrono::steady_clock::now(); auto time =3D [&begin] { return std::chrono::duration_cast(std::chrono::steady_c= lock::now()-begin).count(); }; std::jthread jthr( [&sem, &time] { printf("[%ld] thread start\n", time()); std::this_thread::sleep_for(1s); printf("[%ld] sem.release\n", time()); sem.release(); }); printf("[%ld] sem.try_acquire_for\n", time()); if (sem.try_acquire_for(10s)) printf("[%ld] sem.acquired\n", time()); else printf("[%ld] failed to acquire sem\n", time()); } output: [0] sem.try_acquire_for [0] thread start [1000] sem.release [10000] sem.acquired POSIX semaphores with same code work correctly and unblock immediately when semaphore released g++ -std=3Dc++20 -lpthread -D_GLIBCXX_USE_POSIX_SEMAPHORE=3D1 output: [0] sem.try_acquire_for [0] thread start [1000] sem.release [1000] sem.acquired=