A variable is tested before and after taking a lock, and the first test reads it without the lock and without std::atomic
What it finds
if (!instance) { std::lock_guard<std::mutex> lock(m); if (!instance) instance = new T; }: the first test reads instance without the lock while another thread may be writing it under the lock. That is a data race, undefined in C++, and in practice the reader may see the pointer before the object it points to is fully built.
Declaring the variable std::atomic, or using std::call_once or a function-local static, which the language initializes once and safely, avoids the race.
The rule looks for an if whose condition tests a variable, a lock taken in its body with std::lock_guard, std::unique_lock, std::scoped_lock or .lock(), and the same test again after it, guarding an assignment to the variable. The variable must be declared in the file without atomic in its type.
From the rule's source, double_checked_lock_not_atomic.rs.
Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/cpp:double-checked-lock-not-atomic" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/cpp:double-checked-lock-not-atomic] reason="..."tx0 explain tx0/cpp:double-checked-lock-not-atomic shows the same in the terminal. Found a false positive? Report it.