I need to create a Thread-safe static variable in C# .Net

后端 未结 7 1389
失恋的感觉
失恋的感觉 2020-12-17 18:01

ok, its a little more complicated than the question.

class A
{
   static int needsToBeThreadSafe = 0;

   public static void M1()
   {
     needsToBeThreadSa         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 18:34

    How About:

    public static void M1()
    {
        Interlocked.Exchange( ref needsToBeThreadSafe, RandomNumber() );
    }
    
    public static void M2()
    {
        print( Interlocked.Read( ref needsToBeThreadSafe ) );
    }
    

提交回复
热议问题