A volatile field is updated with ++, -- or a compound assignment, which is not atomic
What it finds
volatile makes each read and write visible to other threads, but count++ is a read and a write: two threads can read the same value and both write it back incremented once. Interlocked.Increment or a lock makes the update atomic.
From the rule's source, volatile_compound.rs.
class Stats{ private volatile int hits; private int misses; void Record(bool hit) { if (hit) { hits++;tx0 reports: updating the volatile field hits this way is not atomic this.hits += 2;tx0 reports: updating the volatile field hits this way is not atomic } else { misses++; hits = 0; } }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:volatile-compound" = "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/csharp:volatile-compound] reason="..."tx0 explain tx0/csharp:volatile-compound shows the same in the terminal. Found a false positive? Report it.