Can we use std :: atomic <std :: array <>>?
As it is known: https://stackoverflow.com/a/2208778
Page 1104: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
C ++ 11 ยง29.5 / 1 says
There is a common template pattern. The template type argument T must be trivially copied (3.9).
ยง3.9 says
Scalar types, types with the ability to copy types (section 9), arrays of such types, and cv-qualification versions of these types (3.9.3) are collectively called trivially copied types.
Full text:
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 ] Also this code shows that 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; } As a result:
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 But if I try to use array_atomic.store(myarray); , then I get an error:
/tmp/ccgTvl0G.o: In function `main': main.cpp:(.text.startup+0xec): undefined reference to `__atomic_store' collect2: error: ld returned 1 exit status What do we need to use custom C ++ - type (class) as a std::atomic<> template?
ANSWER:
There must be new compilers, and sometimes you need to link the -latomic library
#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; } It can be compiled and linked:
- ARM gcc 4.8.2 and higher
-std=c++11 -O3: https://godbolt.org/g/tLRGD6 - ARM64 gcc 4.8 and higher
-std=c++11 -O3: https://godbolt.org/g/QFrZPZ - PowerPC gcc 4.8 and higher
-std=c++11 -O3: https://godbolt.org/g/9XP013 - x86 clang 3.3 and above
-std=c++11 -O3 -latomic: https://godbolt.org/g/BdJsnb - x86 gcc 4.9.0 and higher
-std=c++11 -O3 -latomic: https://godbolt.org/g/wifvCm - x86 icc 13.0.1 and higher
-std=c++11 -O3: https://godbolt.org/g/UIURSW
No one has answered this question yet.
See similar questions:
or similar: