C# Thread safe fast(est) counter

后端 未结 5 2023
攒了一身酷
攒了一身酷 2020-12-12 17:32

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()         


        
5条回答
  •  春和景丽
    2020-12-12 18:19

    This would be simpler:

    return Interlocked.Increment(ref COUNTER);
    

    MSDN Interlocked.Increment

提交回复
热议问题