TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C++ Coding Standard
Search

Rule:  OLC#021Checked automatically with code checker

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. Prefer initialization of atomic variables in the class definition. E.g.

class C
{
public:
       C(int x);
private:
       std::atomic<int> i { 0 };
};

If initialization of the atomic variable depends on a value passed to the constructor, initialize the variable in the initializer list of the constructor:

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);
}