ok, its a little more complicated than the question.
class A
{
   static int needsToBeThreadSafe = 0;
   public static void M1()
   {
     needsToBeThreadSafe = RandomNumber();
   }
   public static void M2()
   {
     print(needsToBeThreadSafe);
   }
}
now i require that between M1() and M2() calls 'needsToBeThreadSafe' stays Thread Safe.
How About:
public static void M1()
{
    Interlocked.Exchange( ref needsToBeThreadSafe, RandomNumber() );
}
public static void M2()
{
    print( Interlocked.Read( ref needsToBeThreadSafe ) );
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With