Atomic operation uses a non-default memory order
ID |
c.correctness.relaxed_memory_order |
Severity |
low |
Remediation Complexity |
hard |
Remediation Risk |
high |
Remediation Effort |
high |
Resource |
Concurrency |
Language |
C / C++ |
Description
This atomic operation specifies an explicit memory_order_relaxed, memory_order_acquire, memory_order_release, or memory_order_acq_rel instead of the default memory_order_seq_cst. Relaxing the ordering only guarantees atomicity of this operation, not the relative visibility of other memory accesses around it — a guarantee the surrounding code may be silently relying on. It is only correct when paired with a specific, hand-verified synchronization pattern (a producer/consumer handoff, a reference count). Confirm the pairing is actually correct and documented, or fall back to the default sequentially-consistent ordering.
Rationale
This atomic operation specifies an explicit memory_order_relaxed, memory_order_acquire, memory_order_release, or memory_order_acq_rel instead of the default memory_order_seq_cst. Relaxing the ordering only guarantees atomicity of this operation, not the relative visibility of other memory accesses around it — a guarantee the surrounding code may be silently relying on. It is only correct when paired with a specific, hand-verified synchronization pattern (a producer/consumer handoff, a reference count). Confirm the pairing is actually correct and documented, or fall back to the default sequentially-consistent ordering.
The following code illustrates the pattern detected by this rule:
void relaxed_load()
{
// FLAGGED: Atomic operation uses a non-default memory order
int v = counter.load(std::memory_order_relaxed);
(void)v;
}