Search code examples
c#thread-safetylockingarithmetic-expressions

Are basic arithmetic operations in C# atomic


Are the basic arithmetic operations Thread safe?

For example, if there is ++ operation on a global variable, which will be modified from different threads, is it necessary to a lock around it?

For example

void MyThread() // can have many running instances
{
    aGlobal++;
}

or should it be

void MyThread()
{
    lock( lockerObj)
    {
        aGlobal++;
    }
}

Solution

  • The spec sums it up very well. Section 5.5, "Atomicity of variable references":

    Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. In addition, reads and writes of enum types with an underlying type in the previous list are also atomic. Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic. Aside from the library functions designed for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or decrement.

    Conclusions:

    • Independent reads/writes are atomic (but only for some data types)
    • Read/modify/write (such as i++) is never atomic
    • You can use the Interlocked class methods to achieve atomicity when it's not already guaranteed

    In cases where Interlocked functionality is not enough there is no other option than to use a synchronization primitive, such as Monitor.Enter (which the compiler also exposes through the lock statement).