User Defined Atomics
This lesson gives an overview of user-defined atomics used from the perspective of concurrency in C++.
We'll cover the following...
There are a lot of deep restrictions on a user-defined type MyType if you use it for an atomic type std::atomic<MyType>. These restrictions are on the type MyType, but also on the operations that std::atomic<MyType> can perform.
Here are the restrictions for MyType to become an atomic type:
- The copy assignment operator for
MyType(all base classes ofMyTypeand all non-static members ofMyType) must be trivial. This means that you must not define the copy assignment operator but request it bydefaultfrom the compiler. MyTypemust not have virtual methods or virtual base classes.MyTypemust be bitwise comparable so that the C functions memcpy or memcmp can be applied.
Check the type properties at compile time
The type properties on
MyTypecan be checked at compile time, by using the following functions:std::is_trivially_copy_constructible,std::is_polymorphicandstd::is_trivial. All these functions are part of the very powerful type-traits library.
The user-defined atomic type std::atomic<MyType> supports only a limited interface.