5

I have no guess as to why it would be desirable for this function to have "C" rather than "C++" linkage.

1

2 Answers 2

5

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

3
  • I don't understand how the C and C++ prototypes are compatible, given that the C++ prototype is in the std namespace.
    – WaltK
    Feb 2, 2017 at 13:38
  • @WaltK I would guess it allows the C++ implementation of <atomic> to include <stdatomic.h> and then say namespace std { using ::atomic_thread_fence; } or something equivalent. Of course it doesn't help atomic_flag_test_and_set which still has C++ linkage in C++ - perhaps that NB comment wasn't thought out that well.
    – Cubbi
    Feb 3, 2017 at 3:58
  • 3
    @WaltK exactly like sin or printf.
    – n. m.
    Feb 3, 2017 at 8:44
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.