C# Thread safe fast(est) counter

What is the way to obtain a thread safe counter in C# with best possible performance?

This is as simple as it gets:

public static long GetNextValue()
{
    long result;
    lock (LOCK)
    {
        result = COUNTER++;
    }
    return result;
}

But are there faster alternatives?

5 Answers
5

Leave a Comment