众所周知:https://stackoverflow.com/a/32694707/1558037
第1104页:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
C ++11§29.5/ 1说
有一个泛型类模板atomic。模板的类型 参数T应该是可以复制的(3.9)。
§3.9告诉
标量类型,简单的可复制类类型(第9节),数组 这些类型和这些类型的cv限定版本(3.9.3)是 统称为平凡的可复制类型。
全文:
1 There is a generic class template atomic<T>. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically initializable may be difficult to use.
— end note ]
2 The semantics of the operations on specializations of atomic are defined in 29.6.
3 Specializations and instantiations of the atomic template shall have a deleted copy constructor, a deleted copy assignment operator, and a constexpr value constructor.
4 There shall be full specializations of the atomic template for the integral types char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, char16_t, char32_t, wchar_t, and any other types needed by the typedefs in the header <cstdint>. For each integral type integral, the specialization atomic<integral> provides additional atomic operations appropriate to integral types. There shall be a specialization atomic<bool> which provides the general atomic operations as specified in 29.6.1.
5 The atomic integral specializations and the specialization atomic<bool> shall have standard layout. They shall each have a trivial default constructor and a trivial destructor. They shall each support aggregate initialization syntax.
6 There shall be pointer partial specializations of the atomic class template. These specializations shall have standard layout, trivial default constructors, and trivial destructors. They shall each support aggregate initialization syntax.
7 There shall be named types corresponding to the integral specializations of atomic, as specified in Table 145, and a named type atomic_bool corresponding to the specified atomic<bool>. Each named type is a either typedef to the corresponding specialization or a base class of the corresponding specialization. If it is a base class, it shall support the same member functions as the corresponding specialization.
8 There shall be atomic typedefs corresponding to the typedefs in the header <inttypes.h> as specified in Table 146.
9 [ Note: The representation of an atomic specialization need not have the same size as its corresponding argument type. Specializations should have the same size whenever possible, as this reduces the effort required to port existing code. — end note ]
此代码还显示std::array<int, 100>
is_trivially_copyable:http://coliru.stacked-crooked.com/a/712a445302406f68
#include <iostream>
#include <type_traits>
#include <array>
#include <atomic>
int main() {
std::array<int, 100> myarray; // 100 elements
std::cout << "myarray - trivially_copyable: " << std::boolalpha <<
std::is_trivially_copyable<decltype(myarray)>::value << std::endl;
int c_array[100];
std::cout << "c_array - trivially_copyable: " << std::boolalpha <<
std::is_trivially_copyable<decltype(c_array)>::value << std::endl;
std::atomic<std::array<int, 100>> array_atomic;
std::cout << "sizeof(array_atomic): " << std::boolalpha <<
sizeof(decltype(array_atomic)) << std::endl;
//array_atomic.store(myarray); // error
//std::atomic<c_array[100]> c_array_atomic; // error
//std::atomic<decltype(c_array)> c_array_atomic; // error
return 0;
}
结果:
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
myarray - trivially_copyable: true
c_array - trivially_copyable: true
sizeof(array_atomic): 400
但如果我尝试使用array_atomic.store(myarray);
,那么我会收到错误:
/tmp/ccgTvl0G.o: In function `main': main.cpp:(.text.startup+0xec):
undefined reference to `__atomic_store' collect2: error: ld returned 1
exit status
我们还需要使用自定义C ++ - 类型(类)作为std::atomic<>
的模板吗?
解答:
应该有新的编译器,有时您需要链接库-latomic
#include <iostream>
#include <array>
#include <atomic>
int main() {
std::array<int, 100> myarray; // 100 elements
std::atomic<std::array<int, 100>> array_atomic;
array_atomic.store(myarray); // atomic copy 100 elements
return 0;
}
可以编译和链接:
-std=c++11 -O3
:https://godbolt.org/g/tLRGD6 -std=c++11 -O3
:https://godbolt.org/g/QFrZPZ -std=c++11 -O3
:https://godbolt.org/g/9XP013 -std=c++11 -O3 -latomic
:https://godbolt.org/g/BdJsnb -std=c++11 -O3 -latomic
:https://godbolt.org/g/wifvCm -std=c++11 -O3
:https://godbolt.org/g/UIURSW