I have no guess as to why it would be desirable for this function to have "C" rather than "C++" linkage.
2 Answers
That was added by LWG issue 1479 which was addressing a last-minute comment on C++11.
The rationale for this change was C language compatibility (C11 thread library has identically-named function atomic_thread_fence in stdatomic.h).
As far as I understand, it was always a plan that C and C++ atomic libraries can coexist: other examples of compatiblity are the C-compatible type aliases for std::atomic, such as atomic_int
and the C-compatibility macro ATOMIC_VAR_INIT
-
I don't understand how the C and C++ prototypes are compatible, given that the C++ prototype is in the std namespace.– WaltKFeb 2, 2017 at 13:38
-
@WaltK I would guess it allows the C++ implementation of
<atomic>
to include<stdatomic.h>
and then saynamespace std { using ::atomic_thread_fence; }
or something equivalent. Of course it doesn't helpatomic_flag_test_and_set
which still has C++ linkage in C++ - perhaps that NB comment wasn't thought out that well.– CubbiFeb 3, 2017 at 3:58 -
3
atomic_thread_fence
establishes memory synchronization ordering of non-atomic and relaxed atomic accesses.
Concurrency, especially relaxed-memory concurrency, is a notoriously
subtle and error-prone domain, and so verifying such optimisations
is of great interest. Ref1.
For such thing is widely used CompCertTSO.
CompCertTSO is a compiler that generates x86 assembly code from ClightTSO, a large subset of the C programming language enhanced with concurrency primitives for thread management and synchronisation, and with a TSO relaxed memory model based on the x86-TSO model.
So, for verification, optimisation and testing purposes is desirable for this function to have "C" linkage.
C
en.cppreference.com/w/c/atomic/atomic_thread_fence and this is just the imported (into the std namespace) version?