What's the difference between “atomic” and “cs

2019-02-17 00:14发布

问题:

can someone please clarify the difference between the include options #include <atomic> and #inlucde <cstdatomic>?

I'm guessing that there is none, because its the same behaviour?

I am asking this because on my debian system I've got only the atomic and on my kubuntu system I've got the cstdatomic.

  • compiler on Debian: version 4.7.2 (Debian 4.7.2-4)

  • compiler on Kubuntu: version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

回答1:

<atomic> is the C++ atomic operations library.

<cstdatomic> is the C++ version of the C atomic operations library.

Both will give you e.g., std::atomic_char but only the C++ version has std::atomic<T>.

As a general rule, C headers should be used in C++ by removing the .h extension and prepending c to the name: stdatomic.h becomes cstdatomic. That will include the C headers into the namespace std.

Note also that stdatomic.h (and cstdatomic hence) is C11 and atomic is C++11, which might explain the difference in compiler support.



回答2:

Both existing answers are wrong, and most comments too.

<cstdatomic> is not a header defined in any standard.

It was defined in old C++0x drafts but is not in the final C++11 standard, only <atomic> is. So it was included as part of GCC 4.4's experimental C++0x support, but then renamed for later releases when it got renamed in the C++0x drafts (which was done in 2009 by N2992).

You should not use <cstdatomic> unless you are stuck with GCC 4.4 and happy to use an incomplete and buggy version of C++11 atomics. (I have no idea why Kubuntu's GCC 4.6 includes the header, it is not in the upstream GCC 4.6 releases, it must be an Ubuntu or Kubuntu or Linaro patch.)

<atomic> is the standard C++11 header that you can rely on for any reasonably conforming C++11 implementation.

<stdatomic.h> is the C11 header, but the C++11 library is based on the C99 library, so does not include <stdatomic.h> and does not provide a <cstdatomic> corresponding to it.



标签: c++ c++11 atomic