Should I use static fields and interlocked together, in cases when i need to provide thread safety and atomic operations with static fields, Is static fields are atomic by default? For example:
Interlocked.Increment(ref Factory.DefectivePartsCount);
Thanks.
Yes.
The field (assuming Int32) is atomic, not because it’s static but because it’s 32 bits.
How ever,
Factory.DefectivePartsCount += 1requires a read and a write action on the variable so the whole operation is not thread-safe.