Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Static Variable access across threads

I am experiencing a bad behavior in my C# Multiple thread program. Some of my static members are loosing their values in other threads, while some statics of the same Declaring Type, do not loose their values.

public class Context {
  public Int32 ID { get; set; }
  public String Name { get; set; }

  public Context(Int32 NewID, String NewName){
      this.ID = NewID;
      this.Name = NewName;
  }
}

public class Root {
    public static Context MyContext;
    public static Processor MyProcessor;

   public Root(){
     Root.MyContext = new Context(1,"Hal");

     if(Root.MyContext.ID == null || Root.MyContext.ID != 1){
         throw new Exception("Its bogus!") // Never gets thrown
     }

     if(Root.MyContext.Name == null || Root.MyContext.Name != "Hal"){
         throw new Exception("It's VERY Bogus!"); // Never gets thrown
     } 

     Root.MyProcessor = new MyProcessor();
     Root.MyProcessor.Start();
   }
}

public class Processor {
   public Processor() {
   }

   public void Start(){
      Thread T= new Thread (()=> {

          if(Root.MyContext.Name == null || Root.MyContext.Name != "Hal"){
                throw new Exception("Ive lost my value!"); // Never gets Thrown
          }

          if(Root.MyContext.ID == null){
              throw new Exception("Ive lost my value!"); // Always gets thrown
          }

      });
   }
}

IS this a thread mutation problem while using static members of certain types?

like image 823
Rex Whitten Avatar asked Dec 18 '12 19:12

Rex Whitten


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Use volatile or alternatively access the variables using Interlocked.

The problem you run into is that the compiler (as well as the native compiler) is free to optimize access to the variables as he sees fit without them. So he may dump a variable into a register and just not reread it.

In order to avoid it, you must make sure that the variable is actually REALLY read. Volatile does that. Interlocked does that as well (and allows increment / add etc. to happen atomically).

Which is better you have to decide. Both force a memory barrier onto the processor which does have a non trivial cost when done often. One pattern I use regularly is to have those objects read only mostly, so that I only replace the root object (one memory barrier). Dealing with memory barriers manually (possible, read manual for the keyword) is a pain to get right. It is, though, a lot more efficient - depending how much you do there etc. it may be needed.

like image 166
TomTom Avatar answered Oct 11 '22 07:10

TomTom