C++ Logo

std-proposals

Advanced search

[std-proposals] atomic_compare_exchange_and_notify

From: Ryan P. Nicholl <rnicholl_at_[hidden]>
Date: Wed, 14 Jan 2026 00:55:24 +0000
It has come to my attention that it isn't possible to implement data structures like std::latch or a uselock using the C++ atomic library without undefined behavior.

Namely, std::latch guarantees that count_down doesn't cause a race even if there are waiters, this isn't possible to implement on the abstract machine unless the notification can be safely executed atomically with the value set. Ordinary notify_one has undefined behavior if it races with the destructor of the atomic object.

Of course, it's possible to implement std::latch using things like SYS_futex, so this isn't impossible to implement per se, only impossible to implement using the C++ standard library without relying on undefined behavior.

I would propose adding atomic_compare_exchange_and_notify. This would have the same behavior as atomic compare_exchange followed by notify, except that it doesn't cause undefined behavior if a waiter unblocks on the new value and destroys the atomic, which could race with notify.

--
Ryan P. Nicholl
Tel: (678)-358-7765

Received on 2026-01-14 00:55:31