title: C# Updateing object references and multithreading
body: <p>After reading so much about how to do it, I'm quite confused.</p>
  
  <p>So here is what I want to do:
  I have a datastructure/object that holds all kinds of information. I tread the datastructure as if it were immutable. Whenever I need to update information, I make a DeepCopy and do the changes to it. Then I swap the old and the newly created object.</p>
  
  <p>Now I don't know how to do everything right.</p>
  
  <p>Let's look at it from the side of the reader/consumer threads.</p>
  
  <pre><code>MyObj temp = dataSource;
  var a = temp.a;
  ... // many instructions
  var b = temp.b;
  ....
  </code></pre>
  
  <p>As I understand reading references is atomic. So I don't need any volatile or locking to assign the current reference of dataSource to temp. But what about the Garbage Collection. As I understand the GC has some kind of reference counter to know when to free memory. So when another thread updates dataSource exactly at the moment when dataSource is assigned to temp, does the GC increase the counter on the right memory block?
  The other thing is compiler/CLR optimization. I assign dataSource to temp and use temp to access data members. What does the CLR do? Does it really make a copy of dataSource or does the optimizer just use dataSource to access .a and .b? Let's assume that between temp.a and temp.b are lot's of instructions so that the reference to temp/dataSource cannot be held in a CPU register. So is temp.b really temp.b or is it optimized to dataSource.b because the copy to temp can be optimized away. This is especially important if another thread updates dataSource to point to a new object.</p>
  
  <p>Do I really need volatile, lock, ReadWriterLockSlim, Thread.MemoryBarrier or something else?
  The important thing to me is that I want to make sure that temp.a and temp.b access the old datastructure even when another thread updates dataSource to another newly created data structure. I never change data inside an existing structure. Updates are always done by creating a copy, updating data and then updating the reference to the new copy of the datastructre.</p>
  
  <hr>
  
  <p>Maybe one more question. If I don't use volatile, how long does it take until all cores on all CPUs see the updated reference?</p>
  
  <hr>
  
  <p>When it comes to volatile please have a look here: <a href="https://stackoverflow.com/questions/72275/when-should-the-volatile-keyword-be-used-in-c">When should the volatile keyword be used in C#?</a></p>
  
  <hr>
  
  <p>I have done a little test programm:</p>
  
  <pre><code>namespace test1 {
    public partial class Form1 : Form {
      public Form1() { InitializeComponent(); }
  
      Something sharedObj = new Something();
  
      private void button1_Click(object sender, EventArgs e) {
        Thread t = new Thread(Do);          // Kick off a new thread
        t.Start();                               // running WriteY()
  
        for (int i = 0; i &lt; 1000; i++) {
          Something reference = sharedObj;
  
          int x = reference.x; // sharedObj.x;
          System.Threading.Thread.Sleep(1);
          int y = reference.y; // sharedObj.y;
  
          if (x != y) {
            button1.Text = x.ToString() + "/" + y.ToString();
            Update();
          }
        }
      }
  
      private void Do() {
        for (int i = 0; i &lt; 1000000; i++) {
          Something someNewObject = sharedObj.Clone(); // clone from immutable
          someNewObject.Do();
          sharedObj = someNewObject; // atomic
        }
      }
    }
  
    class Something {
      public Something Clone() { return (Something)MemberwiseClone(); }
      public void Do() { x++; System.Threading.Thread.Sleep(0); y++; }
      public int x = 0;
      public int y = 0;
    }
  }
  </code></pre>
  
  <p>In Button1_click there is a for-loop and inside the for-loop I access a datastructure/object once using the direct "shareObj" and once using a temporarily created "reference". Using the reference is enough to make sure that "var a" and "var b" are initialized with values from the same object.</p>
  
  <p>The only thing I don't understand is, why is "Something reference = sharedObj;" not optimized away and "int x = reference.x;" not replaced by "int x = sharedObj.x;"?</p>
  
  <p>How does the compiler, jitter know not to optimize this? Or are temporarily objects never optimized in C#?</p>
  
  <p>But most important: Is my example running as intended because it is correct or is it running as intended only by accident?</p>