Synopsis: | Initialize atomic variables correctly |
Language: | C++ |
Severity Level: | 1 |
Category: | Object Life Cycle |
Description: |
If an atomic variable is not default constructed, its behavior will be undefined. So it is important to make sure that atomic variables are initialized in the class definition or via the initializer list of the constructor. For example: class C { public: C(int x); private: std::atomic<int> i { 0 }; }; or alternatively C::C(int x) : i(x) {} Never use atomic_init, because if it is already initialized its behavior is undefined: C::C(int x) { … Some calculation … std::atomic_init(&i, y); } |