title: Understanding Multithreading and execution process
body: <p>I'm not clear on the process that CLR performs when running a program.  I'm not quite clear on when to use lock() or some other type of thread safe objects. </p>
  
  <p>Please help me understand the following:</p>
  
  <p>Having this code:</p>
  
  <pre><code>    public class Person {
          string Name {get;set;}
      }
  
  
  public class Calc {
      public string DoStuff(Person p){
          // perform some processing on person object
          // maybe call external API and update person object
      }
  
  }
  </code></pre>
  
  <p>Please correct me if I'm wrong but this is my understanding of Example 1. In single threaded app (suppose it's a mvc application and I'm using DI and already registered "Service" in the container. </p>
  
  <p>I call Service.Work() from somewhere in my application.  new Calc() is created (CLR allocates this object in memory) and Calc.DoStuff() does computation on Person object. Since before the call new Calc() is created and get called from two browsers they are in separate execution paths. Is this correct?</p>
  
  <pre><code>// Example 1
  public class Service {
      public void Work(person) {
         var calc = new Calc();
         string test = calc.DoStuff(person);
      }
  }
  
  // Example 2
  public class Service {  
      public Service() {
          var calc = new Calc(); // this is different
      }   
      public void Work(person) {
          string test = calc.DoStuff(person);
      }
  }
  </code></pre>
  
  <p>Now what happens in the same scenario, however this time at the time of allocating Service in memory Calc is instantiated in the Service's constructor. Would that cause problems with to simultaneous calls to Work() (since there is only ONE instance of Calc? object. </p>
  
  <ul>
  <li>What happens then? Would string name be populated with the "latest" string?). </li>
  <li>Would lock be required here so that first it has to finish 1st calls request then second?</li>
  <li>What happens when two browsers call Work(Person) sending two instances of Person to that method. Suppose Person objects goes "inside" Calc class and then an external API is called - and stalls for a bit. Then second request is called to Calc(Person) would this Person object be updated independently (even though there is only one instance of Calc()?). Or would the first request (the one that stalled) be cancelled by the second and forgotten?</li>
  </ul>